Elive/Connection.pm

filter: Statementstmt 80.0% 112 / 140 Branchbran 56.6% 34 / 60 Conditioncond 40.0% 14 / 35 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 86.3% 19 / 22 Podpod 100% 5 / 5 totaltotal 65.9% 188 / 285 CC 45 SCAR 29.9
CC
45
Cov
68%
_check_for_errors
49.5
call
20.0
_connect
18.9
CRAP 19.8
« Elive/View/Session.pm ↑ Summary Elive/DAO.pm »
1 package Elive::Connection;
2 22 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 22 our $VERSION = '1.36';
5  
6 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use File::Spec::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 require SOAP::Lite;
11 22 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use URI::Escape qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 ▶use parent qw{Class::Accessor Class::Data::Inheritable};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 ▶use Elive;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 22 ▶use Elive::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Elive::Connection - Manage Elluminate Live SOAP connections.
24  
25 =head1 DESCRIPTION
26  
27 This is an abstract class for managing connections and related resources.
28  
29 Most of the time, you'll be dealing with specific class instances; See L<Elive::Connection::SDK> L<Elive::Connection::API>.
30  
31 =cut
32  
33 22 __PACKAGE__->mk_accessors( qw{_url user pass _soap debug type timeout} );
34  
35 =head1 METHODS
36  
37 =cut
38  
39 =head2 connect
40  
41 my $sdk_c1 = Elive::Connection->connect('http://someserver.com/test',
42 'user1', 'pass1', debug => 1,
43 );
44  
45 my $url1 = $sdk_c1->url; # 'http://someserver.com/test'
46  
47 my $sdk_c2 = Elive::Connection->connect('http://user2:pass2@someserver.com/test');
48 my $url2 = $sdk_c2->url; # 'http://someserver.com/test'
49  
50 Establishes a logical SOAP connection.
51  
52 =cut
53  
54 sub connect {
55 0 ▶ my ($class, $url, $user, $pass, %opt) = @_;
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
56 #
57 # default connection - for backwards compatibility
58 #
59 0 require Elive::Connection::SDK;
60 0 return Elive::Connection::SDK->connect($url, $user => $pass, %opt);
61 }
62  
63 sub _connect {
64 5 ▶ my ($class, $url, $user, $pass, %opt) = @_;
Subroutine _connect: called CC=17 SCAR=29.4
65  
66 5 ▶ my $debug = $opt{debug}||0;
Condition50%1/2
$opt{'debug'} || 0
l!l
05
MC/DC0%0/1
$opt{'debug'} || 0
$opt{'debug'}
Truth table50%1/2
A
A= $opt{'debug'}
Aresult
11
00
67  
68 5 $url =~ s{/$}{}x;
69  
70 5 my $uri_obj = URI->new($url);
71  
72 5 my $userinfo = $uri_obj->userinfo;
73  
74 5 ▶ if ($userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo) 3 2 5
75  
76 #
77 # extract and remove any credentials from the url
78 #
79  
80 3 my ($uri_user, $uri_pass) = split(':',$userinfo, 2);
81  
82 3 ▶ if ($uri_user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_user) 3 0 3
83 3 ▶ if ($user && $user ne $uri_user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and $user ne $uri_user) { } 0 3 3
Condition33%1/3
$user and $user ne $uri_user
!ll&&!rl&&r
300
MC/DC0%0/2
$user and $user ne $uri_user
$user $user ne $uri_user
Truth table33%1/3
A and B
A= $userB= $user ne $uri_user
ABresult
0X0
100
111
84 0 carp 'ignoring user in URI scheme - overridden';
85 }
86 else {
87 3 $user = URI::Escape::uri_unescape($uri_user);
88 }
89 }
90  
91 3 ▶ if ($uri_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_pass) 3 0 3
92 3 ▶ if ($pass && $pass ne $uri_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and $pass ne $uri_pass) { } 0 3 3
Condition33%1/3
$pass and $pass ne $uri_pass
!ll&&!rl&&r
300
MC/DC0%0/2
$pass and $pass ne $uri_pass
$pass $pass ne $uri_pass
Truth table33%1/3
A and B
A= $passB= $pass ne $uri_pass
ABresult
0X0
100
111
93 0 carp 'ignoring pass in URI scheme - overridden';
94 }
95 else {
96 3 $pass = URI::Escape::uri_unescape($uri_pass);
97 }
98 }
99 }
100  
101 5 my $uri_path = $uri_obj->path;
102  
103 5 ▶ $pass = '' unless defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass 2 3 5
104  
105 5 my @path = File::Spec::Unix->splitdir($uri_path);
106  
107 5 ▶ shift (@path)
Branch: 1 branch
BranchTrueFalseTotal
if @path and not $path[0] 2 3 5
Condition66%2/3
@path and not $path[0]
!ll&&!rl&&r
302
MC/DC50%1/2
@path and not $path[0]
@path not $path[0]
Truth table66%2/3
A and not(B)
A= @pathB= not $path[0]
ABresult
0X0
100
111
108 if (@path && !$path[0]);
109  
110 5 ▶ pop (@path)
Branch: 1 branch
BranchTrueFalseTotal
if @path and $path[-1] eq "webservice.event" 2 3 5
Condition66%2/3
@path and $path[-1] eq "webservice.event"
!ll&&!rl&&r
302
MC/DC50%1/2
@path and $path[-1] eq "webservice.event"
@path $path[-1] eq "webservice.event"
Truth table66%2/3
A and B
A= @pathB= $path[-1] eq "webservice.event"
ABresult
0X0
100
111
111 if (@path && $path[-1] eq 'webservice.event');
112  
113 #
114 # normalise the connection url by removing suffixes. The following
115 # all reduce to http://mysite/myinst:
116 # -- http://mysite/myinst/webservice.event
117 # -- http://mysite/myinst/v2
118 # -- http://mysite/myinst/v2/webservice.event
119 # -- http://mysite/myinst/default
120 # -- http://mysite/myinst/default/webservice.event
121 #
122 # there's some ambiguity, an instance named v1 ... v9 will cause trouble!
123 #
124  
125 5 ▶ pop(@path)
Branch: 1 branch
BranchTrueFalseTotal
if @path and $path[-1] =~ /^v(\d+)$/ 1 4 5
Condition100%3/3
@path and $path[-1] =~ /^v(\d+)$/
!ll&&!rl&&r
311
MC/DC100%2/2
@path and $path[-1] =~ /^v(\d+)$/
@path $path[-1] =~ /^v(\d+)$/
Truth table100%3/3
A and B
A= @pathB= $path[-1] =~ /^v(\d+)$/
ABresult
0X0
100
111
126 if (@path && $path[-1] =~ m{^v(\d+)$});
127  
128 5 $uri_obj->path(File::Spec::Unix->catdir(@path));
129  
130 5 my $soap_url = $uri_obj->as_string;
131  
132 #
133 # remove any embedded credentials
134 #
135 5 ▶ $soap_url =~ s{\Q${userinfo}\E\@}{} if $userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if $userinfo 3 2 5
136  
137 5 my $self = {};
138 5 bless $self, $class;
139  
140 5 $self->url($soap_url);
141 5 $self->user($user);
142 5 $self->pass($pass);
143 5 $self->debug($debug);
144 5 $self->timeout($opt{timeout});
145  
146 5 return $self
147 }
148  
149 sub _check_for_errors {
150 98 ▶ my $class = shift;
Subroutine _check_for_errors: called CC=13 SCAR=39.0
151 98 my $som = shift;
152  
153 98 ▶ die "No response from server\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $som 0 98 98
154 unless $som;
155  
156 98 ▶ die $som->fault->{ faultstring }."\n" if ($som->fault);
Branch: 1 branch
BranchTrueFalseTotal
if $som->fault 0 98 98
157  
158 98 my $result = $som->result;
159 98 my @paramsout = $som->paramsout;
160  
161 98 ▶ warn YAML::Syck::Dump({result => $result, paramsout => \@paramsout})
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 0 98 98
162 if ($class->debug);
163  
164 98 my @results = ($result, @paramsout);
165  
166 98 foreach my $result (@results) {
167 196 ▶ next unless Scalar::Util::reftype($result);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($result) 105 91 196
168
169 #
170 # Look for Elluminate-specific errors
171 #
172 91 ▶ if ($result->{Code}
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"Code"} and my $code = $result->{"Code"}{"Value"}) 0 91 91
Condition33%1/3
$result->{"Code"} and my $code = $result->{"Code"}{"Value"}
!ll&&!rl&&r
9100
MC/DC0%0/2
$result->{"Code"} and my $code = $result->{"Code"}{"Value"}
$result->{"Code"} my $code = $result->{"Code"}{"Value"}
Truth table33%1/3
A and B
A= $result->{"Code"}B= my $code = $result->{"Code"}{"Value"}
ABresult
0X0
100
111
173 && (my $code = $result->{Code}{Value})) {
174  
175 #
176 # Elluminate error!
177 #
178
179 0 my $reason = $result->{Reason}{Text};
180 0 my @stack_trace;
181  
182 0 my $stack = $result->{Detail}{Stack};
183  
184 0 ▶ if ($stack && (my $trace = $stack->{Trace})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack and my $trace = $stack->{"Trace"}) 0 0 0
Condition0%0/3
$stack and my $trace = $stack->{"Trace"}
!ll&&!rl&&r
000
MC/DC0%0/2
$stack and my $trace = $stack->{"Trace"}
$stack my $trace = $stack->{"Trace"}
Truth table0%0/3
A and B
A= $stackB= my $trace = $stack->{"Trace"}
ABresult
0X0
100
111
185 0 ▶ @stack_trace = (Elive::Util::_reftype($trace) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
Elive::Util::_reftype($trace) eq 'ARRAY' ? : 0 0 0
186 ? @$trace
187 : $trace);
188  
189 }
190  
191 0 my %seen;
192  
193 0 ▶ my @error = grep {$_ && !$seen{$_}++} ($code, $reason, @stack_trace);
Branch: 1 branch
BranchTrueFalseTotal
$_ and not $seen{$_}++ 0 0 0
194 0 ▶ my $msg = @error ? join(' ', @error) : YAML::Syck::Dump($result);
Branch: 1 branch
BranchTrueFalseTotal
@error ? : 0 0 0
195 0 die "$msg\n";
196 }
197 }
198 }
199  
200 =head2 check_command
201  
202 my $command1 = Elive->check_command([qw{getUser listUser}])
203 my $command2 = Elive->check_command(deleteUser => 'd')
204  
205 Find the first known command in the list. Raise an error if it's unknown;
206  
207 See also: elive_lint_config.
208  
209 =cut
210  
211 sub check_command {
212 200 ▶ my $class = shift;
Subroutine check_command: called CC=13 SCAR=29.2
Pod documented
213 200 my $commands = shift;
214 200 my $crud = shift; #create, read, update or delete
215 200 my $params = shift;
216  
217 200 ▶ if (Elive::Util::_reftype($commands) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (Elive::Util::_reftype($commands) eq "CODE") 0 200 200
218 0 $commands = $commands->($crud, $params);
219 }
220  
221 200 ▶ $commands = [$commands]
Branch: 1 branch
BranchTrueFalseTotal
unless Elive::Util::_reftype($commands) eq "ARRAY" 200 0 200
222 unless Elive::Util::_reftype($commands) eq 'ARRAY';
223  
224 200 my $usage = "usage: \$class->check_command(\$name[,'c'|'r'|'u'|'d'])";
225 200 ▶ die $usage unless @$commands && $commands->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @$commands and $commands->[0] 0 200 200
Condition33%1/3
@$commands and $commands->[0]
!ll&&!rl&&r
00200
MC/DC0%0/2
@$commands and $commands->[0]
@$commands $commands->[0]
Truth table33%1/3
A and B
A= @$commandsB= $commands->[0]
ABresult
0X0
100
111
226  
227 200 my $known_commands = $class->known_commands;
228  
229 die "no known commands for class: $class"
230 200 ▶ unless $known_commands && (keys %{$known_commands});
Branch: 1 branch
BranchTrueFalseTotal
unless $known_commands and keys %{$known_commands;} 0 200 200
Condition33%1/3
$known_commands and keys %{$known_commands;}
!ll&&!rl&&r
00200
MC/DC0%0/2
$known_commands and keys %{$known_commands;}
$known_commands keys %{$known_commands;}
Truth table33%1/3
A and B
A= $known_commandsB= keys %{$known_commands;}
ABresult
0X0
100
111
231  
232 200 my ($command) = grep {exists $known_commands->{$_}} @$commands;
233  
234 200 ▶ croak "Unknown command(s): @{$commands}"
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 200 200
235 unless $command;
236  
237 200 ▶ if ($crud) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crud) 98 102 200
238 98 $crud = lc(substr($crud,0,1));
239 98 ▶ die $usage
Branch: 1 branch
BranchTrueFalseTotal
unless $crud =~ /^[c|r|u|d]$/xi 0 98 98
240 unless $crud =~ m{^[c|r|u|d]$}xi;
241  
242 98 my $command_type = $known_commands->{$command};
243 98 ▶ die "misconfigured command: $command"
Branch: 1 branch
BranchTrueFalseTotal
unless $command_type and $command_type =~ /^[c|r|u|d]+$/xi 0 98 98
Condition33%1/3
$command_type and $command_type =~ /^[c|r|u|d]+$/xi
!ll&&!rl&&r
0098
MC/DC0%0/2
$command_type and $command_type =~ /^[c|r|u|d]+$/xi
$command_type $command_type =~ /^[c|r|u|d]+$/xi
Truth table33%1/3
A and B
A= $command_typeB= $command_type =~ /^[c|r|u|d]+$/xi
ABresult
0X0
100
111
244 unless $command_type && $command_type =~ m{^[c|r|u|d]+$}xi;
245  
246 98 ▶ die "command $command. Type mismatch. Expected $crud, found $command_type"
Branch: 1 branch
BranchTrueFalseTotal
unless $crud =~ /[$command_type]/i 0 98 98
247 unless ($crud =~ m{[$command_type]}i);
248 }
249  
250 200 return $command;
251 }
252  
253 =head2 known_commands
254  
255 Returns an array of hash-value pairs for all Elluminate I<Live!> commands
256 required by Elive. This list is cross-checked by the script elive_lint_config.
257  
258 =cut
259  
260 =head2 call
261  
262 my $som = $self->call( $cmd, %params );
263  
264 Performs an Elluminate SOAP method call. Returns the response as a
265 SOAP::SOM object.
266  
267 =cut
268  
269 sub call {
270 0 ▶ my ($self, $cmd, %params) = @_;
Subroutine call: not called CC=4 SCAR=30.0
Pod documented
271  
272 0 $cmd = $self->check_command($cmd, undef, \%params);
273  
274 0 my @soap_params = $self->_preamble($cmd);
275  
276 0 foreach my $name (keys %params) {
277  
278 0 my $value = $params{$name};
279  
280 $value = SOAP::Data->type(string => Elive::Util::string($value))
281 unless (Scalar::Util::blessed($value)
282 0 ▶ && try {$value->isa('SOAP::Data')});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and &try(sub { $value->isa("SOAP::Data"); } ) 0 0 0
Condition0%0/3
builtin::blessed($value) and &try(sub { $value->isa("SOAP::Data"); } )
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and &try(sub { $value->isa("SOAP::Data"); } )
builtin::blessed($value) &try(sub { $value->isa("SOAP::Data"); } )
Truth table0%0/3
A and B
A= builtin::blessed($value)B= &try(sub { $value->isa("SOAP::Data"); } )
ABresult
0X0
100
111
283  
284 0 my $soap_param = $value->name($name);
285  
286 0 push (@soap_params, $soap_param);
287 }
288  
289 0 my $som = $self->soap->call( @soap_params );
290  
291 0 return $som;
292 }
293  
294 =head2 disconnect
295  
296 Closes a connection.
297  
298 =cut
299  
300 sub disconnect {
301 32 ▶ my $self = shift;
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
302 32 return;
303 }
304  
305 =head2 url
306  
307 my $url1 = $connection1->url;
308 my $url2 = $connection2->url;
309  
310 Returns a restful url for the connection.
311  
312 =cut
313  
314 sub url {
315 338 ▶ my $self = shift;
Subroutine url: called CC=2 SCAR=6.9
Pod documented
316 338 ▶ $self->_url(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 25 313 338
317 338 return $self->_url;
318 }
319  
320 sub DESTROY {
321 16 ▶ shift->disconnect;
Subroutine DESTROY: called CC=1 SCAR=0.0
322 16 return;
323 }
324  
325 =head1 SEE ALSO
326  
327 L<Elive::Connection::SDK> L<Elive::Connection::API> L<SOAP::Lite>
328  
329 =cut
330  
331 22 1;
« Elive/View/Session.pm ↑ Summary Elive/DAO.pm »