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   2747 use Moose;
  5         11  
  5         36  
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 17 my ($self,$interface_name) = @_;
13              
14 6         185 return exists($self->metadata->{$interface_name});
15             }
16              
17             # (service_metadata: ServiceMetadata) -> Void
18             sub add_service_definition {
19 19     19 0 50 my ($self,$service_metadata) = @_;
20              
21 19         649 my $interface_name = $service_metadata->implements;
22 19         607 my $service_name = $service_metadata->class_name;
23 19         654 my $environment = $service_metadata->environment;
24              
25 19         522 $self->logger->debug("Registering service $service_name for interface $interface_name ");
26            
27 19 100       676 if( exists $self->metadata->{$interface_name} ) {
28 1         25 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 18         460 $self->metadata->{$interface_name}->{$environment} = $service_metadata;
37             }
38              
39 19         458 $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 100 my ($self,$interface_name,$original_environment) = @_;
45 37   100     94 my $environment = $original_environment || 'default';
46              
47 37         1135 $self->logger->trace("An implementation for $interface_name was requested");
48 37         333 my $service_definition;
49            
50 37 100       1019 if( exists $self->metadata->{$interface_name} ) {
51 36         932 $service_definition = $self->metadata->{$interface_name}->{$environment};
52             # always try the default environment if the specified one didn't exist
53 36 50 66     103 if(not(defined($service_definition)) and not($environment eq 'default')){
54 1         23 $self->logger->trace("An implementation for $interface_name was not found in environment $environment, searching in default namespace");
55 1         29 $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 36 50       92 $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         23 $self->logger->debug("An implementation for $interface_name was requested, but none have been declared");
63             }
64              
65 37         98 return $service_definition;
66             }
67              
68             # () -> result:Int
69             sub services_count {
70 8     8 0 20 my ($self) = @_;
71              
72 8         20 my $count = 0;
73              
74 8         21 while(my ($interface,$environments) = each( %{$self->metadata} )){
  26         678  
75 18         59 $count += keys %$environments;
76             }
77              
78 8         52 return $count;
79             }
80              
81             1;