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

filter: Statementstmt 16.4% 41 / 250 Branchbran 0.7% 1 / 138 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 30.7% 12 / 39 Podpod 0.0% 0 / 20 totaltotal 9.8% 54 / 550 CC 101 SCAR 58.5
CC
101
Cov
9%
generate_dbix
870.0
from_shopify
702.0
__ANON__
182.0
CRAP 348.7
↑ 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 0 ▶ my $package = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
44 0 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
int @_ > 0 ? : 0 0 0
45 namespace => int(@_) > 0 ? [@_] : undef,
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 1 unless defined $_[0]->{namespace}; return defined first { $_ eq $_[1] } @{$_[0]->{namespace}} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine in_namespace: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"namespace"} 0 0 0
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 174 174
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 0 ▶ my $package = ref($_[0]) ? ref($_[0]) : $_[0];
Subroutine has_shop_field: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
153 0 ▶ $package = $package->represents if $package =~ m/DBIx/;
Branch: 1 branch
BranchTrueFalseTotal
if $package =~ /DBIx/ 0 0 0
154 0 ▶ return !$package->is_shop && (!$package->is_nested || $package =~ m/Address/);
Condition0%0/6
!$package->is_shop && (!$package->is_nested || $package =~ /Address/)
!ll&&!rl&&r
000
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 table0%0/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=29 SCAR=67.7
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 0 ▶ push(@ids, $parent_variable) if $needs_adding && !$fields->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $needs_adding and not $fields->{"id"} 0 0 0
Condition0%0/3
$needs_adding and not $fields->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$needs_adding and not $fields->{"id"}
$needs_adding not $fields->{"id"}
Truth table0%0/3
A and not(B)
A= $needs_addingB= not $fields->{"id"}
ABresult
0X0
100
111
175 }
176 # All simple columns.
177 0 foreach my $field_name (grep { !$fields->{$_}->is_relation } keys(%$fields)) {
178 0 my $field = $fields->{$field_name};
179 0 my %attributes = ();
180 0 $attributes{'data_type'} = $field->sql_type;
181 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
182 0 push(@columns, "\"$field_name\", { " . join(", ", map { "$_ => '" . uc($attributes{$_}) . "'" } keys(%attributes)) . " }");
183
184 }
185 # If we don't have an ID give us one, so that all DB stuff can have primary keys.
186 0 ▶ if (!$fields->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields->{"id"}) 0 0 0
187 0 push(@columns, "\"id\", { data_type => 'BIGINT', is_nullable => 0, is_auto_increment => 1 }");
188 }
189 # All relationship columns that are belong to.
190 # ReferenceOne / Non-Nested / Interior : Belongs To
191 # ReferenceOne / Non-Nested / Exterior : Belongs To
192 # Parent : Belongs To
193 # OwnOne / Non-Nested / Interior : Belongs To
194 # OwnOne / Nested / Exterior : Belong To
195  
196 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
197  
198 0 my @relationships = ();
199 0 foreach my $field_name (grep { $fields->{$_}->is_db_belongs_to } @field_relations) {
200 0 my $field = $fields->{$field_name};
201 0 ▶ die $field_name unless $field->relation;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->relation 0 0 0
202 0 my %attributes = ();
203 0 my $accessor_name;
204 0 my $mod_field_name = $field_name;
205 0 ▶ if ($field_name =~ m/_id$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_name =~ /_id$/) { } 0 0 0
206 0 $accessor_name = $`;
207 }
208 else {
209 0 $accessor_name = $field_name;
210 0 $mod_field_name = $field_name . "_id";
211 }
212 0 $attributes{'data_type'} = $field->sql_type;
213 # Geenrally make non-parent fields nullable.
214 0 ▶ $attributes{'is_nullable'} = 1 unless $field->is_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_parent 0 0 0
215 0 push(@columns, "\"$mod_field_name\", { " . join(", ", map { "$_ => '" . uc($attributes{$_}) . "'" } keys(%attributes)) . " }");
216 0 ▶ push(@relationships, "__PACKAGE__->belongs_to($accessor_name => '" . $self->transform_package($field->relation) . "', '$mod_field_name'" . ($attributes{'is_nullable'} ? ", { join_type => 'left' }" : "") . ");");
Branch: 1 branch
BranchTrueFalseTotal
$attributes{'is_nullable'} ? : 0 0 0
217 }
218 # Many / Nested / Interior : Has Many
219 0 foreach my $field_name (grep { $fields->{$_}->is_db_has_many } @field_relations) {
220 0 my $field = $fields->{$field_name};
221 0 my ($parent_var) = $self->get_parent_column_name($field->relation);
222 0 push(@relationships, "__PACKAGE__->has_many($field_name => '" . $self->transform_package($field->relation) . "', '" . $parent_var . "');");
223 }
224 # OwnOne / Nested / Interior : Has One
225 0 foreach my $field_name (grep { $fields->{$_}->is_db_has_one } @field_relations) {
226 0 my $field = $fields->{$field_name};
227 0 my $parent_variable = $self->transform_package($field->relation)->parent_variable;
228 0 push(@relationships, "__PACKAGE__->has_one($field_name => '" . $self->transform_package($field->relation) . "', '$parent_variable');");
229 }
230 # OwnOne / Non-Nested / Exterior : Many-Many
231 # Many / Nested : Many-Many
232 # Many / Non-Nested : Many-Many
233 0 foreach my $field_name (grep { $fields->{$_}->is_db_many_many } @field_relations) {
234 0 my $field = $fields->{$field_name};
235 0 my $joining_name = $self->joining_class_name($package, $field->relation);
236 0 my $accessor_name = $field_name . "_hasmany";
237 0 $self->generate_dbix_join($package, $field->relation);
238 0 push(@relationships, "__PACKAGE__->has_many($accessor_name => '" . $joining_name . "', '" . $package->singular . "_id');");
239 0 push(@relationships, "__PACKAGE__->many_to_many($field_name => '$accessor_name', '" . $field->relation->singular . "');");
240 }
241  
242 0 my @shop_relations = ();
243 0 ▶ if ($package->is_shop) {
Branch: 2 branches
BranchTrueFalseTotal
if ($package->is_shop) { } 0 0 0
elsif (has_shop_field($package)) { } 0 0 0
244 # Get a list somewhere of all the top-level stuff.
245 0 @shop_relations = map { "__PACKAGE__->has_many(" . $_->plural . " => '" . $self->transform_package($_) . "', 'shop_id');" }
246 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
247 }
248 elsif (has_shop_field($package)) {
249 0 push(@columns, "\"shop_id\", { data_type => \"BIGINT\" }");
250 0 push(@shop_relations, "__PACKAGE__->belongs_to(shop => '" . $self->package_prefix . "::Model::Shop', 'shop_id');");
251 }
252  
253 0 my @unique_keys = $package->unique_fields;
254  
255 $self->{classes}->{$self->transform_package($package)} = "
256 #!/usr/bin/perl
257 use strict;
258 use warnings;
259 # This class is generated from DBIx.pm. Do not modify.
260 package " . $self->transform_package($package) . ";
261 use base qw/DBIx::Class::Core/;
262  
263 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
264 " . ($has_date ? "__PACKAGE__->load_components(qw/InflateColumn::DateTime/);" : "") . "
265 __PACKAGE__->table('" . $self->table_prefix . $table_name . "');
266 __PACKAGE__->add_columns(
267 " . join(",\n\t", @columns) . "
268 );
269 0 ▶__PACKAGE__->set_primary_key(" . join(", ", map { "'$_'" } (0 ? @ids : "id")) . ");
Branch: 1 branch
BranchTrueFalseTotal
int @unique_keys > 0 ? : 0 0 0
270  
271 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
272  
273 " . join("\n", @shop_relations) . "
274  
275 " . join("\n", @relationships) . "
276 sub represents { return '" . $package . "'; }
277 sub parent_variable { return " . ($parent_variable ? "'$parent_variable'" : "undef") . "; }
278 " . $self->arbitrary_sql($package) . "
279 1;";
280 }
281  
282  
283 1 ▶use JSON qw(encode_json from_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
284 1 ▶use WWW::Shopify::Common::DBIxGroup;
Subroutine BEGIN: called CC=1 SCAR=0.0
285 # Takes in a schema and a shopify object and maps it to a DBIx existence.
286 sub from_shopify {
287 my $internal_from = sub {
288 0 ▶ my ($self, $schema, $type, $data, $shop_id) = @_;
Subroutine __ANON__: not called CC=12 SCAR=50.5
289 # If we have a class relationship.
290 0 ▶ if ($type->is_relation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_relation) 0 0 0
291 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
292 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
293 0 ▶ return [] unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
294 0 my $array = [map { $self->from_shopify($schema, $_, $shop_id); } @$data];
295 0 return $array;
296 }
297 elsif ($type->is_own()) {
298 0 ▶ return {} unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
299 0 return $self->from_shopify($data);
300 }
301 ▶ 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
302 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
303 0 return $type->from_shopify($data);
304 }
305 }
306 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
307 0 return $type->from_shopify($data);
308 0 ▶ };
Subroutine from_shopify: not called CC=26 SCAR=65.5
Pod undocumented
309  
310 0 my ($self, $schema, $shopifyObject, $shop_id) = @_;
311 0 ▶ return undef unless $shopifyObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $shopifyObject 0 0 0
312 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
313 0 my $dbPackage = $self->transform_package(ref($shopifyObject));
314 0 my $dbObject = undef;
315 0 my %identifiers = map { $_ => $shopifyObject->$_ } $shopifyObject->identifier;
316 0 ▶ if ($shopifyObject->is_nested && $dbPackage->parent_variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shopifyObject->is_nested and $dbPackage->parent_variable) 0 0 0
Condition0%0/3
$shopifyObject->is_nested and $dbPackage->parent_variable
!ll&&!rl&&r
000
MC/DC0%0/2
$shopifyObject->is_nested and $dbPackage->parent_variable
$shopifyObject->is_nested $dbPackage->parent_variable
Truth table0%0/3
A and B
A= $shopifyObject->is_nestedB= $dbPackage->parent_variable
ABresult
0X0
100
111
317 0 ▶ die new WWW::Shopify::Exception("Invalid nested object passed into to_shopify.") unless $shopifyObject->associated_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $shopifyObject->associated_parent 0 0 0
318 0 ▶ $identifiers{$dbPackage->parent_variable} = $shopifyObject->associated_parent->id if $shopifyObject->associated_parent->can('id');
Branch: 1 branch
BranchTrueFalseTotal
if $shopifyObject->associated_parent->can("id") 0 0 0
319 }
320 0 ▶ $dbObject = $schema->resultset($dbPackage)->find(\%identifiers) if $shopifyObject;
Branch: 1 branch
BranchTrueFalseTotal
if $shopifyObject 0 0 0
321 0 ▶ $dbObject = $schema->resultset($dbPackage)->new({}) unless $dbObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbObject 0 0 0
322 0 my $fields = $shopifyObject->fields();
323 0 my $group = WWW::Shopify::Common::DBIxGroup->new(contents => $dbObject);
324
325 # Anything that's many-many like metafields shouldn't set parent variables on themselves. Or not.
326 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
327 0 my $parent_variable = $dbObject->parent_variable;
328 0 ▶ $dbObject->$parent_variable($shopifyObject->associated_parent->id) if $shopifyObject->associated_parent->can('id');
Branch: 1 branch
BranchTrueFalseTotal
if $shopifyObject->associated_parent->can("id") 0 0 0
329 }
330 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
331 0 $dbObject->shop_id($shop_id);
332 }
333 0 ▶ if ($shopifyObject->is_shop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shopifyObject->is_shop) 0 0 0
334 0 $shop_id = $dbObject->id;
335 }
336  
337 0 foreach my $key (keys(%$fields)) {
338 0 ▶ next if $key =~ m/metafields/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /metafields/ 0 0 0
339 0 my $data = $shopifyObject->$key();
340 0 ▶ if ($fields->{$key}->is_relation && $fields->{$key}->is_many()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$key}->is_relation and $fields->{$key}->is_many) 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
341 0 $_->associated_parent($shopifyObject) for (@$data);
342 }
343 0 my $db_value = &$internal_from($self, $schema, $fields->{$key}, $data, $shop_id);
344 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 and not $fields->{$key}->is_parent) { } 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
345 0 $group->add_children(grep { defined $_ } @$db_value);
346 }
347 ▶ elsif (!$fields->{$key}->is_relation || ($fields->{$key}->is_reference && !$fields->{$key}->is_parent)) {
Condition0%0/6
not $fields->{$key}->is_relation or $fields->{$key}->is_reference and not $fields->{$key}->is_parent
l!l&&r!l&&!r
000
$fields->{$key}->is_reference and not $fields->{$key}->is_parent
!ll&&!rl&&r
000
MC/DC0%0/3
not $fields->{$key}->is_relation or $fields->{$key}->is_reference and not $fields->{$key}->is_parent
not $fields->{$key}->is_relation $fields->{$key}->is_reference not $fields->{$key}->is_parent
Truth table0%0/4
not(A) or B and not(C)
A= not $fields->{$key}->is_relationB= $fields->{$key}->is_referenceC= not $fields->{$key}->is_parent
ABCresult
1XX1
0111
00X0
0100
348 0 $dbObject->$key($db_value);
349 }
350 }
351 0 return $group;
352 }
353  
354 sub to_shopify {
355 my $internal_to = sub {
356 0 ▶ my ($self, $type, $data, $shopifyObject, $test) = @_;
Subroutine __ANON__: not called CC=13 SCAR=52.0
357 # If we have a class relationship.
358 0 ▶ if ($type->is_relation()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_relation) 0 0 0
359 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
360 0 ▶ return undef if $type->relation =~ m/Metafield/;
Branch: 1 branch
BranchTrueFalseTotal
if $type->relation =~ /Metafield/ 0 0 0
361 0 ▶ return [] unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
362 0 my $array = [map { my $object = $self->to_shopify($_, $test); $object->associated_parent($shopifyObject); $object } $data->all()];
363 0 return $array;
364 }
365 elsif ($type->is_own()) {
366 0 ▶ return {} unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
367 0 my $object = $self->to_shopify($data, $test);
368 0 $object->associated_parent($shopifyObject);
369 0 return $object;
370 }
371 elsif ($type->is_reference()) {
372 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
373 0 return $type->to_shopify($data, $test);
374 }
375 }
376 # This seems confusing, but due to us storing our stuff in the database as Shopify stuff
377 # We're transferring TYPE from shopify, but SELF to shopify.
378 0 ▶ return $type->from_shopify($data) if ref($type) =~ m/timezone/i;
Branch: 1 branch
BranchTrueFalseTotal
if ref($type) =~ /timezone/i 0 0 0
379 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
380 0 return $data;
381 0 ▶ };
Subroutine to_shopify: not called CC=7 SCAR=40.3
Pod undocumented
382  
383 0 my ($self, $dbObject, $test) = @_;
384 0 ▶ return undef unless $dbObject;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbObject 0 0 0
385 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
386  
387 0 my $shopifyObject = $dbObject->represents()->new;
388 0 my $fields = $shopifyObject->fields();
389 0 foreach my $key (keys(%$fields)) {
390 # Easiest way, AFAICT to work around this: http://lists.scsys.co.uk/pipermail/dbix-class/2009-December/008687.html
391 # DBIx strangeness.
392 0 my $data;
393 0 ▶ if ($dbObject->can($key . "_id")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbObject->can($key . '_id')) { } 0 0 0
394 0 my $method = $key . "_id";
395 0 ▶ $data = $dbObject->$key if ($dbObject->$method);
Branch: 1 branch
BranchTrueFalseTotal
if $dbObject->$method 0 0 0
396 } else {
397 0 $data = $dbObject->$key;
398 }
399 0 ▶ $shopifyObject->{$key} = &$internal_to($self, $fields->{$key}, $data, $shopifyObject, $test) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
400 }
401 0 $shopifyObject->associate($test);
402 0 return $shopifyObject;
403 }
404  
405 =head1 SEE ALSO
406  
407 L<WWW::Shopify>
408  
409 =head1 AUTHOR
410  
411 Adam Harrison
412  
413 =head1 LICENSE
414  
415 See LICENSE in the main directory.
416  
417 =cut
418  
419 1 1
↑ Summary blib/lib/WWW/Shopify/URLHandler.pm »