| 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 | package WWW::Shopify::Common::DBIxGroup; | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | my $package = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | my %hash = @_; | |||||||||||||||||||||||||||||||||||||||||
| 11 | bless { | ||||||||||||||||||||||||||||||||||||||||||
| 12 | parent => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | contents => $hash{'contents'} ? $hash{'contents'} : undef, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | children => [] | ||||||||||||||||||||||||||||||||||||||||||
| 15 | }, $package; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub children { return @{$_[0]->{children}}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
children: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub contents { return $_[0]->{contents}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
contents: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub parent { $_[0]->{parent} = $_[1] if $_[1]; return $_[0]->{parent}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parent: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub members { return ($_[0]->contents, $_[0]->children); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
members: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | sub add_children { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my ($self, @children) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_children: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | @children = @{$children[0]} if int(@children) == 1 && ref($children[0]) eq "ARRAY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
int @children == 1 and ref $children[0] eq "ARRAY"
MC/DC0%0/2
int @children == 1 and ref $children[0] eq "ARRAY"
int @children == 1
ref $children[0] eq "ARRAY"
Truth table0%0/3
A and B
A= int @children == 1B= ref $children[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | foreach my $child (@children) { | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | die new WWW::Shopify::Exception("Must be a DBIxGroup: " . ref($child)) unless $child && ref($child) && ref($child) =~ m/DBIxGroup/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$child and ref $child and ref($child) =~ /DBIxGroup/
$child and ref $child
MC/DC0%0/3
$child and ref $child and ref($child) =~ /DBIxGroup/
$child
ref $child
ref($child) =~ /DBIxGroup/
Truth table0%0/4
A and B and C
A= $childB= ref $childC= ref($child) =~ /DBIxGroup/
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | $child->parent($self); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | push(@{$self->{children}}, $child); | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | sub update { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $_->update_or_insert for ($self->children); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $self->contents->update; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | sub delete { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $_->delete for ($self->children); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $self->contents->delete; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | sub update_or_insert { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_or_insert: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($self->parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $relationship = "add_to_" . $self->contents->represents->plural; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $columns = {$self->contents->get_columns}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (!$self->contents->in_storage) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->parent->contents->$relationship($columns); | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $self->contents->update; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $self->contents->update_or_insert; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $_->update_or_insert for ($self->children); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | sub insert { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($self->parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $relationship = "add_to_" . $self->contents->represents->plural; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $parent_variable = $self->contents->parent_variable; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $self->contents->$parent_variable($self->parent->contents->id); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $columns = {$self->contents->get_columns}; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $self->parent->contents->$relationship($columns); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $self->contents->insert; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $_->insert for ($self->children); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Ensure that we have no unecessary children present. | ||||||||||||||||||||||||||||||||||||||||||
| 75 | sub ensure_synchronicity { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ensure_synchronicity: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $package = $self->contents->represents; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $fields = $package->fields; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | foreach my $field (grep { $_->is_relation && ($_->is_many || $_->is_own) && $_->relation !~ m/Metafield/ } values(%$fields)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$_->is_relation and $_->is_many || $_->is_own
$_->is_many or $_->is_own
MC/DC0%0/3
$_->is_relation and $_->is_many || $_->is_own
$_->is_relation
$_->is_many
$_->is_own
Truth table0%0/4
A and B or C
A= $_->is_relationB= $_->is_manyC= $_->is_own
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $name = $field->name; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my @children = grep { $_->contents->represents eq $field->relation } $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 82 | # In case we're not actually keeping certain elements in sync. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my @ids = $field->relation->identifier; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (int(@ids) == 1 && $ids[0] eq "id") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
int @ids == 1 and $ids[0] eq "id"
MC/DC0%0/2
int @ids == 1 and $ids[0] eq "id"
int @ids == 1
$ids[0] eq "id"
Truth table0%0/3
A and B
A= int @ids == 1B= $ids[0] eq "id"
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->contents->$name->search({ id => { -not_in => [map { $_->contents->id } @children] } })->delete; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my @db_children = $self->contents->$name->all; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | for my $db_child (@db_children) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $exists = 0; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | for my $pot_child (@children) { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $exists = 1 if int(grep { $db_child->$_ eq $pot_child->contents->$_ } (@ids)) == int(@ids); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $db_child->delete if !$exists; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub nested { my ($self) = @_; return ($self->contents, map { $_->nested } $self->children); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nested: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||