| 1 | package Ubic::Multiservice; | ||||||||||
| 2 | # ABSTRACT: interface of multiservice representing several named services | ||||||||||
| 3 | 156 | $Ubic::Multiservice::VERSION = '1.59'; | |||||||||
| 4 | 156 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 156 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | sub simple_service($$); | ||||||||||
| 8 | |||||||||||
| 9 | sub service_names($); | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | 156 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 156 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 156 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 156 | ▶ | use parent qw(Ubic::Service); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | sub service($$) { | ||||||||||
| 19 | 1460 | ▶ | my $self = shift; | ||||||||
|
Subroutine
service: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 20 | 1460 | my ($name) = validate_pos(@_, { type => SCALAR, regex => qr{^[\w-]+(?:\.[\w-]+)*$} }); | |||||||||
| 21 | 1460 | my @parts = split '\\.', $name; | |||||||||
| 22 | |||||||||||
| 23 | 1460 | ▶ | if ($self->{service_cache}{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 103 | ▶ | if (my $error = $self->{service_cache}{$name}{error}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | die $error; | |||||||||
| 26 | } | ||||||||||
| 27 | else { | ||||||||||
| 28 | 103 | return $self->{service_cache}{$name}{service}; | |||||||||
| 29 | } | ||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 1357 | my $service; | |||||||||
| 33 | try { | ||||||||||
| 34 | 1357 | ▶ | if (@parts == 1) { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||
| 35 | 1284 | $service = $self->simple_service($name); | |||||||||
| 36 | 1277 | ▶ | unless (defined $service->name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 138 | $service->name($name); | |||||||||
| 38 | } | ||||||||||
| 39 | 1277 | $service->parent_name($self->full_name); | |||||||||
| 40 | } | ||||||||||
| 41 | else { | ||||||||||
| 42 | # complex service | ||||||||||
| 43 | 73 | my $top_level = $self->simple_service($parts[0]); | |||||||||
| 44 | 73 | ▶ | unless ($top_level->isa('Ubic::Multiservice')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | croak "top-level service '$parts[0]' is not a multiservice"; | |||||||||
| 46 | } | ||||||||||
| 47 | 73 | ▶ | unless (defined $top_level->name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 3 | $top_level->name($parts[0]); | |||||||||
| 49 | } | ||||||||||
| 50 | 73 | $top_level->parent_name($self->full_name); | |||||||||
| 51 | 73 | $service = $top_level->service(join '.', @parts[1..$#parts]); | |||||||||
| 52 | } | ||||||||||
| 53 | 1349 | $self->{service_cache}{$name} = { service => $service }; | |||||||||
| 54 | } | ||||||||||
| 55 | catch { | ||||||||||
| 56 | 8 | ▶ | $self->{service_cache}{$name} = { error => $_ }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 8 | die $_; | |||||||||
| 58 | 1357 | }; | |||||||||
| 59 | 1349 | return $service; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | sub has_service($$) { | ||||||||||
| 64 | 262 | ▶ | my $self = shift; | ||||||||
|
Subroutine
has_service: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 65 | 262 | my ($name) = validate_pos(@_, { type => SCALAR, regex => qr{^[\w-]+(?:\.[\w-]+)*$} }); | |||||||||
| 66 | 262 | my @parts = split '\\.', $name; | |||||||||
| 67 | 262 | ▶ | if (@parts == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 204 | return $self->has_simple_service($name); | |||||||||
| 69 | } | ||||||||||
| 70 | # complex service | ||||||||||
| 71 | 58 | ▶ | return undef unless $self->has_service($parts[0]); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 57 | my $top_level = $self->service($parts[0]); | |||||||||
| 73 | 57 | ▶ | unless ($top_level->isa('Ubic::Multiservice')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | # strange, top-level service is not a multiservice | ||||||||||
| 75 | 3 | return undef; | |||||||||
| 76 | } | ||||||||||
| 77 | 54 | return $top_level->has_service(join '.', @parts[1..$#parts]); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | sub services($) { | ||||||||||
| 81 | 257 | ▶ | my $self = shift; | ||||||||
|
Subroutine
services: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 82 | 257 | my @services; | |||||||||
| 83 | 257 | for my $name ($self->service_names) { | |||||||||
| 84 | 1159 | my $service = eval { $self->service($name) }; | |||||||||
| 85 | 1159 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 5 | warn "Can't construct '$name': $@"; | |||||||||
| 87 | 5 | next; | |||||||||
| 88 | } | ||||||||||
| 89 | 1154 | push @services, $service; | |||||||||
| 90 | } | ||||||||||
| 91 | 257 | return @services; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | sub has_simple_service($$) { | ||||||||||
| 95 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
has_simple_service: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 96 | 0 | my ($name) = validate_pos(@_, { type => SCALAR, regex => qr{^[\w-]+$} }); | |||||||||
| 97 | 0 | return grep { $_ eq $name } $self->service_names; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | sub multiop($) { | ||||||||||
| 101 | 0 | ▶ | return 'allowed'; | ||||||||
|
Subroutine
multiop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | 156 | 1; | |||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||