blib/lib/Finance/BankVal/UK.pm

filter: Statementstmt 46.0% 52 / 113 Branchbran 11.7% 4 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 8 totaltotal 32.8% 65 / 198 CC 24 SCAR 30.9
CC
24
Cov
34%
loadUidPin
56.0
validateFormat
36.7
goValidate
12.0
CRAP 22.0
↑ Summary
1 package Finance::BankVal::UK;
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 1 ▶use vars qw(@params $size $account $error $sortcode $uid $pin &responseString $ua);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT_OK = qw(bankValUK new);
13 1 our $VERSION = '0.9';
14  
15 1 my $sortcode; #sortcode to be validated
16 my $account; #account number to be validated
17 1 my $uid; #userID
18 1 my $pin; #PIN
19 1 my $size; #holds length of param array
20 1 my $url; #the URL built for the REST call
21 1 my $responseString; #the return to the calling method
22 1 my $error; #holds any error messages etc from the module, erors from the web servoce will be returned in $response string
23 1 my $json; #holds the data to call the service with
24  
25 #constructor
26 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my $proto = shift;
28 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
29 0 my $self = {};
30 0 bless( $self, $class );
31 0 return $self;
32 }
33  
34 #
35 # Exportable sub can take parameter array of 4 or 2 elements
36 # these must be in the order detailed in the perldoc for this module
37 #
38 1 ▶sub bankValUK {
Subroutine bankValUK: called CC=6 SCAR=24.5
Pod undocumented
39 1 $error = "";
40 1 my @params = @_;
41 1 $size = @params;
42  
43 #the following block checks to see if the first param is a reference
44 #if it is then the sub was called as an object ref so size is reduced
45 #accordingly
46 #my $refCheck = shift @_; #remove the leftmost array element
47 #if ( ref($refCheck) ) { #check if its a reference
48 # $size--; #if it is reduce the size value to account for it
49 # }
50 # else { #otherwise
51 # unshift( @_, $refCheck ); #put it back
52 # }
53 1 $sortcode = $_[0]; #set the sortcode
54  
55 #strip sortcode of seperating - or spaces
56 1 $sortcode =~ s/-| //g;
57 1 print STDOUT $sortcode;
58 #Switch to handle different amount of parameters
59 SWITCH: {
60 1 ▶ $size == 4 && do {
Branch: 1 branch
BranchTrueFalseTotal
$size == 4 and do { $account = $_[1]; $uid = $_[2]; $pin = $_[3]; last SWITCH } 1 0 1
61 1 $account = $_[1];
62 1 $uid = $_[2];
63 1 $pin = $_[3];
64 1 last SWITCH;
65 };
66 0 ▶ $size == 3 && do { #todo this is now an error with only sortcode
Branch: 1 branch
BranchTrueFalseTotal
$size == 3 and do { $uid = $_[1]; $pin = $_[2]; last SWITCH } 0 0 0
67 0 $uid = $_[1];
68 0 $pin = $_[2];
69 0 last SWITCH;
70 };
71 0 ▶ $size == 2 && do {
Branch: 1 branch
BranchTrueFalseTotal
$size == 2 and do { $account = $_[1]; &loadUidPin; last SWITCH } 0 0 0
72 0 $account = $_[1];
73 0 &loadUidPin;
74 0 last SWITCH;
75 };
76 0 ▶ $size == 1 && do { #todo this is now an error with only sortcode
Branch: 1 branch
BranchTrueFalseTotal
$size == 1 and do { &loadUidPin; last SWITCH } 0 0 0
77 0 &loadUidPin;
78 0 last SWITCH;
79 };
80 }
81  
82 #call validation sub now all elements are loaded
83 1 &validateFormat;
84  
85 1 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 0 1
86 1 $responseString = "$error";
87 1 &formatErrorMsg;
88 1 return $responseString;
89 }
90  
91 #call validation sub
92 0 &goValidate;
93 0 print STDOUT $responseString;
94 0 return $responseString;
95 }
96  
97 #call main servers REST services passing url with relevant parameters
98 0 ▶sub goValidate {
Subroutine goValidate: not called CC=3 SCAR=24.8
Pod undocumented
99 0 $responseString = "";
100 #create user agent
101 0 local $ua = LWP::UserAgent->new();
102  
103 #set the URL
104 0 $url = 'https://www.unifiedservices.co.uk/services/enhanced/bankval';
105  
106 #call the service
107 0 &loadContent;
108 0 my $req = HTTP::Request->new( 'POST', $url );
109 0 $req->header( 'Content-Type' => 'application/json' );
110 0 $req->content($json);
111 0 my $response = $ua->request($req);
112  
113 #Check the response code anything under 200 or over 399 is an error with main server try backup
114 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
115 0 &goFallOver;
116 }
117 else {
118 0 $responseString = $response->content(); #otherwise return the returned
119 }
120 }
121  
122 #sub to call backup servers
123 0 ▶sub goFallOver {
Subroutine goFallOver: not called CC=3 SCAR=24.8
Pod undocumented
124  
125 #set the URL
126 0 $url = 'https://www.unifiedsoftware.co.uk/services/enhanced/bankval';
127  
128 #call the service
129 0 my $req = HTTP::Request->new( 'POST', $url );
130 0 $req->header( 'Content-Type' => 'application/json' );
131 0 $req->content($json);
132 0 my $response = $ua->request($req);
133  
134 #Check the response code
135 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
136 0 $responseString .=
137 $response->code; #still a problem so return error code
138 }
139 else {
140 0 $responseString =
141 $response->content(); #ok this time so return the returned
142 }
143 }
144  
145 #sub to validate the parameters input
146 1 ▶sub validateFormat {
Subroutine validateFormat: called CC=7 SCAR=36.0
Pod undocumented
147  
148 #Validate sortcode all numeric 6 chars (-'s and ws stripped in calling routine)
149 1 ▶ if ( $sortcode !~ /^\d\d\d\d\d\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sortcode =~ /^\d\d\d\d\d\d$/) 1 0 1
150 1 $error .= "INVALID - Sortcode";
151 1 return;
152 }
153  
154 #Validate account all numeric between 6 and 12 digits
155 0 ▶ if ( ( length($account) < 6 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $account < 6 or length $account > 12 or not $account =~ /^\d+\d$/) 0 0 0
Condition0%0/6
length $account < 6 or length $account > 12 or not $account =~ /^\d+\d$/
l!l&&r!l&&!r
000
length $account < 6 or length $account > 12
l!l&&r!l&&!r
000
MC/DC0%0/3
length $account < 6 or length $account > 12 or not $account =~ /^\d+\d$/
length $account < 6 length $account > 12 not $account =~ /^\d+\d$/
Truth table0%0/4
A or B or not(C)
A= length $account < 6B= length $account > 12C= not $account =~ /^\d+\d$/
ABCresult
1XX1
01X1
0011
0000
156 || ( length($account) > 12 )
157 || ( $account !~ /^\d+\d$/ ) )
158 {
159 0 $error .= "INVALID - Account";
160 0 return;
161 }
162  
163 #Validate PIN all numeric 5 characters
164 0 ▶ if ( $pin !~ /^\d\d\d\d\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin =~ /^\d\d\d\d\d$/) 0 0 0
165 0 $error .= "ERROR - Invalid User ID/PIN";
166 0 return;
167 }
168  
169 #Validate UID must end with 3 numerics exactly and start with 3 Alpha variable length otherwise
170 0 ▶ if ( $uid !~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $uid =~ /^[a-zA-Z\-_][a-zA-Z][a-zA-Z]*\D\d\d\d$/) 0 0 0
171 0 $error .= "ERROR - Invalid User ID/PIN";
172 0 return;
173 }
174 }
175  
176 #sub to load PIN and UserID from LoginConfig.txt file if they weren't passed in with the method call
177 #returns an error if unsuccessful
178 0 ▶sub loadUidPin {
Subroutine loadUidPin: not called CC=7 SCAR=40.3
Pod undocumented
179 0 my $fileOpened = open UIDCONF, "LoginConfig.txt";
180 0 ▶ if ( !$fileOpened ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fileOpened) { } 0 0 0
181 0 $error .=
182 "No UserID / PIN supplied, please visit https://www.unifiedsoftware.co.uk/request-a-free-trial/: ";
183 }
184 else {
185 0 ▶ while (<UIDCONF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline UIDCONF)) 0 0 0
186 0 ▶ if ( $_ =~ /^UserID/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ /^UserID/) { } 0 0 0
elsif ($_ =~ /^PIN/) { } 0 0 0
187 0 chomp( my @line = split( / |\t/, $_ ) );
188 0 $uid = $line[-1];
189 }
190 elsif ( $_ =~ /^PIN/ ) {
191 0 chomp( my @line = split( / |\t/, $_ ) );
192 0 $pin = $line[-1];
193 }
194  
195 }
196  
197 #check to see if conf file has empty params - if so return error message directing to free trial page
198 0 ▶ if ( ( $uid !~ /\w/ ) || ( $pin !~ /\w/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid =~ /\w/ and $pin =~ /\w/) 0 0 0
Condition0%0/3
$uid =~ /\w/ and $pin =~ /\w/
!ll&&!rl&&r
000
MC/DC0%0/2
$uid =~ /\w/ and $pin =~ /\w/
$uid =~ /\w/ $pin =~ /\w/
Truth table0%0/3
A and B
A= $uid =~ /\w/B= $pin =~ /\w/
ABresult
0X0
100
111
199 0 $error .=
200 "No UserID / PIN supplied, please visit https://www.unifiedsoftware.co.uk/request-a-free-trial/: ";
201 }
202 0 close UIDCONF;
203 }
204 }
205  
206 0 ▶sub loadContent {
Subroutine loadContent: not called CC=1 SCAR=6.9
Pod undocumented
207 0 $json =
208 "{'credentials':{'uname':'$uid','pin':'$pin'},'account':{'account':'$account','sortcode':'$sortcode'}}";
209 }
210  
211 #sub to format the error message in the correct expected format with all nodes etc
212 1 ▶sub formatErrorMsg {
Subroutine formatErrorMsg: called CC=2 SCAR=8.1
Pod undocumented
213 1 ▶ if ( substr( $responseString, 0, 7 ) eq 'INVALID' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($responseString, 0, 7) eq 'INVALID') { } 1 0 1
214 1 $responseString =
215 "{\"validationID\": \"\",\"BankValUK\": {\"result\":\""
216 . $responseString . "\"}}";
217 }
218 else {
219 0 $responseString = "{\"Error\": \"Invalid Credentials\"}";
220 }
221  
222 }
223  
224 1 1;
225 __END__
↑ Summary