blib/lib/WWW/Shopify/Test.pm

filter: Statementstmt 12.5% 69 / 552 Branchbran 0.3% 1 / 310 Conditioncond 0.0% 0 / 102 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 27.8% 22 / 79 Podpod 44.2% 23 / 52 totaltotal 9.9% 115 / 1161 CC 206 SCAR 54.4
CC
206
Cov
7%
generate_class
1260.0
generate_conditions
380.0
generate_thread
240.0
CRAP 230.8
« blib/lib/WWW/Shopify/URLHandler.pm ↑ Summary blib/lib/WWW/Shopify/Model/Item.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 WWW::Shopify::Test - A subclass of WWW::Shopify which represents a testing environment.
9  
10 =cut
11  
12 =head1 DESCRIPTION
13  
14 A WWW::Shopify object that instead of making calls to a particular URL uses a database to represent an instance of a shopify store.
15  
16 In addition to providing a method to access the database, the test subclass also provides a method to generate entries in the database for testing purposes using the generate method.
17  
18 =cut
19  
20 =head1 EXAMPLES
21  
22 Should be used as L<WWW::Shopify> as a normal store. Prior to doing this, the object must be 'associated' with a particular store on our fake database.
23  
24 It can be done like the following:
25  
26 my $db = WWW::Shopify::Model::DBIx::Schema->connect('dbi:SQLite:dbname=' . tmpnam(), { RaiseError => 1, AutoCommit => 1 });
27 $db->generate([WWW::Shopify::Model::Shop]);
28 my $sa = new WWW::Shopify::Test($db);
29 $sa->associate_randomly();
30  
31 =cut
32  
33 =head1 METHODS
34  
35 A bunch of methods are unique to the WWW::Shopify::Test, and not part of the normal WWW::Shopify, namely the associate methods and the generate method; they are listed here.
36  
37 =cut
38  
39 1 ▶use WWW::Shopify;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use WWW::Shopify::Common::DBIx;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 package WWW::Shopify::Test;
44 1 ▶use parent 'WWW::Shopify';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use String::Random qw(random_regex random_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Data::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use List::Util qw(shuffle first);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 # Make sure we include all our models so that when people call the model, we actually know what they're talking about.
51 1 ▶BEGIN { eval(join("\n", map { "require $_;" } findallmod WWW::Shopify::Model)); die $@ if $@; }
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
52  
53  
54 1 my %api_recycle_times = ();
55  
56 #sub CALL_LIMIT_REFRESH { return $ENV{'SHOPIFY_CALL_REFRESH'} ? $ENV{'SHOPIFY_CALL_REFRESH'} : WWW::Shopify->CALL_LIMIT_REFRESH; }
57 0 ▶sub PULLING_ITEM_LIMIT { return $ENV{'SHOPIFY_PULLING_ITEM_LIMIT'} ? $ENV{'SHOPIFY_PULLING_ITEM_LIMIT'} : WWW::Shopify->PULLING_ITEM_LIMIT; }
Subroutine PULLING_ITEM_LIMIT: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SHOPIFY_PULLING_ITEM_LIMIT'} ? : 0 0 0
58 0 ▶sub CALL_LIMIT_MAX { return $ENV{'SHOPIFY_CALL_MAX'} ? $ENV{'SHOPIFY_CALL_MAX'} : WWW::Shopify->CALL_LIMIT_MAX; }
Subroutine CALL_LIMIT_MAX: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SHOPIFY_CALL_MAX'} ? : 0 0 0
59 0 ▶sub CALL_LIMIT_LEAK_TIME { return $ENV{'SHOPIFY_CALL_LEAK_TIME'} ? $ENV{'SHOPIFY_CALL_LEAK_TIME'} : WWW::Shopify->CALL_LIMIT_LEAK_TIME; }
Subroutine CALL_LIMIT_LEAK_TIME: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SHOPIFY_CALL_LEAK_TIME'} ? : 0 0 0
60 0 ▶sub CALL_LIMIT_LEAK_RATE { return $ENV{'SHOPIFY_CALL_LEAK_RATE'} ? $ENV{'SHOPIFY_CALL_LEAK_RATE'} : WWW::Shopify->CALL_LIMIT_LEAK_RATE; }
Subroutine CALL_LIMIT_LEAK_RATE: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SHOPIFY_CALL_LEAK_RATE'} ? : 0 0 0
61  
62 sub new {
63 0 ▶ my ($package, $dbschema, $shop, $access_token) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
64 0 my $self = bless {
65 _db => $dbschema,
66 _associatedShop => undef,
67 _mapper => new WWW::Shopify::Common::DBIx(),
68 _callback => undef,
69 _access_token => $access_token
70 }, $package;
71 0 ▶ $self->associate($shop) if $shop;
Branch: 1 branch
BranchTrueFalseTotal
if $shop 0 0 0
72 0 return $self;
73 }
74  
75 0 ▶sub db { $_[0]->{_db} = $_[1] if defined $_[1]; return $_[0]->{_db}; }
Subroutine db: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
76 0 ▶sub callback { $_[0]->{_callback} = $_[1] if defined $_[1]; return $_[0]->{_callback}; }
Subroutine callback: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
77  
78 0 ▶sub access_token { $_[0]->{_access_token} = $_[1] if defined $_[1]; return $_[0]->{_access_token}; }
Subroutine access_token: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
79  
80 =head2 ASSOCIATION METHODS
81  
82 =head3 associate
83  
84 Takes in a Model::DBIx::Model::Shop. Associate this object with the shop, meaning that every call you make on this object will target that particular shop.
85  
86 This must be called (directly or indirectly) before any calls can be made to the shop.
87  
88 =cut
89  
90 sub associate {
91 0 ▶ my $self = $_[0];
Subroutine associate: not called CC=6 SCAR=37.4
Pod documented
92 0 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
93 0 my $shop = $_[1];
94 0 ▶ if (ref($shop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $shop) { } 0 0 0
95 0 ▶ die ref($shop) unless ref($shop) =~ m/Model::.*Shop$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($shop) =~ /Model::.*Shop$/ 0 0 0
96 0 $self->{_associatedShop} = $shop;
97 0 ▶ $api_recycle_times{$shop->id} = {calls => 0, recycled => time} if !exists $api_recycle_times{$shop->id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $api_recycle_times{$shop->id} 0 0 0
98 }
99 else {
100 0 $self->associate($self->{_db}->resultset('Model::Shop')->find({myshopify_domain => $shop}));
101 0 ▶ die new WWW::Shopify::Exception("Can't find shop $shop to associate with.") unless $self->associate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
102 }
103 }
104 0 return $self->{_associatedShop};
105 }
106  
107 =head3 associate_randomly
108  
109 Calls associate with a random shop from the database.
110  
111 =cut
112  
113 sub associate_randomly {
114 0 ▶ my $self = $_[0];
Subroutine associate_randomly: not called CC=2 SCAR=17.9
Pod documented
115 0 my @shops = $self->{_db}->resultset('Model::Shop')->all();
116 0 ▶ die new WWW::Shopify::Exception("Unable to associate randomly; no shops present.") unless int(@shops) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless int @shops > 0 0 0 0
117 0 return $self->associate($shops[rand(int(@shops))]);
118 }
119  
120 =head3 associate_unlinked
121  
122 Calls associate with a shop that is NOT in the list of urls that you pass into this function.
123  
124 $SA->associate_unlinked('hostname1', 'hostname2');
125  
126 =cut
127  
128 sub associate_unlinked {
129 0 ▶ my ($self, @ids) = @_;
Subroutine associate_unlinked: not called CC=2 SCAR=17.9
Pod documented
130 0 my @shops = $self->{_db}->resultset('Model::Shop')->search( { myshopify_domain => { 'NOT IN' => [@ids] } } )->all();
131 0 ▶ die new WWW::Shopify::Exception("Unable to associate unlinked; no unlinked shops.") unless int(@shops) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless int @shops > 0 0 0 0
132 0 return $self->associate($shops[rand(int(@shops))]);
133 }
134  
135 =head3 associate_linked
136  
137 Calls associate with a shop that is in the list of urls that you pass in to this function.
138  
139 $SA->associate_linked('hostname1', 'hostname2');
140  
141 =cut
142  
143 # Associates with a paricular shop that IS part of the app.
144 sub associate_linked {
145 0 ▶ my ($self, $ids) = @_;
Subroutine associate_linked: not called CC=2 SCAR=17.9
Pod documented
146 0 my $condition = map { { myshopify_domain => $_ } } @$ids;
147 0 my @shops = $self->{_db}->resultset('Model::Shop')->search( { myshopify_domain => { 'IN' => $ids } } )->all();
148 0 ▶ die new WWW::Shopify::Exception("Unable to associate linked; no linked shops.") unless int(@shops) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless int @shops > 0 0 0 0
149 0 return $self->associate($shops[rand(int(@shops))]);
150 }
151  
152 0 ▶sub get_url($$@) { die new WWW::Shopify::Exception("Can't use URL methods on the test object."); }
Subroutine get_url: not called CC=1 SCAR=6.9
Pod undocumented
153 0 ▶sub post_url($$@) { die new WWW::Shopify::Exception("Can't use URL methods on the test object."); }
Subroutine post_url: not called CC=1 SCAR=6.9
Pod undocumented
154 0 ▶sub put_url($$@) { die new WWW::Shopify::Exception("Can't use URL methods on the test object."); }
Subroutine put_url: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub delete_url($$@) { die new WWW::Shopify::Exception("Can't use URL methods on the test object."); }
Subroutine delete_url: not called CC=1 SCAR=6.9
Pod undocumented
156  
157 =head2 GENERATION METHODS
158  
159 As of now, there's only one generation function. These functions generate objects for the underlying database.
160  
161 =cut
162  
163 =head3 generate
164  
165 The generate method. Generates a whole ton of stuff for your database. Doesn't have to be associate with a shop; generates shops as well.
166  
167 Essentially, what you want to do when you call this is pass in a list of class names (WWW::Shopify::Model:: style, not the DBIx ones) in an array, and this funciton will generate them, and if necessary their dependencies.
168  
169 It'll generate 50*(:: count - 2)^2 of the class (so WWW::Shopify::Model::Product'll get 50, but there'll be 200 WWW::Shopify::Model::Product::Variants)
170  
171 WWW::Shopify::Model::Shop is a special case, that's set to 6, basically for lulz.
172  
173 Calls go like so:
174  
175 $SA->generate(['WWW::Shopify::Model::Shop', 'WWW::Shopify::Model::Product']);
176  
177 Note, this can take a while, depending on the speed of your computer/virtual machine. Once it's finished, you should have a nice little database full of bogus data of the proper type.
178  
179 =cut
180  
181 sub get_class {
182 0 ▶ my ($self, $parent, $package, $ids) = @_;
Subroutine get_class: not called CC=1 SCAR=6.9
Pod undocumented
183
184 }
185  
186 # Used to make objects make sense.
187 sub correct_object {
188 0 ▶ my ($self, $object) = @_;
Subroutine correct_object: not called CC=3 SCAR=24.8
Pod undocumented
189 0 ▶ if (ref($object) =~ m/Product$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($object) =~ /Product$/) 0 0 0
190 0 my $i = 1;
191 0 $_->update({ position => $i++ }) for ($object->options->all);
192 }
193 }
194  
195 1 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
196 1 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
197 sub generate_class {
198 0 ▶ my ($self, $package, $ids, $parent, $restrictions) = @_;
Subroutine generate_class: not called CC=35 SCAR=71.4
Pod undocumented
199 0 ▶ $ids = {} unless defined $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ids 0 0 0
200  
201 0 print STDERR "Generating $package...\n";
202 0 my $fields = $package->fields;
203 0 my $object = $self->{_db}->resultset(transform_name($package))->new({});
204 # Simple fields.
205 0 foreach my $field (grep { !$_->is_relation } values(%$fields)) {
206 0 my $name = $field->name;
207 0 my $value = $field->generate();
208 # If it's a hash, encode it as JSON.
209 0 ▶ $value = encode_json($value) if ref($value) && ref($value) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $value eq "HASH" 0 0 0
Condition0%0/3
ref $value and ref $value eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and ref $value eq "HASH"
ref $value ref $value eq "HASH"
Truth table0%0/3
A and B
A= ref $valueB= ref $value eq "HASH"
ABresult
0X0
100
111
210 0 $object->$name($value);
211 }
212 0 ▶ $ids->{$package} = [] unless defined $ids->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ids->{$package} 0 0 0
213  
214 0 my @identifiers = $package->identifier;
215 0 my $identifier = $identifiers[0];
216 0 ▶ if ($identifiers[0] eq "id" && $restrictions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identifiers[0] eq "id" and $restrictions) 0 0 0
Condition0%0/3
$identifiers[0] eq "id" and $restrictions
!ll&&!rl&&r
000
MC/DC0%0/2
$identifiers[0] eq "id" and $restrictions
$identifiers[0] eq "id" $restrictions
Truth table0%0/3
A and B
A= $identifiers[0] eq "id"B= $restrictions
ABresult
0X0
100
111
217 0 my $tentative;
218 0 my %present = map { $_ => 1 } @{$ids->{$package}};
219 do {
220 0 $tentative = int(rand($restrictions->[1] - $restrictions->[0])) + $restrictions->[0];
221 0 ▶ } while (exists $present{$tentative});
Branch: 1 branch
BranchTrueFalseTotal
exists $present{$tentative} and do { do { $tentative = int(rand $restrictions->[1] - $restrictions->[0]) + $restrictions->[0] } } 0 0 0
222 0 $object->$identifier($tentative);
223 }
224 0 my $id = $object->$identifier;
225 0 push(@{$ids->{$package}}, $id);
226 # Set parent ID.
227 0 my $parent_variable = $object->parent_variable;
228 0 ▶ if ($parent_variable && ((!$package->parent && ref($parent) !~ m/Shop$/) || $package->parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_variable and !($package->parent || ref($parent) =~ /Shop$/) || $package->parent) 0 0 0
Condition0%0/9
$parent_variable and !($package->parent || ref($parent) =~ /Shop$/) || $package->parent
!ll&&!rl&&r
000
not $package->parent || ref($parent) =~ /Shop$/ or $package->parent
l!l&&r!l&&!r
000
$package->parent || ref($parent) =~ /Shop$/
l!l&&r!l&&!r
000
MC/DC0%0/4
$parent_variable and !($package->parent || ref($parent) =~ /Shop$/) || $package->parent
$parent_variable $package->parent ref($parent) =~ /Shop$/ $package->parent
Truth table0%0/6
A and not(B || C) or D
A= $parent_variableB= $package->parentC= ref($parent) =~ /Shop$/D= $package->parent
ABCDresult
0XXX0
11X00
10100
100X1
11X11
10111
229 0 my $parent_id = $parent->id;
230 0 ▶ die "Can't find parent for $package looking in " . $package->parent unless $ids->{$package->parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $$ids{$package->parent} 0 0 0
231 0 my @ids = @{$ids->{$package->parent}};
232 # If we have something that's basically not a shop.
233 0 ▶ if ($parent->represents ne $package->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->represents ne $package->parent) 0 0 0
234 0 $parent_id = $ids[int(rand(int(@ids)))];
235 }
236 0 $object->$parent_variable($parent_id);
237 }
238  
239 # If we're not a shop and we don't have a parent, we have a shop_id field.
240 0 ▶ if (WWW::Shopify::Common::DBIx::has_shop_field($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (WWW::Shopify::Common::DBIx::has_shop_field($object)) 0 0 0
241 0 ▶ die new WWW::Shopify::Exception("Everything not a shop requires a parent.") unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
242 0 ▶ $object->shop_id((ref($parent) !~ m/Shop$/) ? $parent->shop_id : $parent->id);
Branch: 1 branch
BranchTrueFalseTotal
!(ref($parent) =~ /Shop$/) ? : 0 0 0
243 }
244  
245 # Simpler relationships.
246
247 0 $object = $object->insert;
248 0 ▶ foreach my $field (grep { $_->is_relation && !$_->is_parent } values(%$fields)) {
Branch: 1 branch
BranchTrueFalseTotal
$_->is_relation and not $_->is_parent 0 0 0
249 0 my $relation = $field->relation;
250 0 ▶ if ($field->is_db_belongs_to && $field->db_rand_count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($field->is_db_belongs_to and $field->db_rand_count > 0) { } 0 0 0
elsif ($field->is_db_has_one and $field->db_rand_count > 0) { } 0 0 0
elsif ($field->is_db_has_many) { } 0 0 0
Condition0%0/3
$field->is_db_belongs_to and $field->db_rand_count > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$field->is_db_belongs_to and $field->db_rand_count > 0
$field->is_db_belongs_to $field->db_rand_count > 0
Truth table0%0/3
A and B
A= $field->is_db_belongs_toB= $field->db_rand_count > 0
ABresult
0X0
100
111
251 0 my $belongs_to;
252 0 ▶ $self->generate_class($relation, $ids, $parent, $restrictions) unless ($ids->{$relation});
Branch: 1 branch
BranchTrueFalseTotal
unless $ids->{$relation} 0 0 0
253 0 $belongs_to = $ids->{$relation}->[rand(int(@{$ids->{$relation}}))];
254 0 my $field_name = $field->name;
255 0 ▶ $field_name .= "_id" unless $field_name =~ m/_id$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_name =~ /_id$/ 0 0 0
256 0 $object->$field_name($belongs_to);
257 }
258 ▶ elsif ($field->is_db_has_one && $field->db_rand_count > 0) {
Condition0%0/3
$field->is_db_has_one and $field->db_rand_count > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$field->is_db_has_one and $field->db_rand_count > 0
$field->is_db_has_one $field->db_rand_count > 0
Truth table0%0/3
A and B
A= $field->is_db_has_oneB= $field->db_rand_count > 0
ABresult
0X0
100
111
259 0 $self->generate_class($relation, $ids, $object, $restrictions);
260 }
261 elsif ($field->is_db_has_many) {
262 0 my $many_count = $field->db_rand_count;
263 0 ▶ for (my $c = 0; $c < $many_count; ++$c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $many_count) 0 0 0
264 0 $self->generate_class($relation, $ids, $object, $restrictions);
265 }
266 }
267 }
268 0 $self->correct_object($object);
269 0 $object->update;
270 # Many-Many
271 0 ▶ foreach my $field (grep { $_->is_relation && $_->is_db_many_many} values(%$fields)) {
Branch: 1 branch
BranchTrueFalseTotal
$_->is_relation and $_->is_db_many_many 0 0 0
272 0 my $relation = $field->relation;
273 0 ▶ next if $relation =~ m/Metafield/i && $ENV{'SKIP_METAFIELDS'};
Branch: 1 branch
BranchTrueFalseTotal
if $relation =~ /Metafield/i and $ENV{"SKIP_METAFIELDS"} 0 0 0
Condition0%0/3
$relation =~ /Metafield/i and $ENV{"SKIP_METAFIELDS"}
!ll&&!rl&&r
000
MC/DC0%0/2
$relation =~ /Metafield/i and $ENV{"SKIP_METAFIELDS"}
$relation =~ /Metafield/i $ENV{"SKIP_METAFIELDS"}
Truth table0%0/3
A and B
A= $relation =~ /Metafield/iB= $ENV{"SKIP_METAFIELDS"}
ABresult
0X0
100
111
274 0 my $accessor = "add_to_" . $field->name . "_hasmany";
275 0 ▶ if (defined $ids->{$relation}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ids->{$relation}) 0 0 0
276 0 my $many_count = $field->db_rand_count;
277 0 ▶ for (my $c = 0; $c < $many_count; ++$c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $many_count) 0 0 0
278 0 $self->generate_class($relation, $ids, $parent, $restrictions);
279 }
280 }
281 0 my @shuffled = shuffle(@{$ids->{$relation}});
282  
283 $object->$accessor({
284 $package->singular . "_id" => $id,
285 $relation->singular . "_id" => $_
286 0 }) for (grep { defined $_ } @shuffled[0..rand(5)]);
287 }
288 0 return $object;
289 }
290  
291 sub finalize_class {
292 0 ▶ my ($self, $object) = @_;
Subroutine finalize_class: not called CC=2 SCAR=17.9
Pod undocumented
293 0 my $package = ref($object);
294 0 my %dispatch_table = (
295
296 );
297 0 my $sub = $dispatch_table{$package};
298 0 ▶ $object = $sub->($object) if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
299 0 return $object;
300 }
301  
302  
303 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
304 sub check_increment_calls {
305 0 ▶ my ($self) = @_;
Subroutine check_increment_calls: not called CC=4 SCAR=30.0
Pod undocumented
306 0 $self->last_timestamp(DateTime->now);
307 0 ▶ die new WWW::Shopify::Exception::InvalidKey() unless $self->access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_token 0 0 0
308 0 my $duration = time - $api_recycle_times{$self->associate->id}->{recycled};
309 0 my $api_calls = $api_recycle_times{$self->associate->id}->{calls};
310 0 print STDERR "ID: " . $self->associate->id . " Seconds: " . $duration . " Calls: " . $api_recycle_times{$self->associate->id}->{calls} . "\n";
311 0 $api_calls = max(0, $api_calls - (int($duration / $self->CALL_LIMIT_LEAK_TIME) * $self->CALL_LIMIT_LEAK_RATE));
312 0 $self->reset_call_limit($api_calls);
313 0 ▶ if ($api_calls == $self->CALL_LIMIT_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api_calls == $self->CALL_LIMIT_MAX) 0 0 0
314 0 ▶ if ($self->sleep_for_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sleep_for_limit) { } 0 0 0
315 0 sleep(1);
316 0 $duration = time - $api_recycle_times{$self->associate->id}->{recycled};
317 0 $api_calls = $api_recycle_times{$self->associate->id}->{calls};
318 0 $api_calls = max(0, $api_calls - (int($duration / $self->CALL_LIMIT_LEAK_TIME) * $self->CALL_LIMIT_LEAK_RATE));
319 0 $self->reset_call_limit($api_calls);
320 } else {
321 0 die WWW::Shopify::Exception::CallLimit->new(HTTP::Response->new(429, "API Call Limit Reached"));
322 }
323 }
324 0 $self->api_calls($api_calls + 1);
325 0 $api_recycle_times{$self->associate->id}->{calls} = $api_calls+1;
326 }
327 sub reset_call_limit {
328 0 ▶ my ($self, $calls) = @_;
Subroutine reset_call_limit: not called CC=2 SCAR=17.9
Pod undocumented
329 0 ▶ die new WWW::Shopify::Exception("Cannot reset call limit on unassociated api.") unless $self->associate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
330 0 $api_recycle_times{$self->associate->id}->{calls} = $calls;
331 0 $api_recycle_times{$self->associate->id}->{recycled} = time;
332 }
333 sub reset_all_calls {
334 0 ▶ for (keys(%api_recycle_times)) {
Subroutine reset_all_calls: not called CC=2 SCAR=17.9
Pod undocumented
335 0 $api_recycle_times{$_}->{calls} = 0;
336 0 $api_recycle_times{$_}->{recycled} = time;
337 }
338 }
339  
340 sub check_scope {
341 0 ▶ my ($self, $package, $read_write) = @_;
Subroutine check_scope: not called CC=1 SCAR=6.9
Pod undocumented
342 0 return undef;
343 }
344  
345 sub get_timestamp {
346 0 ▶ return DateTime->now->subtract( seconds => 6);
Subroutine get_timestamp: not called CC=1 SCAR=6.9
Pod documented
347 }
348  
349 #use Sys::CPU;
350 #use threads;
351 1 my $internal_range = 1000000000;
352 1 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
353 1 ▶use List::MoreUtils qw(part);
Subroutine BEGIN: called CC=1 SCAR=0.0
354 sub generate {
355 0 ▶ my ($self, %items) = @_;
Subroutine generate: not called CC=2 SCAR=17.9
Pod documented
356 # 0 is auto-pick. Each is on a per-item basis.
357 # Take at least 5 shops.
358 0 my $shop_count = $items{'WWW::Shopify::Model::Shop'};
359 0 ▶ $shop_count = 5 unless defined $shop_count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shop_count 0 0 0
360 #my $cpu_count = min(Sys::CPU::cpu_count(), $shop_count);
361 #$cpu_count = min($ENV{'CPU'}, $cpu_count) if $ENV{'CPU'};
362 #$cpu_count = 1 unless $ENV{'CPU'};
363 0 my $cpu_count = 1;
364 0 print STDERR "Beginning generation of $shop_count shops on $cpu_count threads...\n";
365 0 my $i = 0;
366 0 ▶ my @partitions = part { $i++ % $cpu_count } 1..$shop_count;
Subroutine __ANON__: not called CC=1 SCAR=6.9
367 sub generate_thread {
368 0 ▶ my ($self, $partitions, $items) = @_;
Subroutine generate_thread: not called CC=15 SCAR=54.8
Pod undocumented
369 0 foreach my $initial_range (@$partitions) {
370 0 my $min_range = $initial_range * $internal_range;
371 0 my $max_range = ($initial_range+1) * $internal_range;
372  
373 0 print STDERR "====== GENERATING SHOP =======\n";
374 0 my %ids = ( );
375 0 my $shop = $self->generate_class('WWW::Shopify::Model::Shop', \%ids);
376 0 %ids = ( 'WWW::Shopify::Model::Shop' => [$shop->id] );
377 0 my %counts = map { $_ => $items->{$_} } grep { $_ !~ m/Shop$/ } keys(%$items);
378  
379 0 ▶ while (int(keys(%counts)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int keys %counts > 0) 0 0 0
380 # This should be really fixed. We really shouldn't have to do this.
381 0 my @keys = sort { ((($a =~ m/Checkout/) ? 11 : 0) + ($a =~ m/Order/ ? 10 : 0) + ($a =~ m/Customer/ ? 9 : 0) + ($a =~ m/Transaction/ ? 12 : 0) + $a->nested_level + ($self->{_mapper}->transform_package($a)->parent_variable ? 1 : 0)) <=>
382 0 ▶ ((($b =~ m/Checkout/) ? 11 : 0) + ($b =~ m/Order/ ? 10 : 0) + ($b =~ m/Customer/ ? 9 : 0) + ($b =~ m/Transaction/ ? 12 : 0) + $b->nested_level + ($self->{_mapper}->transform_package($b)->parent_variable ? 1 : 0)) } keys(%counts);
Branch: 10 branches
BranchTrueFalseTotal
$a =~ /Checkout/ ? : 0 0 0
$a =~ /Order/ ? : 0 0 0
$a =~ /Customer/ ? : 0 0 0
$a =~ /Transaction/ ? : 0 0 0
$self->{'_mapper'}->transform_package($a)->parent_variable ? : 0 0 0
$b =~ /Checkout/ ? : 0 0 0
$b =~ /Order/ ? : 0 0 0
$b =~ /Customer/ ? : 0 0 0
$b =~ /Transaction/ ? : 0 0 0
$self->{'_mapper'}->transform_package($b)->parent_variable ? : 0 0 0
383 0 foreach my $item (@keys) {
384 $self->{_db}->txn_do(sub {
385 0 ▶ $self->generate_class($item, \%ids, $shop, [$min_range, $max_range]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
386 0 });
387 0 ▶ delete $counts{$item} if (--$counts{$item} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if --$counts{$item} == 0 0 0 0
388 }
389 }
390 0 print STDERR "====== SHOP FINISHED =======\n";
391 }
392 }
393 #if ($cpu_count > 1) {
394 #my @threads = map { threads->create('generate_thread', $self, $partitions[$_], \%items) } (1..$cpu_count);
395 #$_->join() for (@threads);
396 #}
397 #else {
398 0 generate_thread($self, $partitions[0], \%items);
399 #}
400 }
401  
402 sub transform_name($) {
403 0 ▶ die "Improperly formatted package: " . $_[0] unless $_[0] =~ /WWW::Shopify::(.+)/;
Subroutine transform_name: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /WWW::Shopify::(.+)/ 0 0 0
404 0 return $1;
405 }
406  
407 1 ▶use WWW::Shopify::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
409 sub generate_conditions {
410 0 ▶ my ($self, $package, $specs, $rs) = @_;
Subroutine generate_conditions: not called CC=19 SCAR=59.4
Pod undocumented
411 0 my %conditions = ();
412 0 my $queries = $package->queries;
413 0 ▶ $rs = $rs->search({}, { rows => $self->PULLING_ITEM_LIMIT, page => $specs->{page} }) if exists $specs->{limit} && exists $specs->{page};
Branch: 1 branch
BranchTrueFalseTotal
if exists $specs->{"limit"} and exists $specs->{"page"} 0 0 0
Condition0%0/3
exists $specs->{"limit"} and exists $specs->{"page"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $specs->{"limit"} and exists $specs->{"page"}
exists $specs->{"limit"} exists $specs->{"page"}
Truth table0%0/3
A and B
A= exists $specs->{"limit"}B= exists $specs->{"page"}
ABresult
0X0
100
111
414 0 ▶ $rs = $rs->search({}, { rows => $specs->{limit} }) if exists $specs->{limit} && !exists $specs->{page};
Branch: 1 branch
BranchTrueFalseTotal
if exists $specs->{"limit"} and not exists $specs->{"page"} 0 0 0
Condition0%0/3
exists $specs->{"limit"} and not exists $specs->{"page"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $specs->{"limit"} and not exists $specs->{"page"}
exists $specs->{"limit"} not exists $specs->{"page"}
Truth table0%0/3
A and not(B)
A= exists $specs->{"limit"}B= not exists $specs->{"page"}
ABresult
0X0
100
111
415
416
417 my %override = (
418 'WWW::Shopify::Model::Order' => {
419 'status' => sub {
420 0 ▶ my ($rs, $value) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
421 0 ▶ return $rs if $value eq "any";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "any" 0 0 0
422 0 ▶ return $rs->search({ closed_at => undef, cancelled_at => undef }) if $value eq "open";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "open" 0 0 0
423 0 ▶ return $rs->search({ cancelled_at => { '!=' => undef } }) if $value eq "cancelled";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "cancelled" 0 0 0
424 0 ▶ return $rs->search({ closed_at => { '!=' => undef } }) if $value eq "closed";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "closed" 0 0 0
425 }
426 }
427 0 );
428  
429 0 foreach my $query (values(%$queries)) {
430 0 ▶ next unless exists $specs->{$query->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$specs{$query->name} 0 0 0
431 0 my $value = $specs->{$query->name};
432 0 ▶ $value = $self->{_db}->storage->datetime_parser->format_datetime($value) if ref($value) eq "DateTime";
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "DateTime" 0 0 0
433
434 0 ▶ if ($override{$package} && $override{$package}->{$query->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($override{$package} and $override{$package}{$query->name}) { } 0 0 0
Condition0%0/3
$override{$package} and $override{$package}{$query->name}
!ll&&!rl&&r
000
MC/DC0%0/2
$override{$package} and $override{$package}{$query->name}
$override{$package} $override{$package}{$query->name}
Truth table0%0/3
A and B
A= $override{$package}B= $override{$package}{$query->name}
ABresult
0X0
100
111
435 0 my $sub = $override{$package}->{$query->name};
436 0 $rs = &$sub($rs, $value);
437 }
438 else {
439 0 ▶ $conditions{$query->field_name} = {} unless exists $conditions{$query->field_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conditions{$query->field_name} 0 0 0
440 0 ▶ if (ref($query) =~ m/LowerBound$/) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref($query) =~ /LowerBound$/) { } 0 0 0
elsif (ref($query) =~ /UpperBound$/) { } 0 0 0
elsif (ref($query) =~ /Enum$/) { } 0 0 0
elsif (ref($query) =~ /Match$/) { } 0 0 0
elsif (ref($query) =~ /Custom$/) { } 0 0 0
441 0 $conditions{$query->field_name}->{'>='} = $value;
442 }
443 elsif (ref($query) =~ m/UpperBound$/) {
444 0 $conditions{$query->field_name}->{'<='} = $value;
445 }
446 elsif (ref($query) =~ m/Enum$/) {
447 0 $conditions{$query->field_name} = $value;
448 die new WWW::Shopify::Exception("Can't specify $value for enum " . $query->name . ", must be one of the following: " . join(", ", $query->enums)) unless
449 0 ▶ first { $_ eq $value } $query->enums;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $value; } , $query->enums) 0 0 0
450 0 ▶ delete $conditions{$query->field_name} if $value eq "any";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "any" 0 0 0
451 }
452 elsif (ref($query) =~ m/Match$/) {
453 0 $conditions{$query->field_name} = $value;
454 }
455 elsif (ref($query) =~ m/Custom$/) {
456 0 $rs = $query->routine->($rs, $value);
457 }
458 }
459 }
460 # We keep the conditions hash because of legacy reasons.
461 0 ▶ $rs = $rs->search({%conditions}) if %conditions;
Branch: 1 branch
BranchTrueFalseTotal
if %conditions 0 0 0
462 0 return $rs;
463 }
464  
465 sub filter_gettable {
466 0 ▶ my ($self, $obj) = @_;
Subroutine filter_gettable: not called CC=11 SCAR=48.8
Pod undocumented
467 0 my %fields = map { $_ => 1 } $obj->get_fields;
468 0 my @keys = keys(%{$obj->fields});
469 0 foreach my $key (@keys) {
470 0 ▶ if (!$fields{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $fields{$key}) { } 0 0 0
elsif ($obj->{$key}) { } 0 0 0
471 0 delete $obj->{$key};
472 }
473 elsif ($obj->{$key}) {
474 0 ▶ if (ref($obj->{$key}) =~ m/WWW::Shopify::Model/) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref($obj->{$key}) =~ /WWW::Shopify::Model/) { } 0 0 0
elsif (ref $obj->{$key} eq 'ARRAY') { } 0 0 0
elsif (ref $obj->{$key} eq 'HASH') { } 0 0 0
475 0 $self->filter_gettable($obj->{$key});
476 }
477 elsif (ref($obj->{$key}) eq "ARRAY") {
478 0 ▶ for (grep { $_ && ref($_) =~ m/WWW::Shopify::Model/ } @{$obj->{$key}}) {
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref($_) =~ /WWW::Shopify::Model/ 0 0 0
479 0 $self->filter_gettable($_);
480 }
481 }
482 elsif (ref($obj->{$key}) eq "HASH") {
483 0 ▶ for (grep { $_ && ref($_) =~ m/WWW::Shopify::Model/ } values(%{$obj->{$key}})) {
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref($_) =~ /WWW::Shopify::Model/ 0 0 0
484 0 $self->filter_gettable($_);
485 }
486 }
487 }
488 }
489 0 return $obj;
490 }
491  
492 1 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
493 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
494 sub get_all_limit {
495 0 ▶ my ($self, $package, $specs) = @_;
Subroutine get_all_limit: not called CC=10 SCAR=47.0
Pod undocumented
496 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
497 0 ▶ $specs->{"limit"} = $self->PULLING_ITEM_LIMIT unless $specs->{"limit"};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs->{"limit"} 0 0 0
498 0 ▶ return () if ($specs->{limit} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $specs->{"limit"} == 0 0 0 0
499 0 $package = $self->translate_model($package);
500 0 $self->validate_item($package);
501 0 ▶ return $self->get_shop if $package->is_shop;
Branch: 1 branch
BranchTrueFalseTotal
if $package->is_shop 0 0 0
502  
503 0 $self->resolve_trailing_url($package, "get", $specs->{parent});
504
505 0 my @return;
506 0 my $rs = undef;
507 0 ▶ if ($package =~ m/Metafield/ && $specs->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /Metafield/ and $specs->{'parent'}) { } 0 0 0
Condition0%0/3
$package =~ /Metafield/ and $specs->{"parent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$package =~ /Metafield/ and $specs->{"parent"}
$package =~ /Metafield/ $specs->{"parent"}
Truth table0%0/3
A and B
A= $package =~ /Metafield/B= $specs->{"parent"}
ABresult
0X0
100
111
508 0 my $parent = $self->{_mapper}->from_shopify($self->{_db}, $specs->{parent}, $self->associate->id)->contents;
509 0 $rs = $parent->metafields->search({ 'metafield.shop_id' => $self->associate->id() });
510 }
511 else {
512 0 $rs = $self->{_db}->resultset(transform_name($package))->search({ 'me.shop_id' => $self->associate()->id() });
513 0 ▶ if ($package->get_through_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->get_through_parent) 0 0 0
514 0 $rs = $rs->search({ 'me.' . $self->{_mapper}->transform_package($package)->parent_variable => $specs->{parent}->id });
515 }
516 }
517 0 $rs = $self->generate_conditions($package, $specs, $rs);
518 0 $rs = $rs->search({ }, { rows => $specs->{limit} });
519 0 ▶ print STDERR Dumper([$rs->as_query]) if $ENV{'SHOPIFY_LOG'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} 0 0 0
520 0 @return = $rs->all;
521 0 my $call_count = ceil($specs->{limit} / $self->PULLING_ITEM_LIMIT);
522 0 $self->check_increment_calls for (1..$call_count);
523 0 return map { my $obj = $self->{_mapper}->to_shopify($_, $self); $obj->associated_parent($specs->{parent}); $self->filter_gettable($obj) } @return;
524 }
525  
526 sub search {
527 0 ▶ my ($self, $package, $specs) = @_;
Subroutine search: not called CC=10 SCAR=47.0
Pod documented
528 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
529 0 $package = $self->translate_model($package);
530 0 ▶ die new WWW::Shopify::Exception("Unable to search $package; it is not marked as searchable in Shopify's API.") unless $package->searchable;
Branch: 1 branch
BranchTrueFalseTotal
unless $package->searchable 0 0 0
531 0 ▶ die new WWW::Shopify::Exception("Must have a query to search.") unless $specs && $specs->{query};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs and $specs->{"query"} 0 0 0
Condition0%0/3
$specs and $specs->{"query"}
!ll&&!rl&&r
000
MC/DC0%0/2
$specs and $specs->{"query"}
$specs $specs->{"query"}
Truth table0%0/3
A and B
A= $specsB= $specs->{"query"}
ABresult
0X0
100
111
532 0 $self->validate_item($package);
533 0 $self->resolve_trailing_url($package, "get", $specs->{parent});
534  
535 0 my @criteria = split(/\s+/, $specs->{query});
536 0 my %values = map { my @inner = split(/:/, $_); $inner[0] => $inner[1] } @criteria;
537 0 my $rs = $self->{_db}->resultset(transform_name($package))->search({ 'me.shop_id' => $self->associate()->id(), map { $_ => { like => '%' . $values{$_} . '%' } } keys(%values) });
538 0 ▶ print STDERR Dumper([$rs->as_query]) if $ENV{'SHOPIFY_LOG'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} 0 0 0
539 0 my @return = $rs->all;
540  
541 0 ▶ $specs->{"limit"} = $self->PULLING_ITEM_LIMIT unless $specs->{"limit"};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs->{"limit"} 0 0 0
542 0 my $call_count = ceil($specs->{limit} / $self->PULLING_ITEM_LIMIT);
543 0 $self->check_increment_calls for (1..$call_count);
544  
545 0 @return = map { my $obj = $self->{_mapper}->to_shopify($_, $self); $obj->associate($self); $self->filter_gettable($obj) } @return;
546 0 ▶ return @return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
547 0 ▶ return $return[0] if int(@return) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if int @return > 0 0 0 0
548 0 return undef;
549 }
550  
551 sub get_shop {
552 0 ▶ my ($self) = @_;
Subroutine get_shop: not called CC=2 SCAR=17.9
Pod documented
553 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
554 0 $self->check_increment_calls;
555  
556 0 my $obj = $self->{_mapper}->to_shopify($self->associate(), $self);
557 0 return $self->filter_gettable($obj);
558 }
559  
560 sub get_count {
561 0 ▶ my ($self, $package, $specs) = @_;
Subroutine get_count: not called CC=9 SCAR=45.0
Pod documented
562 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
563 0 $self->check_increment_calls;
564 0 $package = $self->translate_model($package);
565
566 0 ▶ die new WWW::Shopify::Exception("Can't count $package.") unless $package->countable;
Branch: 1 branch
BranchTrueFalseTotal
unless $package->countable 0 0 0
567 0 ▶ die new WWW::Shopify::Exception("Can't count $package unless has parent.") if $package->count_through_parent && !$specs->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $package->count_through_parent and not $specs->{"parent"} 0 0 0
Condition0%0/3
$package->count_through_parent and not $specs->{"parent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$package->count_through_parent and not $specs->{"parent"}
$package->count_through_parent not $specs->{"parent"}
Truth table0%0/3
A and not(B)
A= $package->count_through_parentB= not $specs->{"parent"}
ABresult
0X0
100
111
568
569 0 $self->validate_item($package);
570  
571 0 $self->resolve_trailing_url($package, "get", $specs->{parent});
572  
573 0 my $rs = $self->{_db}->resultset(transform_name($package))->search({ 'me.shop_id' => $self->associate()->id() });
574 0 $rs = $self->generate_conditions($package, $specs, $rs);
575
576 0 ▶ if ($package =~ m/Metafield/ && $specs->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /Metafield/ and $specs->{'parent'}) { } 0 0 0
Condition0%0/3
$package =~ /Metafield/ and $specs->{"parent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$package =~ /Metafield/ and $specs->{"parent"}
$package =~ /Metafield/ $specs->{"parent"}
Truth table0%0/3
A and B
A= $package =~ /Metafield/B= $specs->{"parent"}
ABresult
0X0
100
111
577 0 my $parent = $self->{_mapper}->from_shopify($self->{_db}, $specs->{parent}, $self->associate->id)->contents;
578 0 $rs = $parent->metafields->search({ 'metafield.shop_id' => $self->associate->id() });
579 }
580 else {
581 0 $rs = $self->{_db}->resultset(transform_name($package))->search({ 'me.shop_id' => $self->associate()->id() });
582 0 ▶ if ($package->get_through_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->get_through_parent) 0 0 0
583 0 $rs = $rs->search({ 'me.' . $self->{_mapper}->transform_package($package)->parent_variable => $specs->{parent}->id });
584 }
585 }
586 0 $rs = $self->generate_conditions($package, $specs, $rs);
587
588 0 ▶ print STDERR Dumper([$rs->as_query]) if $ENV{'SHOPIFY_LOG'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} 0 0 0
589 0 return $rs->count;
590 }
591  
592 sub get {
593 0 ▶ my ($self, $package, $id, $specs) = @_;
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
594 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
595 0 $self->check_increment_calls;
596 0 $package = $self->translate_model($package);
597 0 $self->validate_item($package);
598
599 0 $self->resolve_trailing_url($package, "get", $specs->{parent});
600  
601 0 my $rs = $self->{_db}->resultset(transform_name($package))->search({ 'me.shop_id' => $self->associate()->id(), ('me.' . $package->identifier) => $id });
602 0 ▶ print STDERR Dumper([$rs->as_query]) if $ENV{'SHOPIFY_LOG'};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} 0 0 0
603 0 my $row = $rs->first;
604 0 ▶ return undef unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
605 0 my $obj = $self->{_mapper}->to_shopify($row, $self);
606 0 ▶ $obj->associate($self) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 0 0 0
607 0 return $self->filter_gettable($obj);
608 }
609  
610 # Fields that should be filled in by 'shopify'.
611 sub post_creation_fields {
612 0 ▶ my ($self, $item) = @_;
Subroutine post_creation_fields: not called CC=5 SCAR=34.0
Pod undocumented
613 0 ▶ if (ref($item) =~ m/ApplicationCharge/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($item) =~ /ApplicationCharge/) { } 0 0 0
elsif (ref($item) =~ /Discount/) { } 0 0 0
614 0 $item->status("pending");
615 0 $item->confirmation_url("/mock/charge_activation/" . $item->id);
616 }
617 elsif (ref($item) =~ m/Discount/) {
618 0 $item->status("enabled");
619 }
620 0 ▶ $item->created_at(DateTime->now) if ($item->has_column('created_at'));
Branch: 1 branch
BranchTrueFalseTotal
if $item->has_column("created_at") 0 0 0
621 0 ▶ $item->updated_at(DateTime->now) if ($item->has_column('updated_at'));
Branch: 1 branch
BranchTrueFalseTotal
if $item->has_column("updated_at") 0 0 0
622 }
623  
624 sub create {
625 0 ▶ my ($self, $item, $options) = @_;
Subroutine create: not called CC=11 SCAR=48.8
Pod documented
626 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
627 0 $self->check_increment_calls;
628
629 # Helps to have a little delay from time to time.
630 0 sleep(int(rand()*3));
631  
632 0 $self->validate_item(ref($item));
633 0 my $package = ref($item);
634 0 my @missing = grep { !exists $item->{$_} } $item->creation_minimal;
635 0 ▶ die "Missing minimal creation member(s) " . join(", ", @missing) . "." if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 0 0
636 0 ▶ die new WWW::Shopify::Exception(ref($item) . " requires you to login with an admin account.") if $item->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $item->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$item->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$item->needs_login and not $self->logged_in_admin
$item->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $item->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
637  
638 0 $self->resolve_trailing_url($item, "create", $item->associated_parent);
639  
640 0 my $dbixgroup = $self->{_mapper}->from_shopify($self->{_db}, $item);
641  
642 sub fill_creation {
643 0 ▶ my $self = shift;
Subroutine fill_creation: not called CC=11 SCAR=48.8
Pod undocumented
644 0 my %chosen_ids = ();
645 0 foreach my $item (@_) {
646 0 my $fields = $item->represents->fields;
647 0 my @fillable_on_creation = $item->represents->creation_filled;
648 0 for (@fillable_on_creation) {
649 # This will automatically be filled by add_to_ accessors.
650 0 ▶ die new WWW::Shopify::Exception("Unable to generate field $_ in $item; doens't exist in fields.") unless exists $fields->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields->{$_} 0 0 0
651 0 ▶ next if $fields->{$_}->is_relation() && $fields->{$_}->is_parent();
Branch: 1 branch
BranchTrueFalseTotal
if $fields->{$_}->is_relation and $fields->{$_}->is_parent 0 0 0
Condition0%0/3
$fields->{$_}->is_relation and $fields->{$_}->is_parent
!ll&&!rl&&r
000
MC/DC0%0/2
$fields->{$_}->is_relation and $fields->{$_}->is_parent
$fields->{$_}->is_relation $fields->{$_}->is_parent
Truth table0%0/3
A and B
A= $fields->{$_}->is_relationB= $fields->{$_}->is_parent
ABresult
0X0
100
111
652 0 ▶ die new WWW::Shopify::Exception("Unable to generate field $_ in $item; can't generate relations.") if $fields->{$_}->is_relation();
Branch: 1 branch
BranchTrueFalseTotal
if $fields->{$_}->is_relation 0 0 0
653 0 $item->$_($fields->{$_}->generate());
654 }
655 0 ▶ if (exists $fields->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields->{"id"}) 0 0 0
656 0 while (1) {
657 0 my $id = int(rand(10000000));
658 0 $item->id($id);
659 0 my $test = $self->{_db}->resultset(transform_name($item->represents))->find($id);
660 0 ▶ last unless defined $test && !exists $chosen_ids{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test and not exists $chosen_ids{$id} 0 0 0
Condition0%0/3
defined $test and not exists $chosen_ids{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $test and not exists $chosen_ids{$id}
defined $test not exists $chosen_ids{$id}
Truth table0%0/3
A and not(B)
A= defined $testB= not exists $chosen_ids{$id}
ABresult
0X0
100
111
661 }
662 0 $chosen_ids{$item->id} = 1;
663 }
664 0 ▶ $item->shop_id($self->associate->id) if $item->can('shop_id');
Branch: 1 branch
BranchTrueFalseTotal
if $item->can("shop_id") 0 0 0
665 0 $self->post_creation_fields($item);
666 }
667 }
668 0 fill_creation($self, $dbixgroup->nested);
669  
670 my %validities = (
671 ▶ 'WWW::Shopify::Model::Order::Fulfillment' => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
672 # my ($item) = @_;
673 # my $order = $item->parent;
674 # my @fulfillments = $order->fulfillments->all;
675 # if ($order->
676 }
677 0 );
678 #my $represents = $dbixgroup->contents->represents;
679 #my $sub = $validities{$represents}($dbixgroup->contents) if $validites{$represents};
680  
681 0 my $return;
682 0 eval {
683 0 ▶ if ($item->associated_parent && ref($item) =~ m/Metafield/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->associated_parent and ref($item) =~ /Metafield/) { } 0 0 0
Condition0%0/3
$item->associated_parent and ref($item) =~ /Metafield/
!ll&&!rl&&r
000
MC/DC0%0/2
$item->associated_parent and ref($item) =~ /Metafield/
$item->associated_parent ref($item) =~ /Metafield/
Truth table0%0/3
A and B
A= $item->associated_parentB= ref($item) =~ /Metafield/
ABresult
0X0
100
111
684 0 my $dbname = ref($item->associated_parent);
685 0 $dbname =~ s/WWW::Shopify:://;
686 0 my $accessor = "add_to_" . $item->plural;
687
688 0 my $parent = $self->{_db}->resultset($dbname)->find($item->associated_parent->id);
689 0 ▶ die "Requires parent in database." unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
690 0 my $json = $item->to_json;
691 0 $json->{shop_id} = $self->associate->id;
692 0 $parent->$accessor($json);
693 }
694 else {
695 0 $dbixgroup->insert;
696 }
697 0 $return = $self->{_mapper}->to_shopify($dbixgroup->contents, $self);
698 0 $return->associate($self);
699 };
700 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
701 0 die new WWW::Shopify::Exception(new HTTP::Response(422, "$@"))
702 }
703 0 ▶ $self->callback->webhook($self->associate, $item, "create") if $self->callback && $item->throws_create_webhooks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback and $item->throws_create_webhooks 0 0 0
Condition0%0/3
$self->callback and $item->throws_create_webhooks
!ll&&!rl&&r
000
MC/DC0%0/2
$self->callback and $item->throws_create_webhooks
$self->callback $item->throws_create_webhooks
Truth table0%0/3
A and B
A= $self->callbackB= $item->throws_create_webhooks
ABresult
0X0
100
111
704 0 return $return;
705 }
706  
707 sub update {
708 0 ▶ my ($self, $item) = @_;
Subroutine update: not called CC=7 SCAR=40.3
Pod documented
709 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
710 0 ▶ die new WWW::Shopify::Exception(ref($item) . " requires you to login with an admin account.") if $item->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $item->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$item->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$item->needs_login and not $self->logged_in_admin
$item->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $item->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
711 0 $self->validate_item(ref($item));
712 0 $self->check_increment_calls;
713  
714 0 $self->resolve_trailing_url($item, "update", $item->associated_parent);
715  
716 0 my $dbixgroup = $self->{_mapper}->from_shopify($self->{_db}, $item);
717 0 ▶ $dbixgroup->contents->updated_at(DateTime->now) if $item->fields->{updated_at};
Branch: 1 branch
BranchTrueFalseTotal
if $item->fields->{"updated_at"} 0 0 0
718 0 $dbixgroup->update;
719 0 my $obj = $self->{_mapper}->to_shopify($dbixgroup->contents, $self);
720 0 $obj->associate($self);
721 0 ▶ $self->callback->webhook($self->associate, $item, "update") if $self->callback && $item->throws_update_webhooks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback and $item->throws_update_webhooks 0 0 0
Condition0%0/3
$self->callback and $item->throws_update_webhooks
!ll&&!rl&&r
000
MC/DC0%0/2
$self->callback and $item->throws_update_webhooks
$self->callback $item->throws_update_webhooks
Truth table0%0/3
A and B
A= $self->callbackB= $item->throws_update_webhooks
ABresult
0X0
100
111
722 0 return $obj;
723 }
724  
725 sub delete {
726 0 ▶ my ($self, $item) = @_;
Subroutine delete: not called CC=9 SCAR=45.0
Pod documented
727 0 ▶ die new WWW::Shopify::Exception("WWW::Shopify::Test object not associated with shop. Call associate.") unless $self->associate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
728 0 ▶ die new WWW::Shopify::Exception(ref($item) . " requires you to login with an admin account.") if $item->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $item->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$item->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$item->needs_login and not $self->logged_in_admin
$item->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $item->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
729 0 $self->validate_item(ref($item));
730 0 ▶ die new WWW::Shopify::Exception("Class in deletion must be not null, and must be a blessed reference to a model object: " . ref($item)) unless ref($item) =~ m/Model/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($item) =~ /Model/ 0 0 0
731 0 $self->check_increment_calls;
732  
733 0 $self->resolve_trailing_url($item, "delete", $item->associated_parent);
734  
735 0 my $dbixgroup = $self->{_mapper}->from_shopify($self->{_db}, $item);
736 0 ▶ die new WWW::Shopify::Exception($item->singular . " with id " . $item->id . " does not exist.") unless $dbixgroup->contents->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbixgroup->contents->in_storage 0 0 0
737  
738 0 ▶ if (ref($item) =~ m/ApplicationCharge/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($item) =~ /ApplicationCharge/) { } 0 0 0
739 0 $dbixgroup->contents->status('cancelled');
740 0 $dbixgroup->contents->update;
741 }
742 else {
743 0 $dbixgroup->delete;
744 }
745 0 ▶ $self->callback->webhook($self->associate, $item, "delete") if $self->callback && $item->throws_delete_webhooks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback and $item->throws_delete_webhooks 0 0 0
Condition0%0/3
$self->callback and $item->throws_delete_webhooks
!ll&&!rl&&r
000
MC/DC0%0/2
$self->callback and $item->throws_delete_webhooks
$self->callback $item->throws_delete_webhooks
Truth table0%0/3
A and B
A= $self->callbackB= $item->throws_delete_webhooks
ABresult
0X0
100
111
746  
747 0 return 1;
748 }
749  
750 # For orders, among others.
751 sub close {
752 0 ▶ my ($self, $class) = @_;
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
753 0 ▶ die new WWW::Shopify::Exception("Only orders can be closed.") unless defined $class && $class->can_close;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class->can_close 0 0 0
Condition0%0/3
defined $class and $class->can_close
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and $class->can_close
defined $class $class->can_close
Truth table0%0/3
A and B
A= defined $classB= $class->can_close
ABresult
0X0
100
111
754 0 $self->check_increment_calls;
755 0 $self->validate_item(ref($class));
756  
757 0 $self->resolve_trailing_url($class, "close", $class->associated_parent);
758  
759 0 my $object = $self->{_db}->resultset(transform_name(ref($class)))->find({ id => $class->id() });
760 0 ▶ die new WWW::Shopify::Exception("Unable to find orders with id: " . $class->id()) unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
761 0 ▶ die new WWW::Shopify::Exception("Unable to close " . $class->singular . " " . $class->id . "; is already closed!")
Branch: 1 branch
BranchTrueFalseTotal
if $object->closed_at 0 0 0
762 if $object->closed_at;
763 0 $object->closed_at(DateTime->now);
764 0 $object->update;
765  
766 0 my $obj = $self->{_mapper}->to_shopify($object, $self);
767 0 $obj->associate($self);
768 0 return $obj;
769 }
770  
771 # This function is solely for charges.
772 sub activate {
773 0 ▶ my ($self, $class) = @_;
Subroutine activate: not called CC=4 SCAR=30.0
Pod documented
774 0 ▶ die new WWW::Shopify::Exception("Only charges can be activated.") unless defined $class && $class->can_activate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class->can_activate 0 0 0
Condition0%0/3
defined $class and $class->can_activate
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and $class->can_activate
defined $class $class->can_activate
Truth table0%0/3
A and B
A= defined $classB= $class->can_activate
ABresult
0X0
100
111
775 0 $self->check_increment_calls;
776 0 $self->validate_item(ref($class));
777  
778 0 $self->resolve_trailing_url($class, "activate", $class->associated_parent);
779  
780 0 my $object = $self->{_db}->resultset(transform_name(ref($class)))->find({ id => $class->id() });
781 0 ▶ die new WWW::Shopify::Exception("Unable to find charge with id: " . $object->id()) unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
782 0 $object->status("active");
783 0 $object->update;
784  
785 0 my $obj = $self->{_mapper}->to_shopify($object, $self);
786 0 $obj->associate($self);
787 0 return $obj;
788 }
789  
790 sub disable {
791 0 ▶ my ($self, $class) = @_;
Subroutine disable: not called CC=6 SCAR=37.4
Pod documented
792 0 ▶ die new WWW::Shopify::Exception("You can only disable discount codes.") unless defined $class && $class->can_disable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class->can_disable 0 0 0
Condition0%0/3
defined $class and $class->can_disable
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and $class->can_disable
defined $class $class->can_disable
Truth table0%0/3
A and B
A= defined $classB= $class->can_disable
ABresult
0X0
100
111
793 0 ▶ die new WWW::Shopify::Exception(ref($class) . " requires you to login with an admin account.") if $class->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $class->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$class->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$class->needs_login and not $self->logged_in_admin
$class->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $class->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
794 0 $self->check_increment_calls;
795 0 $self->validate_item(ref($class));
796  
797 0 $self->resolve_trailing_url($class, "disable", $class->associated_parent);
798  
799 0 my $object = $self->{_db}->resultset(transform_name(ref($class)))->find({ id => $class->id });
800 0 ▶ die new WWW::Shopify::Exception("Unable to find discount code with id: " . $object->id) unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
801 0 $object->status("disabled");
802 0 $object->update;
803  
804 0 my $obj = $self->{_mapper}->to_shopify($object, $self);
805 0 $obj->associate($self);
806 0 return $obj;
807 }
808  
809 sub enable {
810 0 ▶ my ($self, $class) = @_;
Subroutine enable: not called CC=6 SCAR=37.4
Pod documented
811 0 ▶ die new WWW::Shopify::Exception("You can only enable discount codes.") unless defined $class && $class->can_enable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class->can_enable 0 0 0
Condition0%0/3
defined $class and $class->can_enable
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and $class->can_enable
defined $class $class->can_enable
Truth table0%0/3
A and B
A= defined $classB= $class->can_enable
ABresult
0X0
100
111
812 0 ▶ die new WWW::Shopify::Exception(ref($class) . " requires you to login with an admin account.") if $class->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $class->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$class->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$class->needs_login and not $self->logged_in_admin
$class->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $class->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
813 0 $self->check_increment_calls;
814 0 $self->validate_item(ref($class));
815  
816 0 $self->resolve_trailing_url($class, "enable", $class->associated_parent);
817  
818 0 my $object = $self->{_db}->resultset(transform_name(ref($class)))->find({ id => $class->id });
819 0 ▶ die new WWW::Shopify::Exception("Unable to find discount code with id: " . $object->id) unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
820 0 $object->status("enabled");
821 0 $object->update;
822  
823 0 my $obj = $self->{_mapper}->to_shopify($object, $self);
824 0 $obj->associate($self);
825 0 return $obj;
826 }
827  
828 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
829 sub login_admin {
830 0 ▶ my ($self, $username, $password) = @_;
Subroutine login_admin: not called CC=3 SCAR=24.8
Pod documented
831 0 ▶ return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000 0 0 0
Condition0%0/3
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
$self->{"last_login_check"} time - $self->{"last_login_check"} < 1000
Truth table0%0/3
A and B
A= $self->{"last_login_check"}B= time - $self->{"last_login_check"} < 1000
ABresult
0X0
100
111
832 0 $self->{last_login_check} = time;
833 0 $self->{authenticity_token} = md5_hex(rand());
834 0 return 1;
835 }
836  
837 sub logged_in_admin {
838 0 ▶ my ($self) = @_;
Subroutine logged_in_admin: not called CC=3 SCAR=24.8
Pod documented
839 0 ▶ return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000 0 0 0
Condition0%0/3
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
$self->{"last_login_check"} time - $self->{"last_login_check"} < 1000
Truth table0%0/3
A and B
A= $self->{"last_login_check"}B= time - $self->{"last_login_check"} < 1000
ABresult
0X0
100
111
840 0 $self->{last_login_check} = time;
841 0 return 1;
842 }
843  
844  
845 sub get_activation_link {
846 0 ▶ my ($self, $customer) = @_;
Subroutine get_activation_link: not called CC=4 SCAR=30.0
Pod undocumented
847 0 ▶ die new WWW::Shopify::Exception() unless $customer && $customer->isa('WWW::Shopify::Model::Customer');
Branch: 1 branch
BranchTrueFalseTotal
unless $customer and $customer->isa("WWW::Shopify::Model::Customer") 0 0 0
Condition0%0/3
$customer and $customer->isa("WWW::Shopify::Model::Customer")
!ll&&!rl&&r
000
MC/DC0%0/2
$customer and $customer->isa("WWW::Shopify::Model::Customer")
$customer $customer->isa("WWW::Shopify::Model::Customer")
Truth table0%0/3
A and B
A= $customerB= $customer->isa("WWW::Shopify::Model::Customer")
ABresult
0X0
100
111
848 0 ▶ die new WWW::Shopify::Exception("Getting an activation link requires you to login with an admin account.") if !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in_admin 0 0 0
849 0 return "http://" . $self->shop_url . "/account/activate/" . md5_hex($customer->id);
850 }
851  
852 sub get_reset_token {
853 0 ▶ my ($self, $customer) = @_;
Subroutine get_reset_token: not called CC=4 SCAR=30.0
Pod undocumented
854 0 ▶ die new WWW::Shopify::Exception() unless $customer && $customer->isa('WWW::Shopify::Model::Customer');
Branch: 1 branch
BranchTrueFalseTotal
unless $customer and $customer->isa("WWW::Shopify::Model::Customer") 0 0 0
Condition0%0/3
$customer and $customer->isa("WWW::Shopify::Model::Customer")
!ll&&!rl&&r
000
MC/DC0%0/2
$customer and $customer->isa("WWW::Shopify::Model::Customer")
$customer $customer->isa("WWW::Shopify::Model::Customer")
Truth table0%0/3
A and B
A= $customerB= $customer->isa("WWW::Shopify::Model::Customer")
ABresult
0X0
100
111
855 0 ▶ die new WWW::Shopify::Exception("Getting a reset token requires you to login with an admin account.") if !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in_admin 0 0 0
856 0 return md5_hex($customer->id);
857 }
858  
859  
860 =head2 authorize_url([scope], redirect)
861  
862 When the shop doesn't have an access_token, this is what you should be redirecting your client to. Inputs should be your scope, as an array, and the url you want to redirect to.
863  
864 =cut
865  
866 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
867 sub authorize_url {
868 0 ▶ my ($self, $scope, $redirect) = (@_);
Subroutine authorize_url: not called CC=1 SCAR=6.9
Pod documented
869 0 return "/mock/admin/oauth/authorize?redirect_uri=" . uri_escape($redirect) . "&shop=" . $self->associate->myshopify_domain . "&scope=" . uri_escape(join(",", @$scope));
870 }
871  
872 =head2 exchange_token(shared_secret, code)
873  
874 When you have a temporary code, which you should get from authorize_url's redirect and you want to exchange it for a token, you call this.
875  
876 =cut
877  
878 sub exchange_token {
879 0 ▶ my ($self, $shared_secret, $code) = (@_);
Subroutine exchange_token: not called CC=2 SCAR=17.9
Pod documented
880
881 0 ▶ die new WWW::Shopify::Exception("Unable to exchange tokens when shop is not associated.") unless $self->associate;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associate 0 0 0
882  
883 0 my $req = HTTP::Request->new(POST => "https://" . $self->shop_url . "/admin/oauth/access_token");
884 0 $req->content_type('application/x-www-form-urlencoded');
885 0 my %parameters = (
886 'client_id' => $self->api_key,
887 'client_secret' => $shared_secret,
888 'code' => $code
889 );
890 0 sleep 1;
891 0 return md5_hex($self->api_key . $self->shop_url);
892 }
893  
894 sub shop_url {
895 0 ▶ die new WWW::Shopify::Exception("Can't get a url, unless we're associated.") unless $_[0]->associate;
Subroutine shop_url: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->associate 0 0 0
896 0 return $_[0]->associate->myshopify_domain;
897 }
898 0 ▶sub api_key { return md5_hex(''); }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod undocumented
899  
900 =head1 SEE ALSO
901  
902 L<WWW::Shopify>
903  
904 =head1 AUTHOR
905  
906 Adam Harrison
907  
908 =head1 LICENSE
909  
910 See LICENSE in the main directory.
911  
912 =cut
913  
914  
915 1 1;
« blib/lib/WWW/Shopify/URLHandler.pm ↑ Summary blib/lib/WWW/Shopify/Model/Item.pm »