Sanctions.pm

filter: Statementstmt 77.8% 176 / 226 Branchbran 48.3% 30 / 62 Conditioncond 53.0% 43 / 81 MC/DCmcdc 28.2% 13 / 46 Subroutinesub 83.3% 30 / 36 Podpod 100% 9 / 9 totaltotal 65.4% 301 / 460 CC 80 SCAR 34.6
CC
80
Cov
67%
update_data
182.0
get_sanctioned_info
29.9
_load_data
15.0
CRAP 31.9
« Sanctions/Redis.pm ↑ Summary Sanctions/Fetcher.pm »
1 package Data::Validate::Sanctions;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 require Exporter;
7 9 our @ISA = qw(Exporter);
8 9 our @EXPORT_OK = qw/is_sanctioned set_sanction_file get_sanction_file/;
9  
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Data::Validate::Sanctions::Fetcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Data::Validate::Sanctions::Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use YAML::XS qw/DumpFile LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use List::Util qw(any uniq max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Locale::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Text::Trim qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 9 our $VERSION = '0.21';
25  
26 9 my $sanction_file;
27 my $instance;
28  
29 9 ▶use constant IGNORE_OPERATION_INTERVAL => 8 * 60; # 8 minutes
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # for OO
32 5 ▶sub new { ## no critic (RequireArgUnpacking)
Subroutine new: called CC=5 SCAR=17.4
Pod documented
33 5 my ($class, %args) = @_;
34  
35 5 ▶ my $storage = delete $args{storage} // '';
Condition50%1/2
delete $args{"storage"} // ""
l!l
05
MC/DC0%0/1
delete $args{"storage"} // ""
delete $args{"storage"}
Truth table50%1/2
A
A= delete $args{"storage"}
Aresult
11
00
36  
37 5 ▶ return Data::Validate::Sanctions::Redis->new(%args) if $storage eq 'redis';
Branch: 1 branch
BranchTrueFalseTotal
if $storage eq "redis" 0 5 5
38  
39 5 my $self = {};
40  
41 5 ▶ $self->{sanction_file} = $args{sanction_file} // _default_sanction_file();
Condition33%1/3
$args{"sanction_file"} // _default_sanction_file()
l!l&&r!l&&!r
500
MC/DC0%0/2
$args{"sanction_file"} // _default_sanction_file()
$args{"sanction_file"} _default_sanction_file()
Truth table33%1/3
A // B
A= $args{"sanction_file"}B= _default_sanction_file()
ABresult
1X1
011
000
42  
43 5 $self->{args} = {%args};
44  
45 5 $self->{last_modification} = 0;
46 5 $self->{last_index} = 0;
47 5 $self->{last_data_load} = 0;
48  
49 5 ▶ return bless $self, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
50 }
51  
52 0 ▶sub update_data {
Subroutine update_data: not called CC=13 SCAR=52.0
Pod documented
53 0 my ($self, %args) = @_;
54  
55 0 $self->_load_data();
56  
57 0 my $new_data = Data::Validate::Sanctions::Fetcher::run($self->{args}->%*, %args);
58 0 my $updated = 0;
59 0 foreach my $k (keys %$new_data) {
60 0 ▶ $self->{_data}->{$k} //= {};
Condition0%0/2
$self->{"_data"}{$k} //= {}
l!l
00
MC/DC0%0/1
$self->{"_data"}{$k} //= {}
$self->{"_data"}{$k}
Truth table0%0/2
A
A= $self->{"_data"}{$k}
Aresult
11
00
61 0 ▶ $self->{_data}->{$k}->{updated} //= 0;
Condition0%0/2
$self->{"_data"}{$k}{"updated"} //= 0
l!l
00
MC/DC0%0/1
$self->{"_data"}{$k}{"updated"} //= 0
$self->{"_data"}{$k}{"updated"}
Truth table0%0/2
A
A= $self->{"_data"}{$k}{"updated"}
Aresult
11
00
62 0 ▶ $self->{_data}->{$k}->{content} //= [];
Condition0%0/2
$self->{"_data"}{$k}{"content"} //= []
l!l
00
MC/DC0%0/1
$self->{"_data"}{$k}{"content"} //= []
$self->{"_data"}{$k}{"content"}
Truth table0%0/2
A
A= $self->{"_data"}{$k}{"content"}
Aresult
11
00
63  
64 0 ▶ if (!$new_data->{$k}->{error} && $self->{_data}->{$k}->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $new_data->{$k}{"error"} and $self->{"_data"}{$k}{"error"}) 0 0 0
Condition0%0/3
not $new_data->{$k}{"error"} and $self->{"_data"}{$k}{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $new_data->{$k}{"error"} and $self->{"_data"}{$k}{"error"}
not $new_data->{$k}{"error"} $self->{"_data"}{$k}{"error"}
Truth table0%0/3
not(A) and B
A= not $new_data->{$k}{"error"}B= $self->{"_data"}{$k}{"error"}
ABresult
0X0
100
111
65 0 delete $self->{_data}->{$k}->{error};
66 0 $updated = 1;
67 }
68  
69 0 ▶ if ($new_data->{$k}->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_data->{$k}{'error'}) { } 0 0 0
elsif ($self->{'_data'}{$k}{'updated'} != $new_data->{$k}{'updated'} or scalar @{$self->{'_data'}{$k}{'content'}} != scalar @{$new_data->{$k}{'content'}}) { } 0 0 0
70 0 warn "$k list update failed because: $new_data->{$k}->{error}";
71 0 $self->{_data}->{$k}->{error} = $new_data->{$k}->{error};
72 0 $updated = 1;
73 ▶ } elsif ($self->{_data}{$k}->{updated} != $new_data->{$k}->{updated}
Condition0%0/3
$self->{"_data"}{$k}{"updated"} != $new_data->{$k}{"updated"} or scalar @{$self->{"_data"}{$k}{"content"}} != scalar @{$new_data->{$k}{"content"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_data"}{$k}{"updated"} != $new_data->{$k}{"updated"} or scalar @{$self->{"_data"}{$k}{"content"}} != scalar @{$new_data->{$k}{"content"}}
$self->{"_data"}{$k}{"updated"} != $new_data->{$k}{"updated"} scalar @{$self->{"_data"}{$k}{"content"}} != scalar @{$new_data->{$k}{"content"}}
Truth table0%0/3
A or B
A= $self->{"_data"}{$k}{"updated"} != $new_data->{$k}{"updated"}B= scalar @{$self->{"_data"}{$k}{"content"}} != scalar @{$new_data->{$k}{"content"}}
ABresult
1X1
011
000
74 || scalar $self->{_data}{$k}->{content}->@* != scalar $new_data->{$k}->{content}->@*)
75 {
76 0 ▶ print "Source $k is updated with new data \n" if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 0 0 0
77 0 $self->{_data}->{$k} = $new_data->{$k};
78 0 $updated = 1;
79 } else {
80 0 ▶ print "Source $k is not changed \n" if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 0 0 0
81 }
82 }
83  
84 0 ▶ if ($updated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updated) 0 0 0
85 0 $self->_save_data();
86 0 $self->_index_data();
87 }
88  
89 0 return;
90 }
91  
92 0 ▶sub last_updated {
Subroutine last_updated: not called CC=3 SCAR=24.8
Pod documented
93 0 my $self = shift;
94 0 my $list = shift;
95  
96 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
97 0 return $self->{_data}->{$list}->{updated};
98 } else {
99 0 $self->_load_data();
100 0 ▶ return max(map { $_->{updated} // 0 } values %{$self->{_data}});
Condition0%0/2
$_->{"updated"} // 0
l!l
00
MC/DC0%0/1
$_->{"updated"} // 0
$_->{"updated"}
Truth table0%0/2
A
A= $_->{"updated"}
Aresult
11
00
101 }
102 }
103  
104 4 ▶sub set_sanction_file { ## no critic (RequireArgUnpacking)
Subroutine set_sanction_file: called CC=2 SCAR=6.9
Pod documented
105 4 ▶ $sanction_file = shift // die "sanction_file is needed";
Condition100%2/2
shift() // die("sanction_file is needed")
l!l
31
MC/DC100%1/1
shift() // die("sanction_file is needed")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
106 3 undef $instance;
107 3 return;
108 }
109  
110 7 ▶sub get_sanction_file {
Subroutine get_sanction_file: called CC=3 SCAR=11.7
Pod documented
111 7 ▶ $sanction_file //= _default_sanction_file();
Condition66%2/3
$sanction_file //= _default_sanction_file()
l!l&&r!l&&!r
520
MC/DC0%0/2
$sanction_file //= _default_sanction_file()
$sanction_file _default_sanction_file()
Truth table66%2/3
A //= B
A= $sanction_fileB= _default_sanction_file()
ABresult
1X1
011
000
112 7 ▶ return $instance ? $instance->{sanction_file} : $sanction_file;
Branch: 1 branch
BranchTrueFalseTotal
$instance ? : 0 7 7
113 }
114  
115 =head2 is_sanctioned
116  
117 Checks if the input profile info matches a sanctioned entity.
118 The arguments are the same as those of B<get_sanctioned_info>.
119  
120 It returns 1 if a match is found, otherwise 0.
121  
122 =cut
123  
124 14 ▶sub is_sanctioned { ## no critic (RequireArgUnpacking)
Subroutine is_sanctioned: called CC=1 SCAR=0.0
Pod documented
125 14 return (get_sanctioned_info(@_))->{matched};
126 }
127  
128 0 ▶sub data {
Subroutine data: not called CC=2 SCAR=17.9
Pod documented
129 0 my ($self) = @_;
130  
131 0 ▶ $self->_load_data() unless $self->{_data};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_data"} 0 0 0
132  
133 0 return $self->{_data};
134 }
135  
136 =head2 _match_other_fields
137  
138 Matches fields possibly available in addition to name and date of birth.
139  
140 Returns a a hash-ref reporting the matched fields if it succeeeds; otherwise returns false (undef).
141  
142 =cut
143  
144 20 ▶sub _match_other_fields {
Subroutine _match_other_fields: called CC=6 SCAR=24.2
145 20 my ($self, $entry, $args) = @_;
146  
147 20 my @optional_fields = qw/place_of_birth residence nationality citizen postal_code national_id passport_no/;
148  
149 20 my $matched_args = {};
150 20 for my $field (@optional_fields) {
151 140 ▶ next unless ($args->{$field} && $entry->{$field} && $entry->{$field}->@*);
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$field} and $entry->{$field} and @{$entry->{$field}} 140 0 140
Condition33%2/6
$args->{$field} and $entry->{$field} and @{$entry->{$field}}
!ll&&!rl&&r
14000
$args->{$field} and $entry->{$field}
!ll&&!rl&&r
14000
MC/DC0%0/3
$args->{$field} and $entry->{$field} and @{$entry->{$field}}
$args->{$field} $entry->{$field} @{$entry->{$field}}
Truth table25%1/4
A and B and C
A= $args->{$field}B= $entry->{$field}C= @{$entry->{$field}}
ABCresult
0XX0
10X0
1100
1111
152  
153 0 ▶ return undef unless any { $args->{$field} eq $_ } $entry->{$field}->@*;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $args->{$field} eq $_; } , @{$$entry{$field}}) 0 0 0
154 0 $matched_args->{$field} = $args->{$field};
155 }
156  
157 20 return $matched_args;
158 }
159  
160 =head2 get_sanctioned_info
161  
162 Tries to find a match a sanction entry matching the input profile args.
163 It takes arguments in two forms. In the new API, it takes a hashref containing the following named arguments:
164  
165 =over 4
166  
167 =item * first_name: first name
168  
169 =item * last_name: last name
170  
171 =item * date_of_birth: (optional) date of birth as a string or epoch
172  
173 =item * place_of_birth: (optional) place of birth as a country name or code
174  
175 =item * residence: (optional) name or code of the country of residence
176  
177 =item * nationality: (optional) name or code of the country of nationality
178  
179 =item * citizen: (optional) name or code of the country of citizenship
180  
181 =item * postal_code: (optional) postal/zip code
182  
183 =item * national_id: (optional) national ID number
184  
185 =item * passport_no: (oiptonal) passort number
186  
187 =back
188  
189 For backward compatibility it also supports the old API, taking the following args:
190  
191 =over 4
192  
193 =item * first_name: first name
194  
195 =item * last_name: last name
196  
197 =item * date_of_birth: (optional) date of birth as a string or epoch
198  
199 =back
200  
201 It returns a hash-ref containg the following data:
202  
203 =over 4
204  
205 =item - matched: 1 if a match was found; 0 otherwise
206  
207 =item - list: the source for the matched entry,
208  
209 =item - matched_args: a name-value hash-ref of the similar arguments,
210  
211 =item - comment: additional comments if necessary,
212  
213 =back
214  
215 =cut
216  
217 14 ▶sub get_sanctioned_info { ## no critic (RequireArgUnpacking)
Subroutine get_sanctioned_info: called CC=27 SCAR=34.0
Pod documented
218 14 ▶ my $self = blessed($_[0]) ? shift : $instance;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 0 14 14
219 14 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 5 9 14
220 5 $instance = __PACKAGE__->new(sanction_file => get_sanction_file());
221 5 $self = $instance;
222 }
223  
224 # It's the old interface
225 14 my ($first_name, $last_name, $date_of_birth) = @_;
226 14 my $args = {};
227  
228 # in the new interface we accept fields in a hashref
229 14 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 0 14 14
230 0 ($args) = @_;
231 0 ($first_name, $last_name, $date_of_birth) = $args->@{qw/first_name last_name date_of_birth/};
232 }
233  
234 # convert country names to iso codes
235 14 for my $field (qw/place_of_birth residence nationality citizen/) {
236 56 my $value = $args->{$field};
237 56 ▶ next unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 56 0 56
238  
239 0 $args->{$field} = Data::Validate::Sanctions::Fetcher::get_country_code($value);
240 }
241  
242 14 $self->_load_data();
243  
244 14 ▶ my $client_full_name = join(' ', $first_name, $last_name || ());
Condition66%2/3
$last_name || ()
l!l&&r!l&&!r
1220
MC/DC0%0/2
$last_name || ()
$last_name ()
Truth table66%2/3
A || B
A= $last_nameB= ()
ABresult
1X1
011
000
245  
246 # Split into tokens after cleaning
247 14 my @client_name_tokens = _clean_names($client_full_name);
248  
249 14 my @match_with_dob_text;
250  
251 # only pick the sanctioned names which have common token with the client tokens
252 # and deduplicate the list
253 14 my $filtered_sanctioned_names = {};
254 14 foreach my $token (@client_name_tokens) {
255 29 foreach my $name (keys %{$self->{_token_sanctioned_names}->{$token}}) {
256 313 $filtered_sanctioned_names->{$name} = 1;
257 }
258 }
259  
260 14 foreach my $sanctioned_name (keys %{$filtered_sanctioned_names}) {
261 119 my $sanctioned_name_tokens = $self->{_sanctioned_name_tokens}->{$sanctioned_name};
262 119 ▶ next unless _name_matches(\@client_name_tokens, $sanctioned_name_tokens);
Branch: 1 branch
BranchTrueFalseTotal
unless _name_matches(\@client_name_tokens, $sanctioned_name_tokens) 103 16 119
263  
264 16 for my $entry ($self->{_index}->{$sanctioned_name}->@*) {
265 20 my $matched_args = $self->_match_other_fields($entry, $args);
266 20 ▶ next unless $matched_args;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched_args 0 20 20
267 20 $matched_args->{name} = $sanctioned_name;
268  
269 # dob is matched only if it's included in lookup args
270 20 ▶ return _possible_match($entry->{source}, \%$matched_args) unless defined $date_of_birth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date_of_birth 5 15 20
271  
272 # 1- Some entries in sanction list can have more than one date of birth
273 # 2- first epoch is compared, then year
274 15 my $client_dob_date = Date::Utility->new($date_of_birth);
275 15 $args->{dob_epoch} = $client_dob_date->epoch;
276 15 $args->{dob_year} = $client_dob_date->year;
277  
278 15 for my $dob_field (qw/dob_epoch dob_year/) {
279 29 ▶ $entry->{$dob_field} //= [];
Condition100%2/2
$entry->{$dob_field} //= []
l!l
209
MC/DC100%1/1
$entry->{$dob_field} //= []
$entry->{$dob_field}
Truth table100%2/2
A
A= $entry->{$dob_field}
Aresult
11
00
280 29 ▶ my $checked_dob = any { $_ eq $args->{$dob_field} } $entry->{$dob_field}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
281  
282 29 ▶ return _possible_match($entry->{source}, {%$matched_args, $dob_field => $args->{$dob_field}}) if $checked_dob;
Branch: 1 branch
BranchTrueFalseTotal
if $checked_dob 2 27 29
283 }
284  
285 # Saving names with dob_text for later check.
286 13 ▶ my $has_no_epoch_or_year = ($entry->{dob_epoch}->@* || $entry->{dob_year}->@*) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$entry->{'dob_epoch'}} || @{$entry->{'dob_year'}} ? : 12 1 13
Condition100%3/3
@{$entry->{"dob_epoch"}} or @{$entry->{"dob_year"}}
l!l&&r!l&&!r
1111
MC/DC100%2/2
@{$entry->{"dob_epoch"}} or @{$entry->{"dob_year"}}
@{$entry->{"dob_epoch"}} @{$entry->{"dob_year"}}
Truth table100%3/3
A or B
A= @{$entry->{"dob_epoch"}}B= @{$entry->{"dob_year"}}
ABresult
1X1
011
000
287 13 ▶ my $has_dob_text = @{$entry->{dob_text} // []} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$entry->{'dob_text'} // [];} ? : 0 13 13
Condition50%1/2
$entry->{"dob_text"} // []
l!l
013
MC/DC0%0/1
$entry->{"dob_text"} // []
$entry->{"dob_text"}
Truth table50%1/2
A
A= $entry->{"dob_text"}
Aresult
11
00
288 13 ▶ if ($has_dob_text || $has_no_epoch_or_year) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_dob_text or $has_no_epoch_or_year) 1 12 13
Condition66%2/3
$has_dob_text or $has_no_epoch_or_year
l!l&&r!l&&!r
0112
MC/DC50%1/2
$has_dob_text or $has_no_epoch_or_year
$has_dob_text $has_no_epoch_or_year
Truth table66%2/3
A or B
A= $has_dob_textB= $has_no_epoch_or_year
ABresult
1X1
011
000
289 1 push @match_with_dob_text,
290 {
291 name => $sanctioned_name,
292 entry => $entry,
293 matched_args => $matched_args,
294 };
295 }
296 }
297 }
298  
299 # Return a possible match if the name matches and no date of birth is present in sanctions
300 7 for my $match (@match_with_dob_text) {
301 # We match only in case we have full match for the name
302 # in other case we may get to many false positive
303 1 my ($sanction_name, $client_name) = map { uc(s/[^[:alpha:]\s]//gr) } ($match->{name}, $client_full_name);
304  
305 1 ▶ next unless $sanction_name eq $client_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $sanction_name eq $client_name 0 1 1
306  
307 1 ▶ my $dob_text = $match->{entry}->{dob_text} // [];
Condition50%1/2
$match->{"entry"}{"dob_text"} // []
l!l
01
MC/DC0%0/1
$match->{"entry"}{"dob_text"} // []
$match->{"entry"}{"dob_text"}
Truth table50%1/2
A
A= $match->{"entry"}{"dob_text"}
Aresult
11
00
308  
309 1 my $comment;
310 1 ▶ if (@$dob_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$dob_text) 0 1 1
311 0 $comment = 'dob raw text: ' . join q{, } => @$dob_text;
312 }
313  
314 1 return _possible_match($match->{entry}->{source}, $match->{matched_args}, $comment);
315 }
316  
317 # Return if no possible match, regardless if date of birth is provided or not
318 6 return {matched => 0};
319 }
320  
321 14 ▶sub _load_data {
Subroutine _load_data: called CC=14 SCAR=27.1
322 14 my $self = shift;
323 14 my $sanction_file = $self->{sanction_file};
324 14 ▶ $self->{last_modification} //= 0;
Condition50%1/2
$self->{"last_modification"} //= 0
l!l
140
MC/DC0%0/1
$self->{"last_modification"} //= 0
$self->{"last_modification"}
Truth table50%1/2
A
A= $self->{"last_modification"}
Aresult
11
00
325 14 ▶ $self->{last_index} //= 0;
Condition50%1/2
$self->{"last_index"} //= 0
l!l
140
MC/DC0%0/1
$self->{"last_index"} //= 0
$self->{"last_index"}
Truth table50%1/2
A
A= $self->{"last_index"}
Aresult
11
00
326 14 ▶ $self->{last_data_load} //= 0;
Condition50%1/2
$self->{"last_data_load"} //= 0
l!l
140
MC/DC0%0/1
$self->{"last_data_load"} //= 0
$self->{"last_data_load"}
Truth table50%1/2
A
A= $self->{"last_data_load"}
Aresult
11
00
327 14 ▶ $self->{_data} //= {};
Condition100%2/2
$self->{"_data"} //= {}
l!l
95
MC/DC100%1/1
$self->{"_data"} //= {}
$self->{"_data"}
Truth table100%2/2
A
A= $self->{"_data"}
Aresult
11
00
328 14 ▶ $self->{_sanctioned_name_tokens} //= {};
Condition100%2/2
$self->{"_sanctioned_name_tokens"} //= {}
l!l
95
MC/DC100%1/1
$self->{"_sanctioned_name_tokens"} //= {}
$self->{"_sanctioned_name_tokens"}
Truth table100%2/2
A
A= $self->{"_sanctioned_name_tokens"}
Aresult
11
00
329 14 ▶ $self->{_token_sanctioned_names} //= {};
Condition100%2/2
$self->{"_token_sanctioned_names"} //= {}
l!l
95
MC/DC100%1/1
$self->{"_token_sanctioned_names"} //= {}
$self->{"_token_sanctioned_names"}
Truth table100%2/2
A
A= $self->{"_token_sanctioned_names"}
Aresult
11
00
330  
331 14 ▶ return $self->{_data} if $self->{_data} and $self->{last_data_load} + $self->IGNORE_OPERATION_INTERVAL > time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_data"} and $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time 9 5 14
Condition66%2/3
$self->{"_data"} and $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
!ll&&!rl&&r
059
MC/DC50%1/2
$self->{"_data"} and $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
$self->{"_data"} $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
Truth table66%2/3
A and B
A= $self->{"_data"}B= $self->{"last_data_load"} + $self->IGNORE_OPERATION_INTERVAL > time
ABresult
0X0
100
111
332  
333 5 ▶ if (-e $sanction_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $sanction_file) 5 0 5
334 5 my $file_modify_time = stat($sanction_file)->mtime;
335 5 ▶ return $self->{_data} if $file_modify_time <= $self->{last_modification} && $self->{_data};
Branch: 1 branch
BranchTrueFalseTotal
if $file_modify_time <= $self->{"last_modification"} and $self->{"_data"} 0 5 5
Condition33%1/3
$file_modify_time <= $self->{"last_modification"} and $self->{"_data"}
!ll&&!rl&&r
500
MC/DC0%0/2
$file_modify_time <= $self->{"last_modification"} and $self->{"_data"}
$file_modify_time <= $self->{"last_modification"} $self->{"_data"}
Truth table33%1/3
A and B
A= $file_modify_time <= $self->{"last_modification"}B= $self->{"_data"}
ABresult
0X0
100
111
336 5 $self->{last_modification} = $file_modify_time;
337 5 $self->{_data} = LoadFile($sanction_file);
338 5 $self->{last_data_load} = time;
339 }
340  
341 5 $self->_index_data();
342  
343 5 foreach my $sanctioned_name (keys $self->{_index}->%*) {
344 23102 my @tokens = _clean_names($sanctioned_name);
345 23102 $self->{_sanctioned_name_tokens}->{$sanctioned_name} = \@tokens;
346 23102 foreach my $token (@tokens) {
347 65499 $self->{_token_sanctioned_names}->{$token}->{$sanctioned_name} = 1;
348 }
349 }
350  
351 5 return $self->{_data};
352 }
353  
354 =head2 _index_data
355  
356 Indexes data by name. Each name may have multiple matching entries.
357  
358 =cut
359  
360 5 ▶sub _index_data {
Subroutine _index_data: called CC=7 SCAR=19.5
361 5 my $self = shift;
362  
363 5 ▶ $self->{_data} //= {};
Condition50%1/2
$self->{"_data"} //= {}
l!l
50
MC/DC0%0/1
$self->{"_data"} //= {}
$self->{"_data"}
Truth table50%1/2
A
A= $self->{"_data"}
Aresult
11
00
364 5 $self->{_index} = {};
365 5 for my $source (keys $self->{_data}->%*) {
366 8 ▶ my @content = clone($self->{_data}->{$source}->{content} // [])->@*;
Condition100%2/2
$self->{"_data"}{$source}{"content"} // []
l!l
71
MC/DC100%1/1
$self->{"_data"}{$source}{"content"} // []
$self->{"_data"}{$source}{"content"}
Truth table100%2/2
A
A= $self->{"_data"}{$source}{"content"}
Aresult
11
00
367  
368 8 for my $entry (@content) {
369 15668 $entry->{source} = $source;
370 15668 for my $name ($entry->{names}->@*) {
371 26569 $name = ucfirst($name);
372 26569 ▶ my $entry_list = $self->{_index}->{$name} // [];
Condition100%2/2
$self->{"_index"}{$name} // []
l!l
346723102
MC/DC100%1/1
$self->{"_index"}{$name} // []
$self->{"_index"}{$name}
Truth table100%2/2
A
A= $self->{"_index"}{$name}
Aresult
11
00
373 26569 push @$entry_list, $entry;
374 26569 $self->{_index}->{$name} = $entry_list;
375 }
376 }
377 }
378  
379 5 $self->{last_index} = time;
380  
381 5 return;
382 }
383  
384 0 ▶sub _save_data {
Subroutine _save_data: not called CC=2 SCAR=17.9
385 0 my $self = shift;
386  
387 0 my $sanction_file = $self->{sanction_file};
388 0 my $new_sanction_file = $sanction_file . ".tmp";
389  
390 0 DumpFile($new_sanction_file, $self->{_data});
391  
392 0 ▶ rename $new_sanction_file, $sanction_file or die "Can't rename $new_sanction_file to $sanction_file, please check it\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $new_sanction_file, $sanction_file or die "Can't rename $new_sanction_file to $sanction_file, please check it\n" 0 0 0
393 0 $self->{last_modification} = stat($sanction_file)->mtime;
394 0 return;
395 }
396  
397 2 ▶sub _default_sanction_file {
Subroutine _default_sanction_file: called CC=2 SCAR=9.2
398 2 ▶ return $ENV{SANCTION_FILE} // File::ShareDir::dist_file('Data-Validate-Sanctions', 'sanctions.yml');
Condition33%1/3
$ENV{"SANCTION_FILE"} // File::ShareDir::dist_file("Data-Validate-Sanctions", "sanctions.yml")
l!l&&r!l&&!r
200
MC/DC0%0/2
$ENV{"SANCTION_FILE"} // File::ShareDir::dist_file("Data-Validate-Sanctions", "sanctions.yml")
$ENV{"SANCTION_FILE"} File::ShareDir::dist_file("Data-Validate-Sanctions", "sanctions.yml")
Truth table33%1/3
A // B
A= $ENV{"SANCTION_FILE"}B= File::ShareDir::dist_file("Data-Validate-Sanctions", "sanctions.yml")
ABresult
1X1
011
000
399 }
400  
401 8 ▶sub _possible_match {
Subroutine _possible_match: called CC=1 SCAR=0.0
402 8 my ($list, $matched_args, $comment) = @_;
403  
404 return +{
405 8 matched => 1,
406 list => $list,
407 matched_args => $matched_args,
408 comment => $comment,
409 };
410 }
411  
412 23116 ▶sub _clean_names {
Subroutine _clean_names: called CC=1 SCAR=0.0
413 23116 my ($full_name) = @_;
414  
415 # Remove non-alphabets
416 23116 my @cleaned_full_name = split " ", uc($full_name =~ s/[^[:alpha:]\s]//gr);
417  
418 23116 return @cleaned_full_name;
419 }
420  
421 119 ▶sub _name_matches {
Subroutine _name_matches: called CC=6 SCAR=17.9
422 119 my ($small_tokens_list, $bigger_tokens_list) = @_;
423  
424 119 my $name_matches_count = 0;
425  
426 119 foreach my $token (@$small_tokens_list) {
427 346 ▶ $name_matches_count++ if any { $_ eq $token } @$bigger_tokens_list;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $token; } , @$bigger_tokens_list) 135 211 346
428 }
429  
430 119 my $small_tokens_size = min(scalar(@$small_tokens_list), scalar(@$bigger_tokens_list));
431  
432 # - If more than one word matches, return it as possible match
433 # - Some sanctioned individuals have only one name (ex. Hamza); this should be returned as well
434 119 ▶ return 1 if ($name_matches_count > 1) || ($name_matches_count == 1 && $small_tokens_size == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $name_matches_count > 1 or $name_matches_count == 1 and $small_tokens_size == 1 16 103 119
Condition83%5/6
$name_matches_count > 1 or $name_matches_count == 1 and $small_tokens_size == 1
l!l&&r!l&&!r
142103
$name_matches_count == 1 and $small_tokens_size == 1
!ll&&!rl&&r
01032
MC/DC66%2/3
$name_matches_count > 1 or $name_matches_count == 1 and $small_tokens_size == 1
$name_matches_count > 1 $name_matches_count == 1 $small_tokens_size == 1
Truth table75%3/4
A or B and C
A= $name_matches_count > 1B= $name_matches_count == 1C= $small_tokens_size == 1
ABCresult
1XX1
0111
00X0
0100
435  
436 103 return 0;
437 }
438  
439 0 ▶sub export_data {
Subroutine export_data: not called CC=1 SCAR=6.9
Pod documented
440 0 my ($self, $path) = @_;
441  
442 0 return DumpFile($path, $self->{_data});
443 }
444  
445 9 1;
446 __END__
« Sanctions/Redis.pm ↑ Summary Sanctions/Fetcher.pm »