blib/lib/WWW/Pixelletter.pm

filter: Statementstmt 37.7% 37 / 98 Branchbran 6.2% 2 / 32 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 60.0% 9 / 15 Podpod 100% 6 / 6 totaltotal 30.3% 54 / 178 CC 27 SCAR 35.9
CC
27
Cov
26%
sendPost
90.0
addFile
56.0
_submitForm
30.0
CRAP 36.1
↑ Summary
1 package WWW::Pixelletter;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Locale::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.2';
9 2 ▶use constant ALLOWED_FILE_EXTENSIONS => qw/pdf doc xls ppt rtf wpd psd odt ods odp odg/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use constant MAX_FILE_SIZE => 8388607;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new
13 1 ▶{
Subroutine new: called CC=5 SCAR=16.3
Pod documented
14 1 my ($class, %args) = @_;
15 1 my $self = {};
16  
17 # Some Defaults
18 1 $self->{url} = 'http://www.pixelletter.de/xml/index.php';
19 1 $self->{test_mode} = 'false';
20  
21 1 foreach my $arg (keys %args)
22 {
23 3 $self->{$arg} = $args{$arg};
24 }
25  
26 # Check for required parameters
27 1 foreach( qw/username password/ )
28 {
29 2 ▶ unless( $self->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$_}) 0 2 2
30 {
31 0 die( "Required parameter $_ not defined\n" );
32 }
33 }
34  
35 # The user agent can also be passed (if you want to recycle...), but usually
36 # it will be defined new here
37 1 ▶ if( ! $self->{user_agent} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"user_agent"}) 1 0 1
38 {
39 1 my $ua = LWP::UserAgent->new;
40 1 $self->{user_agent} = $ua;
41 }
42 1 bless($self);
43 1 return($self);
44 }
45  
46 sub addFile
47 0 ▶{
Subroutine addFile: not called CC=7 SCAR=40.3
Pod documented
48 0 my( $self, $file ) = @_;
49 0 my $f = File::Util->new();
50 0 my @files;
51 0 ▶ if( $self->{files} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"files"}) 0 0 0
52 {
53 0 @files = @{ $self->{files} };
54 }
55  
56 # Does the file exist
57 0 ▶ if( ! -f $file )
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
58 {
59 0 die( "File $file does not exist\n" );
60 }
61  
62 # Is it one of the "allowed" extensions (a rudementary test to make sure user is
63 # not trying to send something which pixelletter does not understand
64 0 my $allowed = undef;
65 0 my $extension = ( $file =~ m/.*\.(.*?)$/ )[0];
66 0 foreach( ALLOWED_FILE_EXTENSIONS )
67 {
68 0 ▶ if( $_ eq $extension )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $extension) 0 0 0
69 {
70 0 $allowed = 1;
71 0 last;
72 }
73 }
74 0 ▶ if( ! $allowed )
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed) 0 0 0
75 {
76 0 die( "$extension is not an allowed file type. Allowed file extensions are: " . join( ', ', ALLOWED_FILE_EXTENSIONS ) . "\n" );
77 }
78  
79 # Make sure file is not too big (pixelletter only accepts up to 8MB
80 0 ▶ if( $f->size( $file ) > MAX_FILE_SIZE )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->size($file) > 8388607) 0 0 0
81 {
82 0 die( sprintf( "Cannot process %s because pixelletter only allowes files up to %u bytes but yours is %u bytes\n",
83 $file,
84 MAX_FILE_SIZE,
85 $f->size( $file ),
86 ) );
87 }
88  
89 # Add the file to the form
90 0 push( @files, [$file] );
91 0 $self->{files} = \@files;
92 }
93  
94 sub files
95 0 ▶{
Subroutine files: not called CC=1 SCAR=6.9
Pod documented
96 0 my $self = shift;
97 0 return $self->{files};
98 }
99  
100 sub filecount
101 0 ▶{
Subroutine filecount: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 ▶ if( $self->{files} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"files"}) 0 0 0
104 {
105 0 return( scalar( @{ $self->{files} } ) );
106 }
107 0 return 0;
108 }
109  
110 sub sendFax
111 0 ▶{
Subroutine sendFax: not called CC=4 SCAR=30.0
Pod documented
112 0 my( $self, $fax_number ) = @_;
113
114 0 ▶ if( ! $fax_number || $fax_number !~ m/^\+[0-9]{2,} [0-9]{1,} [0-9]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fax_number and $fax_number =~ /^\+[0-9]{2,} [0-9]{1,} [0-9]+$/) 0 0 0
Condition0%0/3
$fax_number and $fax_number =~ /^\+[0-9]{2,} [0-9]{1,} [0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$fax_number and $fax_number =~ /^\+[0-9]{2,} [0-9]{1,} [0-9]+$/
$fax_number $fax_number =~ /^\+[0-9]{2,} [0-9]{1,} [0-9]+$/
Truth table0%0/3
A and B
A= $fax_numberB= $fax_number =~ /^\+[0-9]{2,} [0-9]{1,} [0-9]+$/
ABresult
0X0
100
111
115 {
116 0 die( "Not a valid fax number\n".
117 "Pixelletter only accepts fax numbers formated like this example:\n+49 89 12345678\n".
118 "You must have a '+' before the country code, and a space either side\n".
119 "of the area code\n" );
120 }
121  
122 0 ▶ if( $self->filecount() < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filecount < 1) 0 0 0
123 {
124 0 die( "No files to send...\n" );
125 }
126  
127 0 my $xml = qq!<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
128 <pixelletter version="1.0">
129 <auth>
130 <email>$self->{username}</email>
131 <password>$self->{password}</password>
132 <agb>ja</agb>
133 <widerrufsverzicht>ja</widerrufsverzicht>
134 <testmodus>$self->{test_mode}</testmodus>
135 </auth>
136 <order>
137 <options>
138 <type>upload</type>
139 <action>2</action>
140 <fax>$fax_number</fax>
141 </options>
142 </order>
143 </pixelletter>
144 !;
145  
146 0 $self->_submitForm( $xml );
147 }
148  
149 sub sendPost
150 0 ▶{
Subroutine sendPost: not called CC=9 SCAR=45.0
Pod documented
151 0 my( $self, $post_center, $dest_country ) = @_;
152
153 0 ▶ if( ! $post_center || $post_center !~ m/^\d$/ || $post_center < 1 || $post_center > 3 )
Branch: 1 branch
BranchTrueFalseTotal
if (not $post_center && $post_center =~ /^\d$/ or $post_center < 1 or $post_center > 3) 0 0 0
Condition0%0/9
not $post_center && $post_center =~ /^\d$/ or $post_center < 1 or $post_center > 3
l!l&&r!l&&!r
000
not $post_center && $post_center =~ /^\d$/ or $post_center < 1
l!l&&r!l&&!r
000
$post_center && $post_center =~ /^\d$/
!ll&&!rl&&r
000
MC/DC0%0/4
not $post_center && $post_center =~ /^\d$/ or $post_center < 1 or $post_center > 3
$post_center $post_center =~ /^\d$/ $post_center < 1 $post_center > 3
Truth table0%0/5
not(A && B) or C or D
A= $post_centerB= $post_center =~ /^\d$/C= $post_center < 1D= $post_center > 3
ABCDresult
0XXX1
10XX1
111X1
11011
11000
154 {
155 0 die( "Not a valid post center id. Valid are 1 to 3.\n" );
156 }
157
158 # Check the destination country is a known ISO abbreviation
159 0 ▶ if( ! $dest_country || $dest_country !~ m/^\w{2}$/ || ! code2country( $dest_country ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($dest_country and $dest_country =~ /^\w{2}$/ and code2country($dest_country)) 0 0 0
Condition0%0/6
$dest_country and $dest_country =~ /^\w{2}$/ and code2country($dest_country)
!ll&&!rl&&r
000
$dest_country and $dest_country =~ /^\w{2}$/
!ll&&!rl&&r
000
MC/DC0%0/3
$dest_country and $dest_country =~ /^\w{2}$/ and code2country($dest_country)
$dest_country $dest_country =~ /^\w{2}$/ code2country($dest_country)
Truth table0%0/4
A and B and C
A= $dest_countryB= $dest_country =~ /^\w{2}$/C= code2country($dest_country)
ABCresult
0XX0
10X0
1100
1111
160 {
161 0 die( "Not a valid destination country code. Please use 2 character ISO3166 codes.\n" );
162 }
163 0 $dest_country = uc( $dest_country );
164  
165 0 ▶ if( $self->filecount() < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filecount < 1) 0 0 0
166 {
167 0 die( "No files to send...\n" );
168 }
169  
170 0 my $xml = qq!<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
171 <pixelletter version="1.0">
172 <auth>
173 <email>$self->{username}</email>
174 <password>$self->{password}</password>
175 <agb>ja</agb>
176 <widerrufsverzicht>ja</widerrufsverzicht>
177 <testmodus>$self->{test_mode}</testmodus>
178 </auth>
179 <order>
180 <options>
181 <type>upload</type>
182 <location>$post_center</location>
183 <destination>$dest_country</destination>
184 <action>1</action>
185 </options>
186 </order>
187 </pixelletter>
188 !;
189  
190 0 $self->_submitForm( $xml );
191 }
192  
193  
194 sub _submitForm
195 0 ▶{
Subroutine _submitForm: not called CC=5 SCAR=34.0
196 0 my( $self, $xml ) = @_;
197 0 my %form = ( 'xml' => $xml );
198 0 my $file_idx = 0;
199 0 foreach( @{ $self->files() } )
200 {
201 0 $form{'uploadfile'.$file_idx} = $_;
202 0 $file_idx++;
203 }
204  
205 0 my $response = $self->{user_agent}->post( $self->{url}, Content_Type => 'multipart/form-data', Content => \%form );
206 0 ▶ unless ($response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
207 {
208 0 die( "Error connecting to server: " . $response->status_line . "\n" );
209 }
210  
211 0 my $response_xml = $response->content;
212 0 ▶ if( $response_xml =~ m/result code\=\"(\d*)\".*\<msg\>(.*?)\<\/msg\>/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($response_xml =~ m[result code\=\"(\d*)\".*\<msg\>(.*?)\</msg\>]s) 0 0 0
213 {
214 0 ▶ if( $1 == 100 )
Branch: 1 branch
BranchTrueFalseTotal
if ($1 == 100) { } 0 0 0
215 {
216 0 return( $1, $2 );
217 }
218 else
219 {
220 0 die( "Send failed ($1): $2\n" );
221 }
222 }
223 0 die( "Send failed:\n$response_xml\n" );
224 }
225  
226 __END__
↑ Summary