File Coverage

blib/lib/IOC/Visitor/SearchForService.pm
Criterion Covered Total %
statement 35 35 100.0
branch 10 10 100.0
condition 4 6 66.6
subroutine 9 9 100.0
pod 2 2 100.0
total 60 62 96.7


line stmt bran cond sub pod time code
1              
2             package IOC::Visitor::SearchForService;
3              
4 11     11   1986 use strict;
  11         23  
  11         501  
5 11     11   63 use warnings;
  11         21  
  11         563  
6              
7             our $VERSION = '0.03';
8              
9 11     11   63 use Scalar::Util qw(blessed);
  11         29  
  11         680  
10              
11 11     11   60 use IOC::Interfaces;
  11         19  
  11         242  
12 11     11   68 use IOC::Exceptions;
  11         40  
  11         246  
13              
14 11     11   440 use base 'IOC::Visitor';
  11         18  
  11         12028  
15              
16             sub new {
17 8     8 1 1684 my ($_class, $service_name) = @_;
18 8 100       27 ($service_name)
19             || throw IOC::InsufficientArguments "You must provide a name of a service to find";
20 7   33     42 my $class = ref($_class) || $_class;
21 7         21 my $visitor = {
22             service_to_find => $service_name
23             };
24 7         16 bless($visitor, $class);
25 7         29 return $visitor;
26             }
27              
28             sub visit {
29 10     10 1 3421 my ($self, $container) = @_;
30 10 100 100     89 (blessed($container) && $container->isa('IOC::Container'))
31             || throw IOC::InsufficientArguments "You must provide an IOC::Container object to search";
32 6         15 my $service_to_find = $self->{service_to_find};
33 6         14 return $self->_recursiveSearch($container, $service_to_find);
34             }
35              
36             sub _recursiveSearch {
37 22     22   33 my ($self, $container, $service_to_find) = @_;
38             # look through all the current services
39 22 100       59 return $container->get($service_to_find) if $container->hasService($service_to_find);
40             # if we have sub-containers, ...
41 21 100       58 if ($container->hasSubContainers()) {
42             # then loop through all the sub-containers
43 8         30 foreach my $sub_container ($container->getAllSubContainers()) {
44 16         35 my $service = $self->_recursiveSearch($sub_container, $service_to_find);
45 16 100       47 return $service if defined $service;
46             }
47             }
48 19         42 return undef;
49             }
50              
51             1;
52              
53             __END__