File Coverage

lib/MooseX/DIC/ServiceRegistry.pm
Criterion Covered Total %
statement 35 37 94.5
branch 8 14 57.1
condition 4 5 80.0
subroutine 5 5 100.0
pod 0 4 0.0
total 52 65 80.0


line stmt bran cond sub pod time code
1             package MooseX::DIC::ServiceRegistry;
2              
3 4     4   1858 use Moose;
  4         7  
  4         24  
4             with 'MooseX::DIC::Loggable';
5              
6             # InterfaceName -> EnvironmentName => ServiceMetadata
7             has metadata => ( is => 'ro', isa => 'HashRef[HashRef[MooseX::DIC::Configuration::ServiceMetadata]]', default => sub { {} } );
8              
9              
10             # (interface_name: Str) -> Bool
11             sub has_service {
12 6     6 0 22 my ($self,$interface_name) = @_;
13              
14 6         165 return exists($self->metadata->{$interface_name});
15             }
16              
17             # (service_metadata: ServiceMetadata) -> Void
18             sub add_service_definition {
19 17     17 0 46 my ($self,$service_metadata) = @_;
20              
21 17         581 my $interface_name = $service_metadata->implements;
22 17         530 my $service_name = $service_metadata->class_name;
23 17         513 my $environment = $service_metadata->environment;
24              
25 17         430 $self->logger->debug("Registering service $service_name for interface $interface_name ");
26            
27 17 100       592 if( exists $self->metadata->{$interface_name} ) {
28 1         23 my $interface_definition = $self->metadata->{$interface_name};
29 1 50       3 if(exists $interface_definition->{$environment}){
30 0 0       0 if($interface_definition->{$environment}->class_name eq $service_name){
31 0         0 $self->logger->warn("A definition was already found for $interface_name for the environment $environment, overwritting it");
32             }
33             }
34 1         3 $interface_definition->{$environment} = $service_metadata;
35             } else {
36 16         399 $self->metadata->{$interface_name}->{$environment} = $service_metadata;
37             }
38              
39 17         412 $self->logger->info("$service_name was registered as an implementation of $interface_name for environment $environment");
40             }
41              
42             # (interface_name:Str[,environment:Str='default']) -> service:ServiceMetadata
43             sub get_service_definition {
44 33     33 0 86 my ($self,$interface_name,$original_environment) = @_;
45 33   100     94 my $environment = $original_environment || 'default';
46              
47 33         845 $self->logger->trace("An implementation for $interface_name was requested");
48 33         293 my $service_definition;
49            
50 33 100       907 if( exists $self->metadata->{$interface_name} ) {
51 32         824 $service_definition = $self->metadata->{$interface_name}->{$environment};
52             # always try the default environment if the specified one didn't exist
53 32 50 66     84 if(not(defined($service_definition)) and not($environment eq 'default')){
54 1         24 $self->logger->trace("An implementation for $interface_name was not found in environment $environment, searching in default namespace");
55 1         33 $service_definition = $self->metadata->{$interface_name}->{'default'};
56 1 50       24 $self->logger->trace("The implementation for $interface_name was found in the default environment") if defined($service_definition);
57             }
58              
59 32 50       80 $self->logger->debug("An implementation for $interface_name name was requested, but none have been declared")
60             if(not(defined($service_definition)));
61             } else {
62 1         24 $self->logger->debug("An implementation for $interface_name was requested, but none have been declared");
63             }
64              
65 33         92 return $service_definition;
66             }
67              
68             # () -> result:Int
69             sub services_count {
70 7     7 0 26 my ($self) = @_;
71              
72 7         19 my $count = 0;
73              
74 7         17 while(my ($interface,$environments) = each( %{$self->metadata} )){
  23         582  
75 16         44 $count += keys %$environments;
76             }
77              
78 7         56 return $count;
79             }
80              
81             1;