| 1 | package MojoX::Authentication::Model::Role::Remap; | ||||||||||||||||||
| 2 | 15 | { our $VERSION = '0.099001' } | |||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 32 | 69 | ▶ | if (ref($remap) eq 'CODE') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 38 | 6 | ▶ | @pairs = reverse(@pairs) if $backwards; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 39 | 6 | ▶ | while (@pairs) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | 12 | my ($okey, $ikey) = splice(@pairs, 0, 2); | |||||||||||||||||
| 41 | 12 | ▶ | next unless exists($record->{$ikey}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 49 | 40 | ▶ | next unless exists($record->{$ikey}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | $record->{$okey} = $op ? $op->($record->{$ikey}, $args->@*) | ||||||||||||||||||
| 51 | 2 | ▶ | : $record->{$ikey}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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__ | ||||||||||||||||||