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 5     5   2629 use Moose;
  5         14  
  5         37  
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 23 my ($self,$interface_name) = @_;
13              
14 6         247 return exists($self->metadata->{$interface_name});
15             }
16              
17             # (service_metadata: ServiceMetadata) -> Void
18             sub add_service_definition {
19 19     19 0 57 my ($self,$service_metadata) = @_;
20              
21 19         836 my $interface_name = $service_metadata->implements;
22 19         824 my $service_name = $service_metadata->class_name;
23 19         839 my $environment = $service_metadata->environment;
24              
25 19         665 $self->logger->debug("Registering service $service_name for interface $interface_name ");
26            
27 19 100       889 if( exists $self->metadata->{$interface_name} ) {
28 1         44 my $interface_definition = $self->metadata->{$interface_name};
29 1 50       4 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         4 $interface_definition->{$environment} = $service_metadata;
35             } else {
36 18         649 $self->metadata->{$interface_name}->{$environment} = $service_metadata;
37             }
38              
39 19         643 $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 37     37 0 131 my ($self,$interface_name,$original_environment) = @_;
45 37   100     133 my $environment = $original_environment || 'default';
46              
47 37         1423 $self->logger->trace("An implementation for $interface_name was requested");
48 37         456 my $service_definition;
49            
50 37 100       1515 if( exists $self->metadata->{$interface_name} ) {
51 36         1407 $service_definition = $self->metadata->{$interface_name}->{$environment};
52             # always try the default environment if the specified one didn't exist
53 36 50 66     133 if(not(defined($service_definition)) and not($environment eq 'default')){
54 1         46 $self->logger->trace("An implementation for $interface_name was not found in environment $environment, searching in default namespace");
55 1         37 $service_definition = $self->metadata->{$interface_name}->{'default'};
56 1 50       27 $self->logger->trace("The implementation for $interface_name was found in the default environment") if defined($service_definition);
57             }
58              
59 36 50       121 $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         26 $self->logger->debug("An implementation for $interface_name was requested, but none have been declared");
63             }
64              
65 37         133 return $service_definition;
66             }
67              
68             # () -> result:Int
69             sub services_count {
70 8     8 0 26 my ($self) = @_;
71              
72 8         22 my $count = 0;
73              
74 8         21 while(my ($interface,$environments) = each( %{$self->metadata} )){
  26         930  
75 18         71 $count += keys %$environments;
76             }
77              
78 8         66 return $count;
79             }
80              
81             1;