| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 48 | 32 | my $map = shift; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 32 | ▶ | die "Not a hash" unless ref($map) eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 54 | 124 | ▶ | die "Hash value for key '$key' is not a code reference" unless ref($map->{$phase}) eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 62 | 0 | ▶ | die "Second parameter is not a code reference" unless ref($method) eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | 1; | |||||||||||||