OxdPackages/OxdClient.pm

filter: Statementstmt 33.6% 36 / 107 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 47.8% 11 / 23 Podpod n/a totaltotal 30.1% 47 / 156 CC 14 SCAR 37.4
CC
14
Cov
27%
request
132.0
getResponseData
6.0
setResponseObject
6.0
CRAP 42.1
↑ Summary OxdPackages/OxdClientSocket.pm »
1 #!/usr/bin/perl
2 # OxdClient.pm, a number as an object
3  
4 package Attribute::Abstract;
5  
6 package OxdClient; # This is the "Class"
7
8 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION = '0.01';
10
11 1 ▶ use OxdPackages::OxdClientSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @ISA = qw(OxdClientSocket); # inherits from OxdClient
13 # makes all attributes available
14 1 ▶ use lib './modules';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use OxdPackages::OxdConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
23
24
25 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
26 0 my $class = shift;
27 0 my $self = {
28 # @var string $command Extend class protocol command name, for sending oxd-server
29 _command=>shift,
30
31 # @var string $params Extends class sending parameters to oxd-server
32 _params => [],
33
34 # @var string $data Response data from oxd-server
35 _data => [],
36
37 # @var string $response_json Response data from oxd-server in format json
38 _response_json=>shift,
39
40 # @var object $response_object Response data from oxd-server in format object
41 _response_object=>shift,
42
43 # @var string $response_status Response status from oxd-server
44 _response_status=>shift,
45
46 # @var array $response_data Response data from oxd-server in format array
47 _response_data => [],
48  
49
50 };
51 # Print all the values just for clarification.
52 #print "First Name is $self->{_firstName}\n";
53 0 bless $self, $class;
54
55 0 return $self;
56 }
57
58 # send function sends the command to the oxd server.
59 # Args:
60 # command (dict) - Dict representation of the JSON command string
61 # @return void
62 #
63 0 ▶ sub request{
Subroutine request: not called CC=11 SCAR=48.8
64 0 my ($self) = @_;
65
66 # @var array $command_types Protocols commands name
67 0 my @command_types = ("get_authorization_url",
68 "update_site_registration",
69 "get_tokens_by_code",
70 "get_user_info",
71 "register_site",
72 "get_logout_uri",
73 "get_authorization_code",
74 "uma_rs_protect",
75 "uma_rs_check_access",
76 "uma_rp_get_rpt",
77 "uma_rp_authorize_rpt",
78 "uma_rp_get_gat");
79
80
81
82 0 $self->setCommand();
83
84 0 my $exist = 'false';
85 0 ▶ for (my $i=0; $i <= scalar @command_types; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= scalar @command_types) 0 0 0
86 0 ▶ if ($command_types[$i] eq $self->getCommand()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_types[$i] eq $self->getCommand) 0 0 0
87 0 my $exist = 'true';
88 0 last;
89 }
90 }
91
92 0 ▶ if (!$exist) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exist) 0 0 0
93 0 $self->log('Command: ' . $self->getCommand() . ' is not exist!','Exiting process.');
94 0 $self->error_message('Command: ' . $self->getCommand() . ' is not exist!');
95 }
96
97 0 $self->setParams();
98
99
100 0 my $json_array = $self->getData();
101 0 my $json = JSON::PP->new;
102
103 0 my $jsondata = $json->encode($json_array);
104
105
106 0 ▶ if(!$self->is_JSON($jsondata)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_JSON($jsondata)) 0 0 0
107 0 $self->log("Sending parameters must be JSON.",'Exiting process.');
108 0 $self->error_message('Sending parameters must be JSON.');
109 }
110
111 0 my $lenght = length $jsondata;
112
113 0 ▶ if($lenght<=0){
Branch: 1 branch
BranchTrueFalseTotal
if ($lenght <= 0) { } 0 0 0
114 0 $self->log("Length must be more than zero.",'Exiting process.');
115 0 $self->error_message("Length must be more than zero.");
116 }else{
117 0 ▶ $lenght = $lenght <= 999 ? "0" . $lenght : $lenght;
Branch: 1 branch
BranchTrueFalseTotal
$lenght <= 999 ? : 0 0 0
118 }
119
120 0 my $lenght_jsondata = encode('UTF-8', $lenght . $jsondata);
121 0 my $response_json = $self->oxd_socket_request($lenght_jsondata);
122
123 0 my $char_count = substr($response_json, 0, 4);
124 0 $response_json =~ s/$char_count//g;
125 0 ▶ $self->{_response_json} = $response_json if defined($response_json);
Branch: 1 branch
BranchTrueFalseTotal
if defined $response_json 0 0 0
126
127 0 ▶ if ( $response_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_json) { } 0 0 0
128 0 my $object = JSON::PP->new->utf8->decode($response_json);
129 #print $object->{data}->{oxd_id};
130 0 ▶ if ($object->{status} eq 'error') {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->{'status'} eq 'error') { } 0 0 0
elsif ($object->{'status'} eq 'ok') { } 0 0 0
131 0 $self->error_message($object->{data}->{error} . ' : ' . $object->{data}->{error_description});
132 } elsif ($object->{status} eq 'ok') {
133 0 $self->setResponseObject( $object );
134 }
135 } else {
136 0 print "I am here";
137 0 $self->log("Response is empty...",'Exiting process.');
138 0 $self->error_message('Response is empty...');
139 }
140 }
141  
142
143 # Response status
144 # @return string, OK on success, error on failure
145 sub getResponseStatus
146 0 ▶ {
Subroutine getResponseStatus: not called CC=1 SCAR=6.9
147 0 my ($self) = @_;
148 0 return $self->{_response_status};
149 }
150  
151
152 # Setting response status
153 # @return void
154 sub setResponseStatus
155 0 ▶ {
Subroutine setResponseStatus: not called CC=1 SCAR=6.9
156 0 my ( $self) = @_;
157 0 $self->{_response_status} = $self->getResponseObject()->{status};
158 0 return $self->{_response_status};
159 }
160  
161
162 # If data is not empty it is returning response data from oxd-server in format array.
163 # If data empty or error , you have problem with parameter or protocol.
164 # @return array
165 0 ▶ sub getResponseData{
Subroutine getResponseData: not called CC=2 SCAR=17.9
166 0 my ($self) = @_;
167 0 ▶ if (!$self->getResponseObject()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->getResponseObject) { } 0 0 0
168 0 $self->{_response_data} = 'Data is empty';
169 0 $self->error_message($self->{_response_data});
170 } else {
171 0 $self->{_response_data} = $self->getResponseObject()->{data};
172 }
173 0 return $self->{_response_data};
174 }
175  
176 # Data which need to send oxd server.
177 # @return array
178 0 ▶ sub getData{
Subroutine getData: not called CC=1 SCAR=6.9
179 0 my ($self) = @_;
180
181 0 my $data = {
182 "command" => $self->getCommand(),
183 "params" => $self->getParams(),
184 };
185
186 #my @data = ('command' => $self->getCommand(), 'params' => $self->getParams());
187 0 return $data;
188 }
189  
190
191 # Protocol name for request.
192 # @return string
193
194 0 ▶ sub getCommand{
Subroutine getCommand: not called CC=1 SCAR=6.9
195 0 my ($self) = @_;
196 0 return $self->{_command};
197 #return 'register_site';
198 }
199  
200
201 # Setting protocol name for request.
202 # @return void
203 #sub setCommand : Abstract;
204
205 # If response data is not empty it is returning response data from oxd-server in format object.
206 # If response data empty or error , you have problem with parameter or protocol.
207 #
208 # @return object
209
210 0 ▶ sub setResponseObject{
Subroutine setResponseObject: not called CC=2 SCAR=17.9
211 0 my ( $self, $response_object ) = @_;
212 0 ▶ $self->{_response_object} = $response_object if defined($response_object);
Branch: 1 branch
BranchTrueFalseTotal
if defined $response_object 0 0 0
213 0 return $self->{_response_object};
214 }
215
216
217 # If response data is not empty it is returning response data from oxd-server in format object.
218 # If response data empty or error , you have problem with parameter or protocol.
219 #
220 # @return object
221
222 0 ▶ sub getResponseObject{
Subroutine getResponseObject: not called CC=1 SCAR=6.9
223 0 my ($self) = @_;
224 0 return $self->{_response_object};
225 }
226  
227
228 # If response data is not empty it is returning response data from oxd-server in format json.
229 # If response data empty or error , you have problem with parameter or protocol.
230 # @return string
231
232 0 ▶ sub getResponseJSON{
Subroutine getResponseJSON: not called CC=1 SCAR=6.9
233 0 my ($self) = @_;
234 0 return $self->{_response_json};
235 }
236  
237
238 # Setting parameters for request.
239 # @return void
240 #sub setParams : Abstract;
241
242
243 # Parameters for request.
244 # @return array
245 0 ▶ sub getParams{
Subroutine getParams: not called CC=1 SCAR=6.9
246 0 my ($self) = @_;
247 0 return $self->{_params};
248 }
249  
250
251 # Checking format string.
252 # @param string $string
253 # @return bool
254 0 ▶ sub is_JSON{
Subroutine is_JSON: not called CC=2 SCAR=17.9
255 # Get passed arguments
256 0 my($self,$jsondata) = @_;
257 0 my $json_out = eval { decode_json($jsondata) };
258 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
259 #print "Error: $@";
260 0 return 0;
261 }else{
262 0 return 1;
263 #print "OK!\n";
264 }
265 }
266  
267 1 1; # this 1; is neccessary for our class to work
↑ Summary OxdPackages/OxdClientSocket.pm »