lib/Serge/Plugin/Base/Callback.pm

filter: Statementstmt 73.6% 28 / 38 Branchbran 30.0% 6 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 7 totaltotal 56.7% 42 / 74 SCAR 32.2
CC
13
Cov
59%
add
22.9
validate_data
7.5
callback
2.0
CRAP 25.0
« lib/Serge/Engine/Plugin/parse_rrc.pm ↑ Summary lib/Serge/Config/Collector.pm »
1 package Serge::Plugin::Base::Callback;
2 2 ▶use parent Serge::Plugin::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 sub init {
5 35 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
6  
7 35 $self->SUPER::init(@_);
8  
9 35 $self->{callback_map} = {};
10 }
11  
12 sub validate_data {
13 35 ▶ my ($self) = @_;
Subroutine validate_data: called CC=4 SCAR=20.1
Pod undocumented
14  
15 35 $self->SUPER::validate_data;
16  
17 # check for unknown phase names
18 35 ▶ if (exists $self->{data}->{phase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{"phase"}) 0 35 35
19 0 foreach my $phase (@{$self->{data}->{phase}}) {
20 0 ▶ die "Unsupported or unknown phase: '$phase'" unless exists $self->{callback_map}->{$phase};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"callback_map"}{$phase} 0 0 0
21 }
22 }
23 }
24  
25 sub clear_callback_map {
26 0 ▶ my $self = shift;
Subroutine clear_callback_map: not called CC=1 SCAR=6.9
Pod undocumented
27  
28 0 $self->{callback_map} = {};
29 }
30  
31 sub get_phases {
32 34 ▶ my $self = shift;
Subroutine get_phases: called CC=1 SCAR=0.0
Pod undocumented
33  
34 # return all phases plugin can handle
35 34 return keys %{$self->{callback_map}};
36 }
37  
38 sub adjust_phases {
39 21 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: called CC=1 SCAR=0.0
Pod undocumented
40  
41 # do not enforce any phases by default
42 }
43  
44 sub add {
45 32 ▶ my $self = shift;
Subroutine add: called CC=9 SCAR=31.3
Pod undocumented
46  
47 32 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 32 0 32
elsif (@_ == 2) { } 0 0 0
48 32 my $map = shift;
49  
50 32 ▶ die "Not a hash" unless ref($map) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map eq "HASH" 0 32 32
51  
52 32 foreach my $phase (keys %$map) {
53 124 ▶ die "Hash key is not a scalar" unless ref(\$phase) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$phase eq "SCALAR" 0 124 124
54 124 ▶ die "Hash value for key '$key' is not a code reference" unless ref($map->{$phase}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map->{$phase} eq "CODE" 0 124 124
55  
56 124 $self->{callback_map}->{$phase} = $map->{$phase};
57 }
58 } elsif (@_ == 2) {
59 0 my ($phase, $method) = @_;
60  
61 0 ▶ die "First parameter is not a scalar" unless ref(\$phase) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$phase eq "SCALAR" 0 0 0
62 0 ▶ die "Second parameter is not a code reference" unless ref($method) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $method eq "CODE" 0 0 0
63  
64 0 $self->{callback_map}->{$phase} = $method;
65 } else {
66 0 die "Only one parameter (HASH) or two parameters (SCALAR, CODE) are allowed";
67 }
68 }
69  
70 sub callback {
71 249 ▶ my ($self, $phase) = @_;
Subroutine callback: called CC=2 SCAR=7.1
Pod undocumented
72 249 my $funcref = $self->{callback_map}->{$phase};
73 249 ▶ &$funcref(@_) if defined $funcref; # run with the rest of the passed parameters
Branch: 1 branch
BranchTrueFalseTotal
if defined $funcref 249 0 249
74 }
75  
76 2 1;
« lib/Serge/Engine/Plugin/parse_rrc.pm ↑ Summary lib/Serge/Config/Collector.pm »