Authentication/Model/Role/Remap.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 84.6% 22 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 92.4% 73 / 79 CC 17 SCAR 22.3
CC
17
Cov
91%
remap
14.7
_restore_protected_keys
3.0
_remove_protected_keys
2.0
CRAP 9.3
« Authentication/Model.pm ↑ Summary Authentication/Model/Hash.pm »
1 package MojoX::Authentication::Model::Role::Remap;
2 15 { our $VERSION = '0.100' }
3  
4 15 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Ouch qw< :trytiny_var >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶sub _remove_protected_keys ($self, $record) {
Subroutine _remove_protected_keys: called CC=2 SCAR=6.9
11 33 ▶ return [] unless $self->can('protected_keys_list');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("protected_keys_list") 17 16 33
12 return [
13 map {
14 16 my $has_key = exists($record->{$_});
15 22 [ $_, $has_key, delete($record->{$_}) ];
16 } $self->protected_keys_list
17 ];
18 }
19  
20 33 ▶sub _restore_protected_keys ($self, $record, $list) {
Subroutine _restore_protected_keys: called CC=3 SCAR=11.0
21 33 for my $item (reverse($list->@*)) {
22 22 my ($key, $restore, $value) = $item->@*;
23 22 delete($record->{$key});
24 22 ▶ $record->{$key} = $value if $restore;
Branch: 1 branch
BranchTrueFalseTotal
if $restore 21 1 22
25 }
26 33 return;
27 }
28  
29 33 ▶sub remap ($self, $record, $mappings, $backwards = 0) {
Subroutine remap: called CC=14 SCAR=26.9
Pod documented
30 33 my $to_restore = $self->_remove_protected_keys($record);
31 33 ▶ for my $remap ($backwards ? reverse($mappings->@*) : $mappings->@*) {
Branch: 1 branch
BranchTrueFalseTotal
$backwards ? : 5 28 33
32 69 ▶ if (ref($remap) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $remap eq 'CODE') { } 23 46 69
elsif (ref $remap eq 'ARRAY') { } 6 40 46
elsif (ref $remap eq 'HASH') { } 40 0 40
33 23 $remap->($record, $backwards);
34 }
35 elsif (ref($remap) eq 'ARRAY') { # simple okey = ikey remaps
36 6 my @pairs = $remap->@*;
37 6 ▶ ouch 400, 'odd number of elements in array remap' if @pairs % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs % 2 0 6 6
38 6 ▶ @pairs = reverse(@pairs) if $backwards;
Branch: 1 branch
BranchTrueFalseTotal
if $backwards 1 5 6
39 6 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 12 6 18
40 12 my ($okey, $ikey) = splice(@pairs, 0, 2);
41 12 ▶ next unless exists($record->{$ikey});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $record->{$ikey} 12 0 12
42 0 $record->{$okey} = $record->{$ikey};
43 }
44 }
45 elsif (ref($remap) eq 'HASH') { # more complex stuff
46 my ($okey, $ikey, $op, $args) =
47 $backwards ? $remap->@{qw< ikey okey backwards_op >}
48 40 ▶ : $remap->@{qw< okey ikey op >};
Branch: 1 branch
BranchTrueFalseTotal
$backwards ? : 1 39 40
49 40 ▶ next unless exists($record->{$ikey});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $record->{$ikey} 38 2 40
50 $record->{$okey} = $op ? $op->($record->{$ikey}, $args->@*)
51 2 ▶ : $record->{$ikey};
Branch: 1 branch
BranchTrueFalseTotal
$op ? : 2 0 2
52 }
53 else {
54 0 ouch 400, 'unsupported remapping', $remap;
55 }
56 }
57 33 $self->_restore_protected_keys($record, $to_restore);
58 33 return $record;
59 }
60  
61  
62 15 1;
63 __END__
« Authentication/Model.pm ↑ Summary Authentication/Model/Hash.pm »