blib/lib/WWW/Shopify/Common/DBIx.pm

filter: Statementstmt 19.1% 46 / 240 Branchbran 2.5% 3 / 118 Conditioncond 1.7% 1 / 57 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 35.0% 14 / 40 Podpod 0.0% 0 / 20 totaltotal 12.6% 64 / 507 CC 87 SCAR 54.8
CC
87
Cov
12%
generate_dbix
702.0
from_shopify
380.0
__ANON__
182.0
CRAP 238.9
↑ Summary blib/lib/WWW/Shopify/URLHandler.pm »
1 #!/usr/bin/perl
2  
3  
4 =head1 NAME
5  
6 DBIx - Main interface to the object-relational mapper; maps shopify objects to database objects, and back again.
7  
8 =cut
9  
10 =head1 DESCRIPTION
11  
12 DBIx represents a way to grab and upload data to the database.
13  
14 =cut
15  
16 =head1 EXAMPLES
17  
18 To give an idea of how you're supposed to use this object, look at the following example, which builds off the example in L<WWW::Shopify>: here we get all the products, and then insert them into the database.
19  
20 my $SA = new WWW::Shopify::Public($ShopURL, $APIKey, $AccessToken);
21 my $DBIX = new WWW::Shopify::Common::DBIx();
22 my @products = $SA->get_all('Product');
23 for (@products) {
24 my $product = $DBIX->from_shopify($_);
25 $product->insert;
26 }
27  
28 This doesn't check for duplicates or anything else, but it's easy enough to check for that; see the DBIx documentation.
29  
30 =cut
31  
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use WWW::Shopify;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 package WWW::Shopify::Common::DBIx;
38  
39 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 our @EXPORT_OK = qw(transform_package strip_head);
41  
42 sub new {
43 1 ▶ my $package = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
44 1 return bless {
45 namespace => [@_],
46 classes => {},
47 package_prefix => 'WWW::Shopify::Model::DBIx::Schema::Result',
48 table_prefix => 'shopify_'
49 }, $package;
50 };
51 0 ▶sub package_prefix { $_[0]->{package_prefix} = $_[1] if defined $_[1]; return $_[0]->{package_prefix}; }
Subroutine package_prefix: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
52 0 ▶sub table_prefix { $_[0]->{table_prefix} = $_[1] if defined $_[1]; return $_[0]->{table_prefix}; }
Subroutine table_prefix: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
53 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 0 ▶sub in_namespace { return defined first { $_ eq $_[1] } @{$_[0]->{namespace}} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine in_namespace: not called CC=1 SCAR=6.9
Pod undocumented
55  
56 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58  
59 my %arbitrary_sql = (
60 "WWW::Shopify::Model::Product" => sub {
61 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 return "__PACKAGE__->has_many('collects', '" . $self->package_prefix . "::Model::CustomCollection::Collect', 'product_id');\n";
63 }
64 1 );
65  
66 sub arbitrary_sql {
67 0 ▶ my ($self, $package) = @_;
Subroutine arbitrary_sql: not called CC=2 SCAR=17.9
Pod undocumented
68 0 ▶ return "" unless exists $arbitrary_sql{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arbitrary_sql{$package} 0 0 0
69 0 $arbitrary_sql{$package}->($self);
70 }
71  
72 sub all_classes {
73 0 ▶ return grep { $_ !~ m/DBIx/ } findallmod WWW::Shopify::Model;
Subroutine all_classes: not called CC=1 SCAR=6.9
Pod undocumented
74 }
75  
76 sub class {
77 0 ▶ my ($self, $class) = @_;
Subroutine class: not called CC=1 SCAR=6.9
Pod undocumented
78 0 return $self->{classes}->{$class};
79 }
80 sub classes {
81 0 ▶ my ($self) = @_;
Subroutine classes: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return values(%{$self->{classes}});
83 }
84 sub class_names {
85 0 ▶ my ($self) = @_;
Subroutine class_names: not called CC=1 SCAR=6.9
Pod undocumented
86 0 return keys(%{$self->{classes}});
87 }
88  
89 sub generate_dbix_all {
90 0 ▶ my ($self) = @_;
Subroutine generate_dbix_all: not called CC=2 SCAR=17.9
Pod undocumented
91 0 $self->generate_dbix($_) for (@{$self->{namespace}});
92 }
93 0 ▶sub strip_head { die unless $_[0] =~ m/^WWW::Shopify::/; return $'; }
Subroutine strip_head: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^WWW::Shopify::/ 0 0 0
94  
95 sub transform_package {
96 0 ▶ return $_[0]->package_prefix . "::" . strip_head($_[1]) if ref($_[0]) && ref($_[0]) eq __PACKAGE__;
Subroutine transform_package: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and ref $_[0] eq __PACKAGE__ 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq __PACKAGE__
ref $_[0] ref $_[0] eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq __PACKAGE__
ABresult
0X0
100
111
97 0 return "WWW::Shopify::Model::DBIx::Schema::Result::" . strip_head($_[0]);
98 }
99  
100 0 ▶sub joining_table_name { my $self = shift; return join("", map { $_->plural } sort(@_)); }
Subroutine joining_table_name: not called CC=1 SCAR=6.9
Pod undocumented
101 0 ▶sub joining_class_name { my $self = shift; return $self->package_prefix . "::Model::" . join("", map { $_ =~ m/\:\:(\w+)$/; $1; } sort(@_)); }
Subroutine joining_class_name: not called CC=1 SCAR=6.9
Pod undocumented
102  
103 sub generate_dbix_join {
104 0 ▶ my ($self, $join1, $join2) = @_;
Subroutine generate_dbix_join: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my $name = $self->joining_class_name($join1, $join2);
106 0 $self->{classes}->{$name} = "
107 #!/usr/bin/perl
108 use strict;
109 use warnings;
110 # This class is generated from DBIx.pm. Do not modify.
111  
112 package $name;
113 use base qw/DBIx::Class::Core/;
114  
115 __PACKAGE__->table('" . $self->table_prefix . $self->joining_table_name($join1, $join2) . "');
116 __PACKAGE__->add_columns(
117 'id', { data_type => 'INT', is_nullable => '0', is_auto_increment => 1 },
118 '" . $join1->singular . "_id', { data_type => '" . WWW::Shopify::Field::Identifier->sql_type . "', is_nullable => 0 },
119 '" . $join2->singular . "_id', { data_type => '" . WWW::Shopify::Field::Identifier->sql_type . "', is_nullable => 0 }
120 );
121 __PACKAGE__->set_primary_key('id');
122 __PACKAGE__->belongs_to(" . $join1->singular . " => '" . $self->transform_package($join1) . "', '" . $join1->singular . "_id');
123 __PACKAGE__->belongs_to(" . $join2->singular . " => '" . $self->transform_package($join2) . "', '" . $join2->singular . "_id');
124  
125 1;";
126 }
127  
128 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 1 ▶BEGIN { foreach my $package (findallmod WWW::Shopify::Model) { $package =~ s/::/\//g; eval { require $package . '.pm' }; print STDERR $@ if $@; } }
Subroutine BEGIN: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 170 170
130 # Essentially an internal method.
131 # Generates a DBIx schema from the specified package.
132 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 sub get_parent_column_name {
135 0 ▶ my ($self, $package) = @_;
Subroutine get_parent_column_name: not called CC=3 SCAR=24.8
Pod undocumented
136  
137 0 my $fields = $package->fields;
138 0 my $parent_variable;
139 0 ▶ if (my $field = first { $_->is_relation && $_->is_parent } values(%$fields)) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 3 branches
BranchTrueFalseTotal
if (my $field = &first(sub { $_->is_relation and $_->is_parent; } , values %$fields)) { } 0 0 0
elsif ($fields->{'parent_id'}) { } 0 0 0
$_->is_relation and $_->is_parent 0 0 0
140 0 return $field->name;
141 }
142 elsif ($fields->{parent_id}) {
143 0 return 'parent_id';
144 }
145 else {
146 0 return ($package->parent->singular . '_id', !exists $fields->{$package->parent->singular . "_id"});
147 }
148 0 return undef;
149 }
150  
151 sub has_shop_field {
152 1 ▶ my $package = ref($_[0]) ? ref($_[0]) : $_[0];
Subroutine has_shop_field: called CC=5 SCAR=21.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
153 1 ▶ $package = $package->represents if $package =~ m/DBIx/;
Branch: 1 branch
BranchTrueFalseTotal
if $package =~ /DBIx/ 1 0 1
154 1 ▶ return !$package->is_shop && (!$package->is_nested || $package =~ m/Address/);
Condition16%1/6
!$package->is_shop && (!$package->is_nested || $package =~ /Address/)
!ll&&!rl&&r
100
not $package->is_nested or $package =~ /Address/
l!l&&r!l&&!r
000
MC/DC0%0/3
!$package->is_shop && (!$package->is_nested || $package =~ /Address/)
!$package->is_shop not $package->is_nested $package =~ /Address/
Truth table25%1/4
not(A) && not(B) or C
A= !$package->is_shopB= not $package->is_nestedC= $package =~ /Address/
ABCresult
0XX0
1000
11X1
1011
155 }
156  
157 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
158 sub generate_dbix {
159 0 ▶ my ($self, $package) = @_;
Subroutine generate_dbix: not called CC=26 SCAR=65.5
Pod undocumented
160  
161 0 my $fields = $package->fields;
162 0 my @ids = $package->identifier;
163 0 ▶ my $has_date = (defined first { $fields->{$_}->sql_type eq "DATETIME" } keys(%$fields));
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 my $parent_variable = undef;
165 0 my $table_name = $package->plural;
166 0 ▶ $table_name = $package->parent->plural . "_" . $table_name if $package->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $package->parent 0 0 0
167  
168 0 my @columns = ();
169 # If we're a nested item, and we don't have something called either parent_id or <parent->singular>_id, or somethign marked a relation parent, create one, 'cause we're expecting it.
170 0 ▶ if ($package->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->parent) 0 0 0
171 0 my $needs_adding;
172 0 ($parent_variable, $needs_adding) = $self->get_parent_column_name($package);
173 0 ▶ push(@columns, "\"$parent_variable\", { data_type => '" . WWW::Shopify::Field::Identifier->sql_type . "' }") if $needs_adding;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_adding 0 0 0
174 }
175 # All simple columns.
176 0 foreach my $field_name (grep { !$fields->{$_}->is_relation } keys(%$fields)) {
177 0 my $field = $fields->{$field_name};
178 0 my %attributes = ();
179 0 $attributes{'data_type'} = $field->sql_type;
180 0 ▶ $attributes{'is_nullable'} = ((!first { $field_name eq $_ } $package->identifier) && (!$package->is_nested || !$package->parent || $parent_variable ne $field_name)) ? 1 : 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!&first(sub { $field_name eq $_; } , $package->identifier) && (!($package->is_nested && $package->parent) || $parent_variable ne $field_name) ? : 0 0 0
Condition0%0/9
not &first(sub { and !($package->is_nested && $package->parent) || $parent_variable ne $field_name
!ll&&!rl&&r
000
not $package->is_nested && $package->parent or $parent_variable ne $field_name
l!l&&r!l&&!r
000
$package->is_nested && $package->parent
!ll&&!rl&&r
000
MC/DC0%0/4
not &first(sub { $field_name eq $_; } , $package->identifier) and !($package->is_nested && $package->parent) || $parent_variable ne $field_name
not &first(sub { $field_name eq $_; } , $package->identifier) $package->is_nested $package->parent $parent_variable ne $field_name
Truth table0%0/5
not(A) and not(B && C) or D
A= not &first(sub { $field_name eq $_; } , $package->identifier)B= $package->is_nestedC= $package->parentD= $parent_variable ne $field_name
ABCDresult
0XXX0
11100
10XX1
110X1
11111
181 0 push(@columns, "\"$field_name\", { " . join(", ", map { "$_ => '" . uc($attributes{$_}) . "'" } keys(%attributes)) . " }");
182
183 }
184 # If we don't have an ID give us one, so that all DB stuff can have primary keys.
185 0 ▶ if (!$fields->{$ids[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$fields{$ids[0]}) 0 0 0
186 0 push(@columns, "\"" . $ids[0] . "\", { data_type => 'BIGINT', is_nullable => 0, is_auto_increment => 1 }");
187 }
188 # All relationship columns that are belong to.
189 # ReferenceOne / Non-Nested / Interior : Belongs To
190 # ReferenceOne / Non-Nested / Exterior : Belongs To
191 # Parent : Belongs To
192 # OwnOne / Non-Nested / Interior : Belongs To
193 # OwnOne / Nested / Exterior : Belong To
194  
195 0 ▶ my @field_relations = grep { $fields->{$_}->is_relation && $self->in_namespace($fields->{$_}->relation) } keys(%$fields);
Branch: 1 branch
BranchTrueFalseTotal
$fields->{$_}->is_relation and $self->in_namespace($fields->{$_}->relation) 0 0 0
196  
197 0 my @relationships = ();
198 0 foreach my $field_name (grep { $fields->{$_}->is_db_belongs_to } @field_relations) {
199 0 my $field = $fields->{$field_name};
200 0 ▶ die $field_name unless $field->relation;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->relation 0 0 0
201 0 my %attributes = ();
202 0 my $accessor_name;
203 0 my $mod_field_name = $field_name;
204 0 ▶ if ($field_name =~ m/_id$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_name =~ /_id$/) { } 0 0 0
205 0 $accessor_name = $`;
206 }
207 else {
208 0 $accessor_name = $field_name;
209 0 $mod_field_name = $field_name . "_id";
210 }
211 0 $attributes{'data_type'} = $field->sql_type;
212 # Geenrally make non-parent fields nullable.
213 0 ▶ $attributes{'is_nullable'} = 1 unless $field->is_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_parent 0 0 0
214 0 push(@columns, "\"$mod_field_name\", { " . join(", ", map { "$_ => '" . uc($attributes{$_}) . "'" } keys(%attributes)) . " }");
215 0 push(@relationships, "__PACKAGE__->belongs_to($accessor_name => '" . $self->transform_package($field->relation) . "', '$mod_field_name');");
216 }
217 # Many / Nested / Interior : Has Many
218 0 foreach my $field_name (grep { $fields->{$_}->is_db_has_many } @field_relations) {
219 0 my $field = $fields->{$field_name};
220 0 my ($parent_var) = $self->get_parent_column_name($field->relation);
221 0 push(@relationships, "__PACKAGE__->has_many($field_name => '" . $self->transform_package($field->relation) . "', '" . $parent_var . "');");
222 }
223 # OwnOne / Nested / Interior : Has One
224 0 foreach my $field_name (grep { $fields->{$_}->is_db_has_one } @field_relations) {
225 0 my $field = $fields->{$field_name};
226 0 my $parent_variable = $self->transform_package($field->relation)->parent_variable;
227 0 push(@relationships, "__PACKAGE__->has_one($field_name => '" . $self->transform_package($field->relation) . "', '$parent_variable');");
228 }
229 # OwnOne / Non-Nested / Exterior : Many-Many
230 # Many / Nested : Many-Many
231 # Many / Non-Nested : Many-Many
232 0 foreach my $field_name (grep { $fields->{$_}->is_db_many_many } @field_relations) {
233 0 my $field = $fields->{$field_name};
234 0 my $joining_name = $self->joining_class_name($package, $field->relation);
235 0 my $accessor_name = $field_name . "_hasmany";
236 0 $self->generate_dbix_join($package, $field->relation);
237 0 push(@relationships, "__PACKAGE__->has_many($accessor_name => '" . $joining_name . "', '" . $package->singular . "_id');");
238 0 push(@relationships, "__PACKAGE__->many_to_many($field_name => '$accessor_name', '" . $field->relation->singular . "');");
239 }
240  
241 0 my @shop_relations = ();
242 0 ▶ if ($package->is_shop) {
Branch: 2 branches
BranchTrueFalseTotal
if ($package->is_shop) { } 0 0 0
elsif (has_shop_field($package)) { } 0 0 0
243 # Get a list somewhere of all the top-level stuff.
244 0 @shop_relations = map { "__PACKAGE__->has_many(" . $_->plural . " => '" . $self->transform_package($_) . "', 'shop_id');" }
245 0 ▶ grep { $_ =~ m/Address/i || (!$_->is_nested && !$_->is_shop && $_ !~ m/metafield/i) } @{$self->{namespace}};
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /Address/i or not $_->is_nested || $_->is_shop || $_ =~ /metafield/i 0 0 0
Condition0%0/9
$_ =~ /Address/i || !($_->is_nested || $_->is_shop || $_ =~ /metafield/i)
l!l&&r!l&&!r
000
$_->is_nested || $_->is_shop || $_ =~ /metafield/i
l!l&&r!l&&!r
000
$_->is_nested or $_->is_shop
l!l&&r!l&&!r
000
MC/DC0%0/4
$_ =~ /Address/i || !($_->is_nested || $_->is_shop || $_ =~ /metafield/i)
$_ =~ /Address/i $_->is_nested $_->is_shop $_ =~ /metafield/i
Truth table0%0/5
A || not(B or C || D)
A= $_ =~ /Address/iB= $_->is_nestedC= $_->is_shopD= $_ =~ /metafield/i
ABCDresult
1XXX1
00001
01XX0
001X0
00010
246 }
247 elsif (has_shop_field($package)) {
248 0 push(@columns, "\"shop_id\", { data_type => \"BIGINT\" }");
249 0 push(@shop_relations, "__PACKAGE__->belongs_to(shop => '" . $self->package_prefix . "::Model::Shop', 'shop_id');");
250 }
251  
252 0 my @unique_keys = $package->unique_fields;
253  
254 $self->{classes}->{$self->transform_package($package)} = "
255 #!/usr/bin/perl
256 use strict;
257 use warnings;
258 # This class is generated from DBIx.pm. Do not modify.
259 package " . $self->transform_package($package) . ";
260 use base qw/DBIx::Class::Core/;
261  
262 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
263 " . ($has_date ? "__PACKAGE__->load_components(qw/InflateColumn::DateTime/);" : "") . "
264 __PACKAGE__->table('" . $self->table_prefix . $table_name . "');
265 __PACKAGE__->add_columns(
266 " . join(",\n\t", @columns) . "
267 );
268 0 ▶__PACKAGE__->set_primary_key(" . join(", ", map { "'$_'" } @ids) . ");
Branch: 1 branch
BranchTrueFalseTotal
int @unique_keys > 0 ? : 0 0 0
269  
270 0 ▶" . ((int(@unique_keys) > 0) ? "__PACKAGE__->add_unique_constraint(constraint_name => [ " . join(" ", map { "\"$_\"" } @unique_keys) . " ]);" : "") . "
Branch: 2 branches
BranchTrueFalseTotal
$has_date ? : 0 0 0
$parent_variable ? : 0 0 0
271  
272 " . join("\n", @shop_relations) . "
273  
274 " . join("\n", @relationships) . "
275 sub represents { return '" . $package . "'; }
276 sub parent_variable { return " . ($parent_variable ? "'$parent_variable'" : "undef") . "; }
277 " . $self->arbitrary_sql($package) . "
278 1;";
279 }
280  
281  
282 1 ▶use JSON qw(encode_json from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
283 1 ▶use WWW::Shopify::Common::DBIxGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 # Takes in a schema and a shopify object and maps it to a DBIx existence.
285 sub from_shopify {
286 my $internal_from = sub {
287 0 ▶ my ($self, $schema, $type, $data, $shop_id) = @_;
Subroutine __ANON__: not called CC=12 SCAR=50.5
288 # If we have a class relationship.
289 0 ▶ if ($type->is_relation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_relation) 0 0 0
290 0 ▶ return undef if $type->relation && !$self->in_namespace($type->relation);
Branch: 1 branch
BranchTrueFalseTotal
if $type->relation and not $self->in_namespace($type->relation) 0 0 0
Condition0%0/3
$type->relation and not $self->in_namespace($type->relation)
!ll&&!rl&&r
000
MC/DC0%0/2
$type->relation and not $self->in_namespace($type->relation)
$type->relation not $self->in_namespace($type->relation)
Truth table0%0/3
A and not(B)
A= $type->relationB= not $self->in_namespace($type->relation)
ABresult
0X0
100
111
291 0 ▶ if ($type->is_many()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type->is_many) { } 0 0 0
elsif ($type->is_own) { } 0 0 0
elsif ($type->is_reference and $type->is_one) { } 0 0 0
292 0 ▶ return [] unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
293 0 my $array = [map { $self->from_shopify($schema, $_, $shop_id); } @$data];
294 0 return $array;
295 }
296 elsif ($type->is_own()) {
297 0 ▶ return {} unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
298 0 return $self->from_shopify($data);
299 }
300 ▶ elsif ($type->is_reference() && $type->is_one()) {
Condition0%0/3
$type->is_reference and $type->is_one
!ll&&!rl&&r
000
MC/DC0%0/2
$type->is_reference and $type->is_one
$type->is_reference $type->is_one
Truth table0%0/3
A and B
A= $type->is_referenceB= $type->is_one
ABresult
0X0
100
111
301 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
302 0 return $type->from_shopify($data);
303 }
304 }
305 0 ▶ return encode_json($type) if ($type eq 'WWW::Shopify::Field::Freeform');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "WWW::Shopify::Field::Freeform" 0 0 0
306 0 return $type->from_shopify($data);
307 0 ▶ };
Subroutine from_shopify: not called CC=19 SCAR=59.4
Pod undocumented
308  
309 0 my ($self, $schema, $shopifyObject, $shop_id) = @_;
310 0 ▶ return undef unless $shopifyObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $shopifyObject 0 0 0
311 0 ▶ die new WWW::Shopify::Exception('Invalid object passed into to_shopify: ' . ref($shopifyObject) . '.') unless ref($shopifyObject) =~ m/Model::/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($shopifyObject) =~ /Model::/ 0 0 0
312 0 my $dbPackage = $self->transform_package(ref($shopifyObject));
313 0 my $dbObject = undef;
314 0 ▶ $dbObject = $schema->resultset($dbPackage)->find({map { $_ => $shopifyObject->$_ } $shopifyObject->identifier}) if $shopifyObject && (!first { !$shopifyObject->{$_} } $shopifyObject->identifier);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $shopifyObject and not &first(sub { not $shopifyObject->{$_}; } , $shopifyObject->identifier) 0 0 0
Condition0%0/3
$shopifyObject and not &first(sub {
!ll&&!rl&&r
000
MC/DC0%0/2
$shopifyObject and not &first(sub { not $shopifyObject->{$_}; } , $shopifyObject->identifier)
$shopifyObject not &first(sub { not $shopifyObject->{$_}; } , $shopifyObject->identifier)
Truth table0%0/3
A and not(B)
A= $shopifyObjectB= not &first(sub { not $shopifyObject->{$_}; } , $shopifyObject->identifier)
ABresult
0X0
100
111
315 0 ▶ $dbObject = $schema->resultset($dbPackage)->new({}) unless $dbObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbObject 0 0 0
316 0 my $fields = $shopifyObject->fields();
317 0 my $group = WWW::Shopify::Common::DBIxGroup->new(contents => $dbObject);
318
319 # Anything that's many-many like metafields shouldn't set parent variables on themselves. Or not.
320 0 ▶ if ($shopifyObject->associated_parent && ref($shopifyObject) !~ m/Metafield$/ && $dbObject->parent_variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shopifyObject->associated_parent and not ref($shopifyObject) =~ /Metafield$/ and $dbObject->parent_variable) 0 0 0
Condition0%0/6
$shopifyObject->associated_parent and not ref($shopifyObject) =~ /Metafield$/ and $dbObject->parent_variable
!ll&&!rl&&r
000
$shopifyObject->associated_parent and not ref($shopifyObject) =~ /Metafield$/
!ll&&!rl&&r
000
MC/DC0%0/3
$shopifyObject->associated_parent and not ref($shopifyObject) =~ /Metafield$/ and $dbObject->parent_variable
$shopifyObject->associated_parent not ref($shopifyObject) =~ /Metafield$/ $dbObject->parent_variable
Truth table0%0/4
A and not(B) and C
A= $shopifyObject->associated_parentB= not ref($shopifyObject) =~ /Metafield$/C= $dbObject->parent_variable
ABCresult
0XX0
10X0
1100
1111
321 0 my $parent_variable = $dbObject->parent_variable;
322 0 $dbObject->$parent_variable($shopifyObject->associated_parent->id);
323 }
324 0 ▶ if (has_shop_field($shopifyObject) && $shop_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_shop_field($shopifyObject) and $shop_id) 0 0 0
Condition0%0/3
has_shop_field($shopifyObject) and $shop_id
!ll&&!rl&&r
000
MC/DC0%0/2
has_shop_field($shopifyObject) and $shop_id
has_shop_field($shopifyObject) $shop_id
Truth table0%0/3
A and B
A= has_shop_field($shopifyObject)B= $shop_id
ABresult
0X0
100
111
325 0 $dbObject->shop_id($shop_id);
326 }
327 0 ▶ if ($shopifyObject->is_shop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shopifyObject->is_shop) 0 0 0
328 0 $shop_id = $dbObject->id;
329 }
330  
331 0 foreach my $key (keys(%$fields)) {
332 0 ▶ next if $key =~ m/metafields/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /metafields/ 0 0 0
333 0 my $data = $shopifyObject->$key();
334 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
335 0 my $db_value = &$internal_from($self, $schema, $fields->{$key}, $data, $shop_id);
336 0 ▶ if ($fields->{$key}->is_relation && $fields->{$key}->is_many()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fields->{$key}->is_relation and $fields->{$key}->is_many) { } 0 0 0
elsif (not $fields->{$key}->is_relation or $fields->{$key}->is_reference) { } 0 0 0
Condition0%0/3
$fields->{$key}->is_relation and $fields->{$key}->is_many
!ll&&!rl&&r
000
MC/DC0%0/2
$fields->{$key}->is_relation and $fields->{$key}->is_many
$fields->{$key}->is_relation $fields->{$key}->is_many
Truth table0%0/3
A and B
A= $fields->{$key}->is_relationB= $fields->{$key}->is_many
ABresult
0X0
100
111
337 0 $group->add_children(grep { defined $_ } @$db_value);
338 }
339 ▶ elsif (!$fields->{$key}->is_relation || $fields->{$key}->is_reference) {
Condition0%0/3
not $fields->{$key}->is_relation or $fields->{$key}->is_reference
l!l&&r!l&&!r
000
MC/DC0%0/2
not $fields->{$key}->is_relation or $fields->{$key}->is_reference
not $fields->{$key}->is_relation $fields->{$key}->is_reference
Truth table0%0/3
not(A) or B
A= not $fields->{$key}->is_relationB= $fields->{$key}->is_reference
ABresult
1X1
011
000
340 0 $dbObject->$key($db_value);
341 }
342 }
343 }
344 0 return $group;
345 }
346  
347 sub to_shopify {
348 my $internal_to = sub {
349 0 ▶ my ($self, $type, $data, $shopifyObject, $test) = @_;
Subroutine __ANON__: not called CC=13 SCAR=52.0
350 # If we have a class relationship.
351 0 ▶ if ($type->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_relation) 0 0 0
352 0 ▶ if ($type->is_db_has_many || $type->is_db_many_many) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type->is_db_has_many or $type->is_db_many_many) { } 0 0 0
elsif ($type->is_own) { } 0 0 0
elsif ($type->is_reference) { } 0 0 0
Condition0%0/3
$type->is_db_has_many or $type->is_db_many_many
l!l&&r!l&&!r
000
MC/DC0%0/2
$type->is_db_has_many or $type->is_db_many_many
$type->is_db_has_many $type->is_db_many_many
Truth table0%0/3
A or B
A= $type->is_db_has_manyB= $type->is_db_many_many
ABresult
1X1
011
000
353 0 ▶ return undef if $type->relation =~ m/Metafield/;
Branch: 1 branch
BranchTrueFalseTotal
if $type->relation =~ /Metafield/ 0 0 0
354 0 ▶ return [] unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
355 0 my $array = [map { my $object = $self->to_shopify($_, $test); $object->associated_parent($shopifyObject); $object } $data->all()];
356 0 return $array;
357 }
358 elsif ($type->is_own()) {
359 0 ▶ return {} unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
360 0 my $object = $self->to_shopify($data, $test);
361 0 $object->associated_parent($shopifyObject);
362 0 return $object;
363 }
364 elsif ($type->is_reference()) {
365 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
366 0 return $type->to_shopify($data, $test);
367 }
368 }
369 # This seems confusing, but due to us storing our stuff in the database as Shopify stuff
370 # We're transferring TYPE from shopify, but SELF to shopify.
371 0 ▶ return $type->from_shopify($data) if ref($type) =~ m/timezone/i;
Branch: 1 branch
BranchTrueFalseTotal
if ref($type) =~ /timezone/i 0 0 0
372 0 ▶ return from_json($data) if ($data && ref($type) eq 'WWW::Shopify::Field::Freeform');
Branch: 1 branch
BranchTrueFalseTotal
if $data and ref $type eq "WWW::Shopify::Field::Freeform" 0 0 0
Condition0%0/3
$data and ref $type eq "WWW::Shopify::Field::Freeform"
!ll&&!rl&&r
000
MC/DC0%0/2
$data and ref $type eq "WWW::Shopify::Field::Freeform"
$data ref $type eq "WWW::Shopify::Field::Freeform"
Truth table0%0/3
A and B
A= $dataB= ref $type eq "WWW::Shopify::Field::Freeform"
ABresult
0X0
100
111
373 0 return $data;
374 0 ▶ };
Subroutine to_shopify: not called CC=5 SCAR=34.0
Pod undocumented
375  
376 0 my ($self, $dbObject, $test) = @_;
377 0 ▶ return undef unless $dbObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbObject 0 0 0
378 0 ▶ die new WWW::Shopify::Exception('Invalid object passed into to_shopify: ' . ref($dbObject) . '.') unless ref($dbObject) =~ m/Model::/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($dbObject) =~ /Model::/ 0 0 0
379  
380 0 my $shopifyObject = $dbObject->represents()->new;
381 0 my $fields = $shopifyObject->fields();
382 0 foreach my $key (keys(%$fields)) {
383 0 my $data = $dbObject->$key();
384 0 ▶ $shopifyObject->{$key} = &$internal_to($self, $fields->{$key}, $data, $shopifyObject, $test) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
385 }
386 0 $shopifyObject->associate($test);
387 0 return $shopifyObject;
388 }
389  
390 =head1 SEE ALSO
391  
392 L<WWW::Shopify>
393  
394 =head1 AUTHOR
395  
396 Adam Harrison
397  
398 =head1 LICENSE
399  
400 See LICENSE in the main directory.
401  
402 =cut
403  
404 1 1
↑ Summary blib/lib/WWW/Shopify/URLHandler.pm »