blib/lib/UID2/Client/KeyContainer.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 12 / 12 Conditioncond 81.8% 9 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 88.2% 75 / 85 SCAR 24.9
CC
12
Cov
97%
new
7.0
get_active_site_key
5.0
is_valid
2.0
CRAP 12.0
« blib/lib/UID2/Client.pm ↑ Summary blib/lib/UID2/Client/Key.pm »
1 package UID2::Client::KeyContainer;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use List::MoreUtils qw(upper_bound);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use UID2::Client::Timestamp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub new {
9 56 ▶ my ($class, @keys) = @_;
Subroutine new: called CC=7 SCAR=19.5
Pod undocumented
10 56 my $latest_expires = 0;
11 56 my (%keys, %keys_by_site);
12 56 for my $key (@keys) {
13 135 $keys{$key->id} = $key;
14 135 ▶ if ($key->site_id > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->site_id > 0) 109 26 135
15 109 ▶ push @{$keys_by_site{$key->site_id} //= []}, $key;
Condition100%2/2
$keys_by_site{$key->site_id} //= []
l!l
5554
MC/DC100%1/1
$keys_by_site{$key->site_id} //= []
$keys_by_site{$key->site_id}
Truth table100%2/2
A
A= $keys_by_site{$key->site_id}
Aresult
11
00
16 }
17 135 ▶ if (!$latest_expires or $key->expires > $latest_expires) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $latest_expires or $key->expires > $latest_expires) 57 78 135
Condition100%3/3
not $latest_expires or $key->expires > $latest_expires
l!l&&r!l&&!r
56178
MC/DC100%2/2
not $latest_expires or $key->expires > $latest_expires
not $latest_expires $key->expires > $latest_expires
Truth table100%3/3
not(A) or B
A= not $latest_expiresB= $key->expires > $latest_expires
ABresult
1X1
011
000
18 57 $latest_expires = $key->expires;
19 }
20 }
21 56 ▶ while (my ($site_id, $keys) = each %keys_by_site) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %keys_by_site) 54 56 110
22 54 $keys_by_site{$site_id} = [sort { $a->activates <=> $b->activates } @$keys];
23 }
24 bless {
25 56 keys => \%keys,
26 keys_by_site => \%keys_by_site,
27 latest_expires => $latest_expires,
28 }, $class;
29 }
30  
31 sub get {
32 50 ▶ my ($self, $id) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
33 50 $self->{keys}->{$id};
34 }
35  
36 sub is_valid {
37 40 ▶ my ($self, $now) = @_;
Subroutine is_valid: called CC=2 SCAR=7.0
Pod undocumented
38 40 ▶ $now //= UID2::Client::Timestamp->now;
Condition66%2/3
$now //= "UID2::Client::Timestamp"->now
l!l&&r!l&&!r
3820
MC/DC0%0/2
$now //= "UID2::Client::Timestamp"->now
$now "UID2::Client::Timestamp"->now
Truth table66%2/3
A //= B
A= $nowB= "UID2::Client::Timestamp"->now
ABresult
1X1
011
000
39 40 $self->{latest_expires} > $now->get_epoch_second;
40 }
41  
42 sub get_active_site_key {
43 19 ▶ my ($self, $site_id, $now) = @_;
Subroutine get_active_site_key: called CC=5 SCAR=16.1
Pod undocumented
44 19 ▶ my $keys = $self->{keys_by_site}->{$site_id} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $keys = $self->{"keys_by_site"}{$site_id} or return 1 18 19
45 18 ▶ $now //= UID2::Client::Timestamp->now;
Condition66%2/3
$now //= "UID2::Client::Timestamp"->now
l!l&&r!l&&!r
1710
MC/DC0%0/2
$now //= "UID2::Client::Timestamp"->now
$now "UID2::Client::Timestamp"->now
Truth table66%2/3
A //= B
A= $nowB= "UID2::Client::Timestamp"->now
ABresult
1X1
011
000
46 18 my $second = $now->get_epoch_second;
47 18 ▶ my $i = upper_bound { $_->activates <=> $second } @$keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
48 18 ▶ while ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 18 4 22
49 18 $i--;
50 18 ▶ return $keys->[$i] if $keys->[$i]->is_active($now);
Branch: 1 branch
BranchTrueFalseTotal
if $keys->[$i]->is_active($now) 14 4 18
51 }
52 4 return;
53 }
54  
55 4 1;
56 __END__
« blib/lib/UID2/Client.pm ↑ Summary blib/lib/UID2/Client/Key.pm »