MWS/XML/Response/FeedSubmissionResult.pm

filter: Statementstmt 25.2% 29 / 115 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 30.4% 7 / 23 Podpod 100% 8 / 8 totaltotal 19.1% 44 / 230 CC 36 SCAR 43.5
CC
36
Cov
16%
_parse_results
210.0
report_errors
110.0
_format_msgs
42.0
CRAP 77.8
« MWS/Routines.pm ↑ Summary MWS/XML/Feed.pm »
1 package Amazon::MWS::XML::Response::FeedSubmissionResult;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use XML::Compile::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Amazon::MWS::XML::Response::FeedSubmissionResult -- response parser
16  
17 =head1 SYNOPSIS
18  
19 my $res = Amazon::MWS::XML::Response::FeedSubmissionResult->new(xml => $xml);
20 if ($res->is_success) { ... };
21  
22 =head1 ACCESSOR
23  
24 =head2 xml
25  
26 The xml string
27  
28 =head2 xml_reader
29  
30 A sub reference with the AmazonEnvelope reader.
31  
32 =head2 structure
33  
34 Lazy attribute built via parsing the xml string passed at the constructor.
35  
36 =head1 METHODS
37  
38 =head2 is_success
39  
40 =head2 errors
41  
42 =head2 report_errors
43  
44 A list of error messages, where each element is an hashref with this keys:
45  
46 =over 4
47  
48 =item code (numeric)
49  
50 =item type (warning or error)
51  
52 =item message (human-readable)
53  
54 =back
55  
56 =cut
57  
58 7 has xml => (is => 'ro', required => '1');
59 7 has xml_reader => (is => 'ro',
60 required => 1);
61 7 has structure => (is => 'lazy');
62  
63 0 ▶sub _build_structure {
Subroutine _build_structure: not called CC=3 SCAR=24.8
64 0 my $self = shift;
65 0 my $struct = $self->xml_reader->($self->xml);
66 0 ▶ die "not a processing report xml" unless $struct->{MessageType} eq 'ProcessingReport';
Branch: 1 branch
BranchTrueFalseTotal
unless $struct->{"MessageType"} eq "ProcessingReport" 0 0 0
67 0 ▶ if (@{$struct->{Message}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$struct->{"Message"};} > 1) 0 0 0
68 0 die $self->xml . " returned more than 1 message!";
69 }
70 0 return $struct->{Message}->[0]->{ProcessingReport};
71 }
72  
73 7 has skus_errors => (is => 'lazy');
74 7 has skus_warnings => (is => 'lazy');
75 7 has orders_errors => (is => 'lazy');
76 7 has orders_warnings => (is => 'lazy');
77  
78 0 ▶sub _build_skus_errors {
Subroutine _build_skus_errors: not called CC=1 SCAR=6.9
79 0 my $self = shift;
80 0 return $self->_parse_results(sku => 'Error');
81 }
82  
83 0 ▶sub _build_skus_warnings {
Subroutine _build_skus_warnings: not called CC=1 SCAR=6.9
84 0 my $self = shift;
85 0 return $self->_parse_results(sku => 'Warning');
86 }
87  
88 0 ▶sub _build_orders_errors {
Subroutine _build_orders_errors: not called CC=1 SCAR=6.9
89 0 my $self = shift;
90 0 return $self->_parse_results(order_id => 'Error');
91 }
92  
93 0 ▶sub _build_orders_warnings {
Subroutine _build_orders_warnings: not called CC=1 SCAR=6.9
94 0 my $self = shift;
95 0 return $self->_parse_results(order_id => 'Warning');
96 }
97  
98 0 ▶sub report_errors {
Subroutine report_errors: not called CC=10 SCAR=47.0
Pod documented
99 0 my ($self) = @_;
100 0 my $struct = $self->structure;
101 0 my @output;
102 0 ▶ if ($struct->{Result}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->{"Result"}) 0 0 0
103 0 foreach my $res (@{ $struct->{Result} }) {
104 0 ▶ if (my $type = $res->{ResultCode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $res->{"ResultCode"}) 0 0 0
105 0 ▶ if ($type eq 'Error' or $type eq 'Warning') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Error" or $type eq "Warning") 0 0 0
Condition0%0/3
$type eq "Error" or $type eq "Warning"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "Error" or $type eq "Warning"
$type eq "Error" $type eq "Warning"
Truth table0%0/3
A or B
A= $type eq "Error"B= $type eq "Warning"
ABresult
1X1
011
000
106 0 my @error_chunks;
107 0 my $error_code = 0;
108 0 ▶ if (my $details = $res->{AdditionalInfo}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $details = $res->{"AdditionalInfo"}) 0 0 0
109 0 foreach my $key (keys %$details) {
110 0 push @error_chunks, "$key: $details->{$key}";
111 }
112 }
113 0 push @error_chunks, $type;
114 0 ▶ if ($res->{ResultMessageCode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"ResultMessageCode"}) 0 0 0
115 0 $error_code = $res->{ResultMessageCode};
116 }
117 0 ▶ if ($res->{ResultDescription}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"ResultDescription"}) 0 0 0
118 0 push @error_chunks, $res->{ResultDescription};
119 }
120 0 push @output, {
121 code => $error_code,
122 type => lc($type),
123 message => join(' ', @error_chunks),
124 };
125 }
126 }
127 }
128 }
129 0 return @output;
130 }
131  
132 0 ▶sub _parse_results {
Subroutine _parse_results: not called CC=14 SCAR=53.5
133 0 my ($self, $type, $code) = @_;
134 0 ▶ die unless ($code eq 'Error' or $code eq 'Warning');
Branch: 1 branch
BranchTrueFalseTotal
unless $code eq "Error" or $code eq "Warning" 0 0 0
Condition0%0/3
$code eq "Error" or $code eq "Warning"
l!l&&r!l&&!r
000
MC/DC0%0/2
$code eq "Error" or $code eq "Warning"
$code eq "Error" $code eq "Warning"
Truth table0%0/3
A or B
A= $code eq "Error"B= $code eq "Warning"
ABresult
1X1
011
000
135 0 my $struct = $self->structure;
136 0 my @msgs;
137 0 my %map = (sku => 'SKU',
138 order_id => 'AmazonOrderID');
139  
140 0 ▶ my $key = $map{$type} or die "Bad type $type";
Branch: 1 branch
BranchTrueFalseTotal
my $key = $map{$type} or die "Bad type $type" 0 0 0
141 0 ▶ if ($struct->{Result}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->{"Result"}) 0 0 0
142 0 foreach my $res (@{ $struct->{Result} }) {
143 0 ▶ if ($res->{ResultCode} and $res->{ResultCode} eq $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"ResultCode"} and $res->{"ResultCode"} eq $code) 0 0 0
Condition0%0/3
$res->{"ResultCode"} and $res->{"ResultCode"} eq $code
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"ResultCode"} and $res->{"ResultCode"} eq $code
$res->{"ResultCode"} $res->{"ResultCode"} eq $code
Truth table0%0/3
A and B
A= $res->{"ResultCode"}B= $res->{"ResultCode"} eq $code
ABresult
0X0
100
111
144 0 ▶ if (my $value = $res->{AdditionalInfo}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $res->{'AdditionalInfo'}{$key}) { } 0 0 0
145 push @msgs, {
146 $type => $value,
147 # this is a bit misnamed, but not too much
148 error => $res->{ResultDescription} || '',
149 0 ▶ code => $res->{ResultMessageCode} || '',
Condition0%0/4
$res->{'ResultDescription'} || ''
l!l
00
$res->{'ResultMessageCode'} || ''
l!l
00
MC/DC0%0/1
$res->{'ResultDescription'} || ''
$res->{'ResultDescription'}
MC/DC0%0/1
$res->{'ResultMessageCode'} || ''
$res->{'ResultMessageCode'}
Truth table0%0/2
A
A= $res->{'ResultDescription'}
Aresult
11
00
Truth table0%0/2
A
A= $res->{'ResultMessageCode'}
Aresult
11
00
150 };
151 }
152 else {
153 push @msgs, {
154 error => $res->{ResultDescription} || '',
155 0 ▶ code => $res->{ResultMessageCode} || '',
Condition0%0/4
$res->{'ResultDescription'} || ''
l!l
00
$res->{'ResultMessageCode'} || ''
l!l
00
MC/DC0%0/1
$res->{'ResultDescription'} || ''
$res->{'ResultDescription'}
MC/DC0%0/1
$res->{'ResultMessageCode'} || ''
$res->{'ResultMessageCode'}
Truth table0%0/2
A
A= $res->{'ResultDescription'}
Aresult
11
00
Truth table0%0/2
A
A= $res->{'ResultMessageCode'}
Aresult
11
00
156 };
157 }
158 }
159 }
160 }
161 0 ▶ @msgs ? return \@msgs : return;
Branch: 1 branch
BranchTrueFalseTotal
@msgs ? : 0 0 0
162 }
163  
164  
165  
166 0 ▶sub is_success {
Subroutine is_success: not called CC=4 SCAR=30.0
Pod documented
167 0 my $self = shift;
168 0 my $struct = $self->structure;
169 0 ▶ if ($struct->{StatusCode} eq 'Complete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->{"StatusCode"} eq "Complete") 0 0 0
170 # Compute the total - successful
171 0 my $success = $struct->{ProcessingSummary}->{MessagesSuccessful};
172 0 my $error = $struct->{ProcessingSummary}->{MessagesWithError};
173 # we ignore the warnings here.
174 # my $warning = $struct->{ProcessingSummary}->{MessagesWithWarning};
175 0 my $total = $struct->{ProcessingSummary}->{MessagesProcessed};
176 0 ▶ if (!$error and $total == $success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $total == $success) 0 0 0
Condition0%0/3
not $error and $total == $success
!ll&&!rl&&r
000
MC/DC0%0/2
not $error and $total == $success
not $error $total == $success
Truth table0%0/3
not(A) and B
A= not $errorB= $total == $success
ABresult
0X0
100
111
177 0 return 1;
178 }
179 }
180 0 return;
181 }
182  
183 0 ▶sub warnings {
Subroutine warnings: not called CC=1 SCAR=6.9
Pod documented
184 0 my $self = shift;
185 0 return $self->_format_msgs($self->skus_warnings);
186 }
187  
188 0 ▶sub errors {
Subroutine errors: not called CC=1 SCAR=6.9
Pod documented
189 0 my $self = shift;
190 0 return $self->_format_msgs($self->skus_errors);
191 }
192  
193 0 ▶sub _format_msgs {
Subroutine _format_msgs: not called CC=6 SCAR=37.4
194 0 my ($self, $list) = @_;
195 0 ▶ if ($list && @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and @$list) 0 0 0
Condition0%0/3
$list and @$list
!ll&&!rl&&r
000
MC/DC0%0/2
$list and @$list
$list @$list
Truth table0%0/3
A and B
A= $listB= @$list
ABresult
0X0
100
111
196 0 my @errors;
197 0 foreach my $err (@$list) {
198 0 ▶ if ($err->{sku}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err->{'sku'}) { } 0 0 0
elsif ($err->{'order_id'}) { } 0 0 0
199 0 push @errors, "SKU $err->{sku}: $err->{error} ($err->{code})";
200 }
201 elsif ($err->{order_id}) {
202 0 push @errors, "Order $err->{order_id}: $err->{error} ($err->{code})";
203 }
204 else {
205 0 push @errors, "$err->{error} ($err->{code})";
206 }
207 }
208 0 return join("\n", @errors);
209 }
210 0 return;
211 }
212  
213 =head2 Failures and warnings
214  
215 They return a list of skus or order_id.
216  
217 =over 4
218  
219 =item failed_skus
220  
221 =item skus_with_warnings
222  
223 =item failed_orders
224  
225 =item orders_with_warnings
226  
227 =back
228  
229 =cut
230  
231 0 ▶sub failed_skus {
Subroutine failed_skus: not called CC=1 SCAR=6.9
Pod documented
232 0 my ($self) = @_;
233 0 return $self->_list_faulty(sku => $self->skus_errors);
234 }
235  
236 0 ▶sub skus_with_warnings {
Subroutine skus_with_warnings: not called CC=1 SCAR=6.9
Pod documented
237 0 my ($self) = @_;
238 0 return $self->_list_faulty(sku => $self->skus_warnings);
239 }
240  
241 0 ▶sub failed_orders {
Subroutine failed_orders: not called CC=1 SCAR=6.9
Pod documented
242 0 my ($self) = @_;
243 0 return $self->_list_faulty(order_id => $self->orders_errors);
244 }
245  
246 0 ▶sub orders_with_warnings {
Subroutine orders_with_warnings: not called CC=1 SCAR=6.9
Pod documented
247 0 my ($self) = @_;
248 0 return $self->_list_faulty(order_id => $self->orders_warnings);
249 }
250  
251  
252 0 ▶sub _list_faulty {
Subroutine _list_faulty: not called CC=4 SCAR=30.0
253 0 my ($self, $what, $list) = @_;
254 0 ▶ die unless $what;
Branch: 1 branch
BranchTrueFalseTotal
unless $what 0 0 0
255 0 ▶ if ($list && @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and @$list) { } 0 0 0
Condition0%0/3
$list and @$list
!ll&&!rl&&r
000
MC/DC0%0/2
$list and @$list
$list @$list
Truth table0%0/3
A and B
A= $listB= @$list
ABresult
0X0
100
111
256 0 return map { $_->{$what} } @$list;
257 }
258 else {
259 0 return;
260 }
261 }
262  
263  
264  
265 7 1;
« MWS/Routines.pm ↑ Summary MWS/XML/Feed.pm »