File Coverage

blib/lib/WWW/Correios/SIGEP/Common.pm
Criterion Covered Total %
statement 30 53 56.6
branch 3 16 18.7
condition n/a
subroutine 8 11 72.7
pod 0 4 0.0
total 41 84 48.8


line stmt bran cond sub pod time code
1             package WWW::Correios::SIGEP::Common;
2 2     2   14 use strict;
  2         3  
  2         53  
3 2     2   8 use warnings;
  2         5  
  2         45  
4 2     2   961 use XML::Compile::WSDL11;
  2         621876  
  2         114  
5 2     2   1109 use XML::Compile::SOAP11; # <-- loads the soap namespace
  2         41179  
  2         71  
6 2     2   1155 use XML::Compile::Transport::SOAPHTTP; # <-- sends messages over HTTP
  2         80376  
  2         77  
7 2     2   20 use LWP::UserAgent; # <-- see "Note" on build_transport();
  2         5  
  2         44  
8 2     2   1557 use File::ShareDir;
  2         55368  
  2         1022  
9              
10             sub call {
11 0     0 0 0 my ($self, $operation, $params) = @_;
12 0         0 my $call = compiled_client($self, $operation);
13 0         0 return process_return($self, $operation, $call->($params));
14             }
15              
16             sub process_return {
17 0     0 0 0 my ($self, $operation, $return, $trace) = @_;
18              
19 0 0       0 if ($self->{debug}) {
20 0         0 $trace->printErrors;
21 0         0 $trace->printRequest;
22 0         0 $trace->printResponse;
23             }
24 0 0       0 if (exists $return->{parameters}{return}) {
    0          
25 0         0 return $return->{parameters}{return};
26             }
27             elsif (exists $return->{parameters}{$operation}) {
28 0 0       0 if (exists $return->{parameters}{$operation}{resultado_solicitacao}) {
29 0         0 return $return->{parameters}{$operation}{resultado_solicitacao};
30             }
31             else {
32 0         0 return $return->{parameters}{$operation};
33             }
34             }
35             else {
36 0         0 return $return;
37             }
38             }
39              
40             sub compiled_client {
41 0     0 0 0 my ($self, $operation) = @_;
42              
43 0         0 my $key = '_compiled' . $operation;
44 0 0       0 if (!$self->{$key}) {
45             $self->{$key} = $self->{wsdl}->compileClient(
46             operation => $operation,
47             transport => $self->{transport},
48 0         0 );
49             }
50 0         0 return $self->{$key};
51             }
52              
53             # FIXME: (fixed in XML::Compile::SOAP 3.07)
54             # Use of uninitialized value $ns in hash element at XML/Compile/Cache.pm line 125.
55             # Use of uninitialized value $ns in hash element at XML/Compile/Cache.pm line 134.
56             # Use of uninitialized value $ns in concatenation (.) or string at XML/Compile/Cache.pm line 135.
57             # Use of uninitialized value in string eq at XML/Compile/Cache.pm line 166.
58             # Use of uninitialized value $ns in string eq at XML/Compile/Cache.pm line 166.
59             sub build_transport {
60 5     5 0 13 my ($self) = @_;
61              
62             my $local_file = File::ShareDir::dist_file(
63             'WWW-Correios-SIGEP',
64             $self->{wsdl_local_file}
65 5         37 );
66 5         906 $self->{wsdl} = XML::Compile::WSDL11->new( $local_file );
67              
68             ############################################################
69             ### Note:
70             ### -----
71             ### Of course Correios does *NOT* have a valid certificate,
72             ### Otherwise we'd be able to skip all this and just do:
73             ###
74             ### my $call = $wsdl->compileClient( 'method' );
75             ###
76             ### but if we do that we get:
77             ### "Can't connect to apphom.correios.com.br:443 (certificate verify failed)"
78             ###
79             ### which is why we need to customize our transport here :(
80 5 50       1420027 my @timeout = $self->{timeout} ? (timeout => $self->{timeout}) : ();
81 5         84 my $ua = LWP::UserAgent->new(
82             @timeout,
83             ssl_opts => { verify_hostname => 0 },
84             # $self->{usuario}, $self->{senha}
85             );
86 5 50       4032 $ua->credentials( @{$self->{ua_auth}} ) if exists $self->{ua_auth};
  0         0  
87              
88             $self->{transport} = XML::Compile::Transport::SOAPHTTP->new(
89             address => $self->{wsdl}->endPoint,
90 5         36 @timeout,
91             user_agent => $ua
92             )->compileClient();
93              
94 5 50       2648 if ($self->{precompile}) {
95 0         0 foreach my $operation (@{$self->{precompile}}) {
  0         0  
96 0         0 compiled_client($self, $operation);
97             }
98             }
99 5         27 return $self;
100             }
101              
102             42;