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

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