blib/lib/Finance/BankVal/International/IBANValidate.pm

filter: Statementstmt 86.0% 86 / 100 Branchbran 75.0% 27 / 36 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 7 totaltotal 71.7% 127 / 177 CC 23 SCAR 16.8
CC
23
Cov
78%
goFallOver
12.0
loadUidPin
7.7
validateFormat
7.0
CRAP 5.4
↑ Summary
1 package Finance::BankVal::International::IBANValidate;
2
3 1 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use vars qw($size $format $iban $userid $pin $error $ua $url);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 require Exporter;
13 1 our @ISA = qw(Exporter);
14 1 our @EXPORT_OK = qw(ibanValidate new);
15 1 our $VERSION = '0.05';
16
17 1 my $format; #response return format i.e. xml json csv
18 my $iban; #the IBAN to be validated
19 1 my $userid;
20 1 my $pin;
21 1 my $size; #the number of parameters passed only 2 and 4 are valid
22 1 my $error; #any error messages generated here, not from unifieds servers
23 1 my $responseString; #the return value of ibanValidate either $errors or web service response
24 1 my $ua;
25 1 my $url;
26
27 #constructor
28 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my $proto = shift;
30 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
31 0 my $self = {};
32 0 bless ($self, $class);
33 0 return $self;
34 }
35
36 # exportable sub takes parameter array of 2 or 4 elements
37 # see perldoc for this module for more details
38 # ibanValidate($format,$iban,$userid,pin);
39 # or ibanValidate($format,$iban);
40 18 ▶sub ibanValidate{
Subroutine ibanValidate: called CC=4 SCAR=13.9
Pod undocumented
41 18 $error="";
42 18 my @params = @_;
43 18 $size = @params;
44 #the following block checks to see if the first param is a reference
45 #if it is then the sub was called as an object ref so size is reduced
46 #accordingly
47 18 my $refCheck = shift @_; #remove the leftmost array element
48 18 ▶ if (ref($refCheck)){ #check if its a reference
Branch: 1 branch
BranchTrueFalseTotal
if (ref $refCheck) { } 0 18 18
49 0 $size--; #if it is reduce the size value to account for it
50 }else{ #otherwise
51 18 unshift(@_, $refCheck); #put it back
52 }
53 18 $format = lc($_[0]);
54 18 $iban = $_[1];
55 18 ▶ if ($size > 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 2) { } 15 3 18
56 15 $userid = $_[2];
57 15 $pin = $_[3];
58 }else{
59 3 &loadUidPin;
60 }
61 #all params should now be present so call validate formats sub
62 18 &validateFormat;
63 #if invalid formats are found return error message
64 18 ▶ if ($error){
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 14 4 18
65 14 $responseString = "$error";
66 14 &formatErrorMsg;
67 14 return $responseString;
68 }
69 #if all formats are ok call web service sub then return response
70 4 &goValidate;
71 4 return $responseString;
72
73 }
74
75 4 ▶sub goValidate{
Subroutine goValidate: called CC=3 SCAR=12.0
Pod undocumented
76 #create user agent
77 4 $ua = LWP::UserAgent->new();
78
79 #build the URL
80 4 my $baseUrl = 'https://www.unifiedsoftware.co.uk/services/bankvalint/ibanvalidator';
81 4 $url = "$baseUrl/userid/$userid/pin/$pin/iban/$iban/$format/";
82
83 #call the service
84 4 my $response = $ua->get($url);
85
86 #Check the response code if its fail call backup server sub
87 4 ▶ if($response->code<200||$response->code>399){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code < 200 or $response->code > 399) { } 0 4 4
Condition33%1/3
$response->code < 200 or $response->code > 399
l!l&&r!l&&!r
004
MC/DC0%0/2
$response->code < 200 or $response->code > 399
$response->code < 200 $response->code > 399
Truth table33%1/3
A or B
A= $response->code < 200B= $response->code > 399
ABresult
1X1
011
000
88 0 &goFallOver;
89 } else {
90 4 $responseString = $response->content();
91 }
92 }
93
94 0 ▶sub goFallOver{
Subroutine goFallOver: not called CC=3 SCAR=24.8
Pod undocumented
95 #build the URL
96 0 my $baseUrl = 'https://www.unifiedservices.co.uk/services/bankvalint/ibanvalidator';
97 0 $url = "$baseUrl/userid/$userid/pin/$pin/iban/$iban/$format/";
98
99 #call the service
100 0 my $response = $ua->get($url);
101
102 #Check the response code
103 0 ▶ if($response->code<200||$response->code>399){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code < 200 or $response->code > 399) { } 0 0 0
Condition0%0/3
$response->code < 200 or $response->code > 399
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->code < 200 or $response->code > 399
$response->code < 200 $response->code > 399
Truth table0%0/3
A or B
A= $response->code < 200B= $response->code > 399
ABresult
1X1
011
000
104 0 $responseString .= $response->code;
105 } else {
106 0 $responseString = $response->content();
107 }
108 }
109
110 18 ▶sub validateFormat{
Subroutine validateFormat: called CC=7 SCAR=19.5
Pod undocumented
111 #Validate response format must match json, xml, or, csv
112 18 ▶ if ($format !~ /^json$|^xml$|^csv$/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($format =~ /^json$|^xml$|^csv$/) 1 17 18
113 1 $error .= "INVALID - Result Format";
114 1 return;
115 }
116 #Validate IBAN up to 34 chars A-Z 0-9
117 17 ▶ if ($iban !~ /^[A-Z,0-9]{1,34}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iban =~ /^[A-Z,0-9]{1,34}$/) 4 13 17
118 4 $error .= "INVALID - FORMAT";
119 4 return;
120 }
121 #Validate PIN all numeric 5 characters
122 13 ▶ if ($pin){
Branch: 1 branch
BranchTrueFalseTotal
if ($pin) 13 0 13
123 13 ▶ if ($pin !~ /^\d\d\d\d\d$/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin =~ /^\d\d\d\d\d$/) 6 7 13
124 6 $error .= "ERROR -Invalid User ID/PIN";
125 6 return;
126 }
127 }
128 #Validate UID must end with 3 numerics exactly and start with 3 Alpha variable length otherwise
129 7 ▶ if ($userid){
Branch: 1 branch
BranchTrueFalseTotal
if ($userid) 7 0 7
130 7 ▶ if ($userid !~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/){
Branch: 1 branch
BranchTrueFalseTotal
if (not $userid =~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/) 3 4 7
131 3 $error .= "ERROR -Invalid User ID/PIN";
132 3 return;
133 }
134 }
135 }
136
137 3 ▶sub loadUidPin {
Subroutine loadUidPin: called CC=7 SCAR=20.4
Pod undocumented
138 3 my $fileOpened = open UIDCONF, "InternationalLoginConfig.txt";
139 3 ▶ if ( ! $fileOpened){
Branch: 1 branch
BranchTrueFalseTotal
if (not $fileOpened) { } 0 3 3
140 0 $error .= "No UserID / PIN supplied, please visit http://www.unifiedsoftware.co.uk/freetrial/free-trial-home.html: ";
141 }else{
142 3 ▶ while (<UIDCONF>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline UIDCONF)) 6 3 9
143 6 ▶ if ($_ =~ /^UserID/){
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ /^UserID/) { } 3 3 6
elsif ($_ =~ /^PIN/) { } 3 0 3
144 3 chomp(my @line = split (/ |\t/,$_));
145 3 $userid = $line[-1];
146 }elsif($_ =~ /^PIN/){
147 3 chomp(my @line = split (/ |\t/,$_));
148 3 $pin = $line[-1];
149 }
150
151 }
152 #check to see if conf file has empty params - if so return error message directing to free trial page
153 3 ▶ if (($userid !~ /\w/) || ($pin !~ /\w/)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($userid =~ /\w/ and $pin =~ /\w/) 3 0 3
Condition33%1/3
$userid =~ /\w/ and $pin =~ /\w/
!ll&&!rl&&r
300
MC/DC0%0/2
$userid =~ /\w/ and $pin =~ /\w/
$userid =~ /\w/ $pin =~ /\w/
Truth table33%1/3
A and B
A= $userid =~ /\w/B= $pin =~ /\w/
ABresult
0X0
100
111
154 3 $error .= "No User ID / PIN supplied, please visit http://www.unifiedsoftware.co.uk/freetrial/free-trial-home.html: ";
155 }
156 3 close UIDCONF;
157 }
158 }
159
160 14 ▶sub formatErrorMsg{
Subroutine formatErrorMsg: called CC=3 SCAR=11.0
Pod undocumented
161 14 ▶ if($format eq "xml"){
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 3 11 14
elsif ($format eq 'json') { } 1 10 11
162 3 $responseString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><iban><result>" . $responseString . "</result></iban>";
163 }elsif($format eq "json"){
164 1 $responseString = "{\"result\":\"" . $responseString . "\"}";
165 }
166 }
167
168 1 1;
169 __END__
↑ Summary