Board/Container.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 63.3% 19 / 30 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 79.4% 120 / 151 CC 25 SCAR 15.7
CC
25
Cov
82%
resolve
11.2
add_sub_container
4.6
add_type_mapping_for
4.6
CRAP 4.8
« Board.pm ↑ Summary Board/Service.pm »
1 package Bread::Board::Container;
2 61 our $AUTHORITY = 'cpan:STEVAN';
3 # ABSTRACT: A container for services and other containers
4 61 $Bread::Board::Container::VERSION = '0.37';
5 61 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use Moose::Util::TypeConstraints 'find_type_constraint';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 61 ▶use MooseX::Params::Validate 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 61 ▶use Bread::Board::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 61 with 'Bread::Board::Traversable';
12  
13 61 has 'name' => (
14 is => 'rw',
15 isa => 'Str',
16 required => 1
17 );
18  
19 has 'services' => (
20 traits => [ 'Hash', 'Clone' ],
21 is => 'rw',
22 isa => 'Bread::Board::Container::ServiceList',
23 coerce => 1,
24 lazy => 1,
25 96 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 51 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
27 51 my $self = shift;
28 51 $_->parent($self) foreach values %{$self->services};
29 },
30 61 handles => {
31 'get_service' => 'get',
32 'has_service' => 'exists',
33 'get_service_list' => 'keys',
34 'has_services' => 'count',
35 }
36 );
37  
38 has 'sub_containers' => (
39 traits => [ 'Hash', 'Clone' ],
40 is => 'rw',
41 isa => 'Bread::Board::Container::SubContainerList',
42 coerce => 1,
43 lazy => 1,
44 105 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 23 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 23 my $self = shift;
47 23 $_->parent($self) foreach values %{$self->sub_containers};
48 },
49 61 handles => {
50 'get_sub_container' => 'get',
51 'has_sub_container' => 'exists',
52 'get_sub_container_list' => 'keys',
53 'has_sub_containers' => 'count',
54 }
55 );
56  
57 has 'type_mappings' => (
58 traits => [ 'Hash' ],
59 is => 'rw',
60 isa => 'Bread::Board::Container::ServiceList',
61 lazy => 1,
62 19 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 61 handles => {
64 '_get_type_mapping_for' => 'get',
65 '_has_type_mapping_for' => 'exists',
66 '_mapped_types' => 'keys',
67 }
68 );
69  
70 49 ▶sub get_type_mapping_for {
Subroutine get_type_mapping_for: called CC=4 SCAR=14.2
Pod documented
71 49 my $self = shift;
72 49 my ($type) = @_;
73  
74 49 ▶ return $self->_get_type_mapping_for($type)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_type_mapping_for($type) 48 1 49
75 if $self->_has_type_mapping_for($type);
76  
77 1 for my $possible ($self->_mapped_types) {
78 1 ▶ return $self->_get_type_mapping_for($possible)
Branch: 1 branch
BranchTrueFalseTotal
if $possible->isa($type) 1 0 1
79 if $possible->isa($type);
80 }
81  
82 0 return;
83 }
84  
85 65 ▶sub has_type_mapping_for {
Subroutine has_type_mapping_for: called CC=4 SCAR=13.9
Pod documented
86 65 my $self = shift;
87 65 my ($type) = @_;
88  
89 65 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_type_mapping_for($type) 45 20 65
90 if $self->_has_type_mapping_for($type);
91  
92 20 for my $possible ($self->_mapped_types) {
93 26 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $possible->isa($type) 1 25 26
94 if $possible->isa($type);
95 }
96  
97 19 return;
98 }
99  
100 227 ▶sub add_service {
Subroutine add_service: called CC=3 SCAR=12.0
Pod documented
101 227 my ($self, $service) = @_;
102 227 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess("You must pass in a Bread::Board::Service instance, not $service") 227 0 227
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
00227
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
103 || confess "You must pass in a Bread::Board::Service instance, not $service";
104 227 $service->parent($self);
105 227 $self->services->{$service->name} = $service;
106 }
107  
108 33 ▶sub add_sub_container {
Subroutine add_sub_container: called CC=4 SCAR=15.2
Pod documented
109 33 my ($self, $container) = @_;
110 (
111 33 ▶ blessed $container &&
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized") or confess("You must pass in a Bread::Board::Container instance, not $container") 33 0 33
Condition50%3/6
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized")
!ll&&!rl&&r
0033
$container->isa("Bread::Board::Container") or $container->isa("Bread::Board::Container::Parameterized")
l!l&&r!l&&!r
2940
MC/DC0%0/3
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized")
builtin::blessed($container) $container->isa("Bread::Board::Container") $container->isa("Bread::Board::Container::Parameterized")
Truth table50%2/4
A and B or C
A= builtin::blessed($container)B= $container->isa("Bread::Board::Container")C= $container->isa("Bread::Board::Container::Parameterized")
ABCresult
0XX0
1000
11X1
1011
112 (
113 $container->isa('Bread::Board::Container')
114 ||
115 $container->isa('Bread::Board::Container::Parameterized')
116 )
117 ) || confess "You must pass in a Bread::Board::Container instance, not $container";
118 33 $container->parent($self);
119 33 $self->sub_containers->{$container->name} = $container;
120 }
121  
122 29 ▶sub add_type_mapping_for {
Subroutine add_type_mapping_for: called CC=4 SCAR=15.2
Pod documented
123 29 my ($self, $type, $service) = @_;
124  
125 29 my $type_constraint = find_type_constraint( $type );
126  
127 29 ▶ (defined $type_constraint)
Branch: 1 branch
BranchTrueFalseTotal
defined $type_constraint or confess("You must pass a valid Moose type, and it must exist already") 29 0 29
128 || confess "You must pass a valid Moose type, and it must exist already";
129  
130 29 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess("You must pass in a Bread::Board::Service instance, not $service") 29 0 29
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
0029
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
131 || confess "You must pass in a Bread::Board::Service instance, not $service";
132  
133 29 $self->type_mappings->{ $type_constraint->name } = $service;
134 }
135  
136 135 ▶sub resolve {
Subroutine resolve: called CC=9 SCAR=24.1
Pod documented
137 135 my ($self, %params) = validated_hash(\@_,
138 service => { isa => 'Str', optional => 1 },
139 type => { isa => 'Str', optional => 1 },
140 parameters => { isa => 'HashRef', optional => 1 },
141 );
142  
143 my %parameters = exists $params{'parameters'}
144 135 ▶ ? %{ $params{'parameters'} }
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'parameters'} ? : 24 111 135
145 : ();
146  
147 135 ▶ if (my $service_path = $params{'service'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $service_path = $params{'service'}) { } 113 22 135
elsif (my $type = $params{'type'}) { } 22 0 22
148 113 my $service = $self->fetch( $service_path );
149 # NOTE:
150 # we might want to allow Bread::Board::Service::Deferred::Thunk
151 # objects as well, but I am not sure that is a valid use case
152 # for this, so for now we just don't go there.
153 # - SL
154 109 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess('You can only resolve services, ' . (defined $service ? $service : "undef") . ' is not a Bread::Board::Service') 109 0 109
defined $service ? : 0 0 0
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
00109
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
155 || confess "You can only resolve services, "
156 . (defined $service ? $service : 'undef')
157 . " is not a Bread::Board::Service";
158 109 return $service->get( %parameters );
159 }
160 elsif (my $type = $params{'type'}) {
161  
162 22 ▶ ($self->has_type_mapping_for( $type ))
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_mapping_for($type) or confess("Could not find a mapped service for type ($type)") 22 0 22
163 || confess "Could not find a mapped service for type ($type)";
164  
165 22 my $service = $self->get_type_mapping_for( $type );
166 22 my $result = $service->get( %parameters );
167  
168 22 ▶ (find_type_constraint( $type )->check( $result ))
Branch: 1 branch
BranchTrueFalseTotal
find_type_constraint($type)->check($result) or confess("The result of the service for type ($type) did not" . " pass the type constraint with $result") 22 0 22
169 || confess "The result of the service for type ($type) did not"
170 . " pass the type constraint with $result";
171  
172 22 return $result;
173 }
174 else {
175 0 confess "Cannot call resolve without telling it what to resolve.";
176 }
177  
178 }
179  
180 61 __PACKAGE__->meta->make_immutable;
181  
182 61 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 61 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 61 1;
186  
187 __END__
« Board.pm ↑ Summary Board/Service.pm »