lib/Date/Holidays.pm

filter: Statementstmt 47.8% 68 / 142 Branchbran 22.0% 11 / 50 Conditioncond 23.8% 5 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 65.3% 17 / 26 Podpod 100% 5 / 5 totaltotal 41.2% 106 / 257 CC 35 SCAR 34.3
CC
35
Cov
39%
__ANON__
132.0
_fetch
19.6
new
16.0
CRAP 30.8
« lib/Date/Holidays/Adapter.pm ↑ Summary blib/lib/Date/Holidays/Adapter/ES.pm »
1 package Date::Holidays;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Locale::Country qw(all_country_codes code2country);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use base 'Date::Holidays::Adapter';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '1.36'; # VERSION: generated by DZP::OurPkgVersion
17  
18 3 ▶sub new {
Subroutine new: called CC=8 SCAR=27.7
Pod documented
19 3 my ( $class, %params ) = @_;
20  
21 3 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
22 _inner_object => undef,
23 _inner_class => undef,
24 _countrycode => undef,
25 },
26 ref $class || $class;
27  
28 3 ▶ if ( $params{'countrycode'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'countrycode'}) { } 3 0 3
29 3 ▶ if ( code2country( $params{countrycode} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code2country($params{'countrycode'})) { } 0 3 3
30 0 $self->{'_countrycode'} = uc $params{countrycode};
31 }
32 else {
33 3 $self->{'_countrycode'} = $params{countrycode};
34 }
35  
36 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 $self->{'_inner_class'} = $self->_fetch(
38 { nocheck => $params{'nocheck'},
39 3 countrycode => $params{'countrycode'},
40 }
41 );
42 3 };
43 }
44 else {
45 0 die "No country code specified\n";
46 }
47  
48 3 ▶ if ( $self
Branch: 2 branches
BranchTrueFalseTotal
if ($self and $self->{'_inner_class'} and $self->{'_inner_class'}->can('new')) { } 3 0 3
elsif (not $self->{'_inner_class'}) { } 0 0 0
Condition33%2/6
$self and $self->{"_inner_class"} and $self->{"_inner_class"}->can("new")
!ll&&!rl&&r
003
$self and $self->{"_inner_class"}
!ll&&!rl&&r
003
MC/DC0%0/3
$self and $self->{"_inner_class"} and $self->{"_inner_class"}->can("new")
$self $self->{"_inner_class"} $self->{"_inner_class"}->can("new")
Truth table25%1/4
A and B and C
A= $selfB= $self->{"_inner_class"}C= $self->{"_inner_class"}->can("new")
ABCresult
0XX0
10X0
1100
1111
49 && $self->{'_inner_class'}
50 && $self->{'_inner_class'}->can('new') )
51 {
52 3 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=8.4
53 my $adapter = $self->{'_inner_class'}->new(
54 countrycode => $self->{'_countrycode'},
55 3 nocheck => $params{'nocheck'},
56 );
57  
58 3 ▶ if ($adapter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adapter) { } 3 0 3
59 3 $self->{'_inner_object'} = $adapter;
60 }
61 else {
62 0 warn "Adapter not defined\n";
63 0 $self = undef;
64 }
65 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 warn "Unable to initialize adapter: $_\n";
67 0 $self = undef;
68 3 };
69  
70 }
71 elsif ( !$self->{'_inner_class'} ) {
72 0 warn "No inner class instantiated\n";
73 0 $self = undef;
74 }
75  
76 3 return $self;
77 }
78  
79 3 ▶sub holidays {
Subroutine holidays: called CC=2 SCAR=7.0
Pod documented
80 3 my ( $self, %params ) = @_;
81  
82 # Our result
83 3 my $r;
84  
85 # Did we get a country list
86 3 ▶ if ( not $params{'countries'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"countries"}) 3 0 3
87  
88 #No countries - so we create a list
89 3 my @countries = all_country_codes(); # From Locale::Country
90 3 @countries = sort @countries;
91  
92 # We stick the complete list of countries to the parameters
93 3 $params{'countries'} = \@countries;
94 }
95  
96 3 $r = $self->{'_inner_object'}->holidays(%params);
97  
98 3 return $r;
99 }
100  
101 3 ▶sub is_holiday {
Subroutine is_holiday: called CC=4 SCAR=19.6
Pod documented
102 3 my ( $self, %params ) = @_;
103  
104 # Our result
105 3 my $r;
106  
107 3 ▶ if ( not $params{'countries'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{'countries'}) { } 3 0 3
108 3 ▶ if ( blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 3 0 3
109 3 $r = $self->{'_inner_object'}->is_holiday(%params);
110 }
111 else {
112 0 my @countries = all_country_codes(); # From Locale::Country
113 0 @countries = sort @countries;
114 0 $params{'countries'} = \@countries;
115  
116 0 $r = __PACKAGE__->_check_countries(%params);
117 }
118  
119 }
120 else {
121 0 ▶ if ( blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 0 0 0
122 0 $r = $self->_check_countries(%params);
123  
124 }
125 else {
126 0 $r = __PACKAGE__->_check_countries(%params);
127 }
128 }
129  
130 3 return $r;
131 }
132  
133 0 ▶sub holidays_dt {
Subroutine holidays_dt: not called CC=2 SCAR=17.9
Pod documented
134 0 my ( $self, %params ) = @_;
135  
136 0 my $hashref = $self->holidays( year => $params{'year'} );
137 0 my %dts;
138  
139 0 foreach my $h ( keys %{$hashref} ) {
140 0 my ( $month, $day ) = $h =~ m{
141 \A # Beginning of string
142 (\d{2}) # 2 digits indicating the month
143 (\d{2}) # 2 digits indicating the day
144 \Z # End of string
145 }xsm;
146 my $dt = DateTime->new(
147 0 year => $params{'year'},
148 month => $month,
149 day => $day,
150 );
151 0 $dts{ $hashref->{$h} } = $dt;
152 }
153  
154 0 return \%dts;
155 }
156  
157 0 ▶sub _check_countries {
Subroutine _check_countries: not called CC=3 SCAR=24.8
158 0 my ( $self, %params ) = @_;
159  
160 0 my $result = {};
161 0 my $precedent_calendar = q{};
162  
163 0 foreach my $country ( @{ $params{'countries'} } ) {
164  
165 #The list of countries is ordered
166 0 ▶ if ( $country =~ m/\A[+](\w+)/xism ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($country =~ /\A[+](\w+)/imsx) 0 0 0
167 0 $country = $1;
168 0 $precedent_calendar = $country;
169 }
170  
171 0 ▶ try {
Subroutine __ANON__: not called CC=11 SCAR=48.8
172 my $dh = $self->new(
173 countrycode => $country,
174 nocheck => $params{nocheck}
175 0 );
176  
177 0 ▶ if ( !$dh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dh) 0 0 0
178 0 my $countryname = code2country($country);
179 0 my $countrycode = $country;
180  
181 0 die
182 "Unable to initialize Date::Holidays for country: $countrycode - $countryname\n";
183 }
184  
185 my %prepared_parameters = (
186 year => $params{'year'},
187 month => $params{'month'},
188 0 day => $params{'day'},
189 );
190  
191 0 ▶ if ( $params{gov} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"gov"}) 0 0 0
192 0 $prepared_parameters{gov} = $params{gov};
193 }
194  
195 0 ▶ if ( $params{lang} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"lang"}) 0 0 0
196 0 $prepared_parameters{lang} = $params{lang};
197 }
198  
199 # did we receive special regions parameter?
200 0 ▶ if ( $params{regions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"regions"}) 0 0 0
201 0 $prepared_parameters{regions} = $params{regions};
202 }
203  
204 # did we receive special state parameter?
205 0 ▶ if ( $params{state} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"state"}) 0 0 0
206 0 $prepared_parameters{state} = $params{state};
207 }
208  
209 0 my $r = $dh->is_holiday(%prepared_parameters);
210  
211 0 ▶ if ( $precedent_calendar eq $country ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precedent_calendar eq $country) 0 0 0
212 0 $self->{precedent_calendar} = $dh;
213 }
214  
215 # handling precedent calendar
216 0 ▶ if ( $precedent_calendar
Branch: 1 branch
BranchTrueFalseTotal
if ($precedent_calendar and $precedent_calendar ne $country) 0 0 0
Condition0%0/3
$precedent_calendar and $precedent_calendar ne $country
!ll&&!rl&&r
000
MC/DC0%0/2
$precedent_calendar and $precedent_calendar ne $country
$precedent_calendar $precedent_calendar ne $country
Truth table0%0/3
A and B
A= $precedent_calendarB= $precedent_calendar ne $country
ABresult
0X0
100
111
217 and $precedent_calendar ne $country )
218 {
219  
220 my $holiday = $self->{precedent_calendar}
221 0 ->is_holiday(%prepared_parameters);
222  
223 # our precedent calendar dictates overwrite or nullification
224 0 ▶ if ( defined $holiday ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $holiday) 0 0 0
225 0 $r = $holiday;
226 }
227 }
228  
229 0 ▶ if ( defined $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) 0 0 0
230 0 $result->{$country} = $r;
231 }
232 }
233 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 warn "$_\n";
235 }
236 0 }
237  
238 0 return $result;
239 }
240  
241 0 ▶sub is_holiday_dt {
Subroutine is_holiday_dt: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243 0 my $dt = shift;
244  
245 0 return $self->is_holiday(
246 year => $dt->year,
247 month => $dt->month,
248 day => $dt->day,
249 @_,
250 );
251 }
252  
253 3 ▶sub _fetch {
Subroutine _fetch: called CC=7 SCAR=29.8
254 3 my ( $self, $params ) = @_;
255  
256 # Do we have a country code?
257 3 ▶ if ( not $self->{'_countrycode'} and not $params->{countrycode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_countrycode"} or $params->{"countrycode"}) 0 3 3
Condition33%1/3
$self->{"_countrycode"} or $params->{"countrycode"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"_countrycode"} or $params->{"countrycode"}
$self->{"_countrycode"} $params->{"countrycode"}
Truth table33%1/3
A or B
A= $self->{"_countrycode"}B= $params->{"countrycode"}
ABresult
1X1
011
000
258 0 die "No country code specified\n";
259 }
260  
261 3 ▶ my $countrycode = $params->{countrycode} || $self->{'_countrycode'};
Condition33%1/3
$params->{'countrycode'} || $self->{'_countrycode'}
l!l&&r!l&&!r
300
MC/DC0%0/2
$params->{'countrycode'} || $self->{'_countrycode'}
$params->{'countrycode'} $self->{'_countrycode'}
Truth table33%1/3
A || B
A= $params->{'countrycode'}B= $self->{'_countrycode'}
ABresult
1X1
011
000
262  
263 # Do we do country code assertion?
264 3 ▶ if ( !$params->{'nocheck'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"nocheck"}) 0 3 3
265  
266 # Is our country code valid or local?
267 0 ▶ if ( $countrycode !~ m/\Alocal\Z/xism
Branch: 1 branch
BranchTrueFalseTotal
unless ($countrycode =~ /\Alocal\Z/imsx or code2country($countrycode)) 0 0 0
Condition0%0/3
$countrycode =~ /\Alocal\Z/imsx or code2country($countrycode)
l!l&&r!l&&!r
000
MC/DC0%0/2
$countrycode =~ /\Alocal\Z/imsx or code2country($countrycode)
$countrycode =~ /\Alocal\Z/imsx code2country($countrycode)
Truth table0%0/3
A or B
A= $countrycode =~ /\Alocal\Z/imsxB= code2country($countrycode)
ABresult
1X1
011
000
268 and not code2country($countrycode) )
269 { #from Locale::Country
270 0 die "$countrycode is not a valid country code\n";
271 }
272 }
273  
274 # Trying to load adapter module for country code
275 3 my $module;
276  
277 3 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=13.3
278 # We load an adapter implementation
279 3 ▶ if ( $countrycode =~ m/\Alocal\Z/xism ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($countrycode =~ /\Alocal\Z/imsx) { } 0 3 3
elsif (code2country($countrycode)) { } 0 3 3
280 0 $module = 'Date::Holidays::Adapter::Local';
281 }
282 elsif ( code2country($countrycode) ) {
283 0 $module = 'Date::Holidays::Adapter::' . uc $countrycode;
284 }
285 else {
286 3 $module = 'Date::Holidays::Adapter::' . $countrycode;
287 }
288  
289 3 $module = $self->_load($module);
290  
291 }
292 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
293 0 warn "Unable to load module: $module - $_\n";
294  
295 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
296  
297 # We load an adapter implementation
298 0 $module = 'Date::Holidays::Adapter::' . $countrycode;
299  
300 0 ▶ if ( $module = $self->_load($module) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module = $self->_load($module)) 0 0 0
301 0 warn "we got a module and we return\n";
302 }
303  
304 }
305 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
306 0 warn "Unable to load module: $module - $_\n";
307  
308 0 $module = 'Date::Holidays::Adapter';
309 0 $module = $self->_load($module);
310 0 };
311 3 };
312  
313 # Returning name of loaded module upon success
314 3 return $module;
315 }
316  
317 2 1;
318  
319 __END__
« lib/Date/Holidays/Adapter.pm ↑ Summary blib/lib/Date/Holidays/Adapter/ES.pm »