blib/lib/Data/Random/HashType.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 100% 94 / 94 CC 8 SCAR 10.6
CC
8
Cov
100%
random
6.0
new
3.0
BEGIN
1.0
CRAP 2.9
↑ Summary
1 package Data::Random::HashType;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::HashType 0.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mo::utils 0.25 qw(check_bool check_isa check_number_min check_required);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Random::Day::InThePast;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 Readonly::Array our @OBSOLETE_HASH_TYPES => qw(MD4 MD5 SHA1);
15 4 Readonly::Array our @DEFAULT_HASH_TYPES => qw(SHA-256 SHA-384 SHA-512);
16 4 Readonly::Array our @ALL_HASH_TYPES => (@OBSOLETE_HASH_TYPES, @DEFAULT_HASH_TYPES);
17  
18 4 our $VERSION = 0.06;
19  
20 15 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
21 15 my ($class, @params) = @_;
22  
23 # Create object.
24 15 my $self = bless {}, $class;
25  
26 # Start date time.
27 15 $self->{'dt_start'} = DateTime->new(
28 'day' => 1,
29 'month' => 1,
30 'year' => ((localtime)[5] + 1900 - 1),
31 );
32  
33 # Id.
34 15 $self->{'id'} = 1;
35 4 ▶ $self->{'cb_id'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 4 return $self->{'id'}++;
37 15 };
38  
39 # Add id or not.
40 15 $self->{'mode_id'} = 0;
41  
42 # Number of hash types.
43 15 $self->{'num_generated'} = 1;
44  
45 # Hash types.
46 15 $self->{'possible_hash_types'} = \@DEFAULT_HASH_TYPES;
47  
48 # Process parameters.
49 15 set_params($self, @params);
50  
51 15 check_required($self, 'dt_start');
52 14 check_isa($self, 'dt_start', 'DateTime');
53 13 check_bool($self, 'mode_id');
54 12 check_number_min($self, 'num_generated', 1);
55 11 check_required($self, 'num_generated');
56 10 ▶ if (ref $self->{'possible_hash_types'} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"possible_hash_types"} ne "ARRAY") 1 9 10
57 1 err "Parameter 'possible_hash_types' must be a reference to array.";
58 }
59 9 ▶ if (! @{$self->{'possible_hash_types'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"possible_hash_types"};}) 1 8 9
60 1 err "Parameter 'possible_hash_types' must contain at least one hash type name.";
61 }
62  
63 $self->{'_random_valid_from'} = Random::Day::InThePast->new(
64 8 'dt_from' => $self->{'dt_start'},
65 );
66  
67 8 return $self;
68 }
69  
70 7 ▶sub random {
Subroutine random: called CC=6 SCAR=17.9
Pod documented
71 7 my $self = shift;
72  
73 7 my @ret;
74 7 ▶ if ($self->{'num_generated'} < @{$self->{'possible_hash_types'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'num_generated'} < @{$self->{'possible_hash_types'};}) { } 2 5 7
75  
76 2 my @list = @{$self->{'possible_hash_types'}};
77 2 foreach my $id (1 .. $self->{'num_generated'}) {
78 2 my $rand_index = int(rand(scalar @list));
79 2 my $hash_type = splice @list, $rand_index, 1;
80 push @ret, Data::HashType->new(
81 $self->{'mode_id'} ? (
82 'id' => $self->{'cb_id'}->($self),
83 ) : (),
84 'name' => $hash_type,
85 2 ▶ 'valid_from' => $self->{'_random_valid_from'}->get->clone,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mode_id'} ? : 1 1 2
86 );
87 }
88 } else {
89 5 my $i = 1;
90 5 foreach my $hash_type (@{$self->{'possible_hash_types'}}) {
91 push @ret, Data::HashType->new(
92 $self->{'mode_id'} ? (
93 'id' => $self->{'cb_id'}->($self),
94 ) : (),
95 'name' => $hash_type,
96 8 ▶ 'valid_from' => $self->{'_random_valid_from'}->get->clone,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mode_id'} ? : 5 3 8
97 );
98 8 $i++;
99 }
100 }
101  
102 7 return @ret;
103 }
104  
105 4 1;
106  
107 __END__
↑ Summary