PagSeguroTransactionParser::readTransaction()   F
last analyzed

Complexity

Conditions 54
Paths > 20000

Size

Total Lines 272
Code Lines 111

Duplication

Lines 26
Ratio 9.56 %

Importance

Changes 0
Metric Value
cc 54
eloc 111
c 0
b 0
f 0
nc 4294967295
nop 1
dl 26
loc 272
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * 2007-2014 [PagSeguro Internet Ltda.]
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 *Licensed under the Apache License, Version 2.0 (the "License");
8
 *you may not use this file except in compliance with the License.
9
 *You may obtain a copy of the License at
10
 *
11
 *http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *Unless required by applicable law or agreed to in writing, software
14
 *distributed under the License is distributed on an "AS IS" BASIS,
15
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *See the License for the specific language governing permissions and
17
 *limitations under the License.
18
 *
19
 *  @author    PagSeguro Internet Ltda.
20
 *  @copyright 2007-2014 PagSeguro Internet Ltda.
21
 *  @license   http://www.apache.org/licenses/LICENSE-2.0
22
 */
23
24
/***
25
 * Class PagSeguroTransactionParser
26
 */
27
class PagSeguroTransactionParser extends PagSeguroServiceParser
28
{
29
30
    /***
31
     * @param $str_xml
32
     * @return PagSeguroTransactionSearchResult
33
     */
34
    public static function readSearchResult($str_xml)
35
    {
36
37
        $parser = new PagSeguroXmlParser($str_xml);
38
        $data = $parser->getResult('transactionSearchResult');
39
40
        $searchResutlt = new PagSeguroTransactionSearchResult();
41
42
        if (isset($data['totalPages'])) {
43
            $searchResutlt->setTotalPages($data['totalPages']);
44
        }
45
46
        if (isset($data['date'])) {
47
            $searchResutlt->setDate($data['date']);
48
        }
49
50
        if (isset($data['resultsInThisPage'])) {
51
            $searchResutlt->setResultsInThisPage($data['resultsInThisPage']);
52
        }
53
54
        if (isset($data['currentPage'])) {
55
            $searchResutlt->setCurrentPage($data['currentPage']);
56
        }
57
58
        if (isset($data['transactions']) && is_array($data['transactions'])) {
59
            $transactions = array();
60 View Code Duplication
            if (isset($data["transactions"]['transaction'][0])) {
61
                $i = 0;
62
                foreach ($data["transactions"]['transaction'] as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data['transactions']['transaction'] of type string is not traversable.
Loading history...
63
                    $transactions[$i++] = self::parseTransactionSummary($value);
64
                }
65
            } else {
66
                $transactions[0] = self::parseTransactionSummary($data["transactions"]['transaction']);
67
            }
68
            $searchResutlt->setTransactions($transactions);
69
        }
70
71
        return $searchResutlt;
72
    }
73
74
    /***
75
     * @param $str_xml
76
     * @return PagSeguroTransaction
77
     */
78
    public static function readTransaction($str_xml)
79
    {
80
81
        // Parser
82
        $parser = new PagSeguroXmlParser($str_xml);
83
84
        // <transaction>
85
        $data = $parser->getResult('transaction');
86
87
        $transaction = new PagSeguroTransaction();
88
89
        // <transaction> <lastEventDate>
90
        if (isset($data["lastEventDate"])) {
91
            $transaction->setLastEventDate($data["lastEventDate"]);
92
        }
93
94
        // <transaction> <date>
95
        if (isset($data["date"])) {
96
            $transaction->setDate($data["date"]);
97
        }
98
99
        // <transaction> <code>
100
        if (isset($data["code"])) {
101
            $transaction->setCode($data["code"]);
102
        }
103
104
        // <transaction> <reference>
105
        if (isset($data["reference"])) {
106
            $transaction->setReference($data["reference"]);
107
        }
108
109
        // <transaction> <recoveryCode>
110
        if (isset($data["recoveryCode"])) {
111
            $transaction->setRecoveryCode($data["recoveryCode"]);
112
        }
113
114
        // <transaction> <type>
115
        if (isset($data["type"])) {
116
            $transaction->setType(new PagSeguroTransactionType($data["type"]));
117
        }
118
119
        // <transaction> <status>
120
        if (isset($data["status"])) {
121
            $transaction->setStatus(new PagSeguroTransactionStatus($data["status"]));
122
        }
123
124
        // <transaction> <cancellationSource>
125
        if (isset($data["cancellationSource"])) {
126
            $transaction->setCancellationSource(
127
                new PagSeguroTransactionCancellationSource($data["cancellationSource"])
128
            );  
129
        }
130
131 View Code Duplication
        if (isset($data["paymentMethod"]) && is_array($data["paymentMethod"])) {
132
133
            // <transaction> <paymentMethod>
134
            $paymentMethod = new PagSeguroPaymentMethod();
135
136
            // <transaction> <paymentMethod> <type>
137
            if (isset($data["paymentMethod"]['type'])) {
138
                $paymentMethod->setType(new PagSeguroPaymentMethodType($data["paymentMethod"]['type']));
139
            }
140
141
            // <transaction> <paymentMethod> <code>
142
            if (isset($data["paymentMethod"]['code'])) {
143
                $paymentMethod->setCode(new PagSeguroPaymentMethodCode($data["paymentMethod"]['code']));
144
            }
145
146
            $transaction->setPaymentMethod($paymentMethod);
147
        }
148
149
        // <transaction> <paymentLink>
150
        if (isset($data["paymentLink"])) {
151
            $transaction->setPaymentLink($data["paymentLink"]);
152
        }
153
154
        // <transaction> <grossAmount>
155
        if (isset($data["grossAmount"])) {
156
            $transaction->setGrossAmount($data["grossAmount"]);
157
        }
158
159
        // <transaction> <discountAmount>
160
        if (isset($data["discountAmount"])) {
161
            $transaction->setDiscountAmount($data["discountAmount"]);
162
        }
163
164
        // <transaction> <feeAmount>
165
        if (isset($data["feeAmount"])) {
166
            $transaction->setFeeAmount($data["feeAmount"]);
167
        }
168
169
        // <transaction> <netAmount>
170
        if (isset($data["netAmount"])) {
171
            $transaction->setNetAmount($data["netAmount"]);
172
        }
173
174
        //<transaction><escrowEndDate>
175
        if (isset($data["escrowEndDate"])) {
176
            $transaction->setEscrowEndDate($data["escrowEndDate"]);
177
        }
178
179
        // <transaction> <extraAmount>
180
        if (isset($data["extraAmount"])) {
181
            $transaction->setExtraAmount($data["extraAmount"]);
182
        }
183
184
        // <transaction> <installmentCount>
185
        if (isset($data["installmentCount"])) {
186
            $transaction->setInstallmentCount($data["installmentCount"]);
187
        }
188
189
        // <transaction> <creditorFees>
190
        if (isset($data["creditorFees"])) {
191
            $transaction->setCreditorFees(new PagSeguroTransactionCreditorFees($data["creditorFees"]));
192
        }
193
194
        //<transaction><operationalFeeAmount>
195
        if (isset($data["operationalFeeAmount"])) {
196
            $transaction->setOperationalFeeAmount($data["operationalFeeAmount"]);
197
        }
198
199
        //<transaction><installmentFeeAmount>
200
        if (isset($data["installmentFeeAmount"])) {
201
            $transaction->setInstallmentFeeAmount($data["installmentFeeAmount"]);
202
        }
203
204
        //<transaction><intermediationRateAmount>
205
        if (isset($data["intermediationRateAmount"])) {
206
            $transaction->setIntermediationRateAmount($data["intermediationRateAmount"]);
0 ignored issues
show
Bug introduced by
The method setIntermediationRateAmount() does not seem to exist on object<PagSeguroTransaction>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207
        }
208
209
        //<transaction><intermediationFeeAmount>
210
        if (isset($data["intermediationFeeAmount"])) {
211
            $transaction->setIntermediationFeeAmount($data["intermediationFeeAmount"]);
0 ignored issues
show
Bug introduced by
The method setIntermediationFeeAmount() does not seem to exist on object<PagSeguroTransaction>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
        }
213
214
        //<transaction><items>
215
        if (isset($data["items"]['item']) && is_array($data["items"]['item'])) {
216
217
            $items = array();
218
            $i = 0;
219
220 View Code Duplication
            if (isset($data["items"]['item'][0])) {
221
                foreach ($data["items"]['item'] as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data['items']['item'] of type string is not traversable.
Loading history...
222
                    $item = self::parseTransactionItem($value);
223
                    $items[$i] = $item;
224
                    $i++;
225
                }
226
            } else {
227
                $items[0] = self::parseTransactionItem($data["items"]['item']);
228
            }
229
230
            // <transaction> <items>
231
            $transaction->setItems($items);
232
        }
233
234
        if (isset($data["sender"])) {
235
236
            // <transaction> <sender>
237
            $sender = new PagSeguroSender();
238
239
            // <transaction> <sender> <name>
240
            if (isset($data["sender"]["name"])) {
241
                $sender->setName($data["sender"]["name"]);
242
            }
243
244
            // <transaction> <sender> <email>
245
            if (isset($data["sender"]["email"])) {
246
                $sender->setEmail($data["sender"]["email"]);
247
            }
248
249
            if (isset($data["sender"]["phone"])) {
250
251
                // <transaction> <sender> <phone>
252
                $phone = new PagSeguroPhone();
253
254
                // <transaction> <sender> <phone> <areaCode>
255
                if (isset($data["sender"]["phone"]["areaCode"])) {
256
                    $phone->setAreaCode($data["sender"]["phone"]["areaCode"]);
257
                }
258
259
                // <transaction> <sender> <phone> <number>
260
                if (isset($data["sender"]["phone"]["number"])) {
261
                    $phone->setNumber($data["sender"]["phone"]["number"]);
262
                }
263
264
                $sender->setPhone($phone);
265
            }
266
267
            // <transaction><sender><documents>
268
            if (isset($data["sender"]['documents']) && is_array($data["sender"]['documents'])) {
269
270
                $documents = $data["sender"]['documents'];
271
                if (count($documents) > 0) {
272
                    foreach ($documents as $document) {
0 ignored issues
show
Bug introduced by
The expression $documents of type string is not traversable.
Loading history...
273
                        $sender->addDocument($document['type'], $document['value']);
274
                    }
275
                }
276
            }
277
278
            $transaction->setSender($sender);
279
        }
280
281
        if (isset($data["shipping"]) && is_array($data["shipping"])) {
282
283
            // <transaction> <shipping>
284
            $shipping = new PagSeguroShipping();
285
286
            // <transaction> <shipping> <type>
287
            if (isset($data["shipping"]["type"])) {
288
                $shipping->setType(new PagSeguroShippingType($data["shipping"]["type"]));
289
            }
290
291
            // <transaction> <shipping> <cost>
292
            if (isset($data["shipping"]["cost"])) {
293
                $shipping->setCost($data["shipping"]["cost"]);
294
            }
295
296
            if (isset($data["shipping"]["address"]) && is_array($data["shipping"]["address"])) {
297
298
                // <transaction> <shipping> <address>
299
                $address = new PagSeguroAddress();
300
301
                // <transaction> <shipping> <address> <street>
302
                if (isset($data["shipping"]["address"]["street"])) {
303
                    $address->setStreet($data["shipping"]["address"]["street"]);
304
                }
305
306
                // <transaction> <shipping> <address> <number>
307
                if (isset($data["shipping"]["address"]["number"])) {
308
                    $address->setNumber($data["shipping"]["address"]["number"]);
309
                }
310
311
                // <transaction> <shipping> <address> <complement>
312
                if (isset($data["shipping"]["address"]["complement"])) {
313
                    $address->setComplement($data["shipping"]["address"]["complement"]);
314
                }
315
316
                // <transaction> <shipping> <address> <city>
317
                if (isset($data["shipping"]["address"]["city"])) {
318
                    $address->setCity($data["shipping"]["address"]["city"]);
319
                }
320
321
                // <transaction> <shipping> <address> <state>
322
                if (isset($data["shipping"]["address"]["state"])) {
323
                    $address->setState($data["shipping"]["address"]["state"]);
324
                }
325
326
                // <transaction> <shipping> <address> <district>
327
                if (isset($data["shipping"]["address"]["district"])) {
328
                    $address->setDistrict($data["shipping"]["address"]["district"]);
329
                }
330
331
                // <transaction> <shipping> <address> <postalCode>
332
                if (isset($data["shipping"]["address"]["postalCode"])) {
333
                    $address->setPostalCode($data["shipping"]["address"]["postalCode"]);
334
                }
335
336
                // <transaction> <shipping> <address> <country>
337
                if (isset($data["shipping"]["address"]["country"])) {
338
                    $address->setCountry($data["shipping"]["address"]["country"]);
339
                }
340
341
                $shipping->setAddress($address);
342
            }
343
344
            // <transaction> <shipping>
345
            $transaction->setShipping($shipping);
346
        }
347
348
        return $transaction;
349
    }
350
351
    /***
352
     * @param $data
353
     * @return PagSeguroItem
354
     */
355
    private static function parseTransactionItem($data)
356
    {
357
358
        // <transaction> <items> <item>
359
        $item = new PagSeguroItem();
360
361
        // <transaction> <items> <item> <id>
362
        if (isset($data["id"])) {
363
            $item->setId($data["id"]);
364
        }
365
366
        // <transaction> <items> <item> <description>
367
        if (isset($data["description"])) {
368
            $item->setDescription($data["description"]);
369
        }
370
371
        // <transaction> <items> <item> <quantity>
372
        if (isset($data["quantity"])) {
373
            $item->setQuantity($data["quantity"]);
374
        }
375
376
        // <transaction> <items> <item> <amount>
377
        if (isset($data["amount"])) {
378
            $item->setAmount($data["amount"]);
379
        }
380
381
        // <transaction> <items> <item> <weight>
382
        if (isset($data["weight"])) {
383
            $item->setWeight($data["weight"]);
384
        }
385
386
        return $item;
387
    }
388
389
    /***
390
     * @param $data
391
     * @return PagSeguroTransactionSummary
392
     */
393
    private static function parseTransactionSummary($data)
394
    {
395
396
        $transactionSummary = new PagSeguroTransactionSummary();
397
398
        if (isset($data['type'])) {
399
            $transactionSummary->setType(new PagSeguroTransactionType($data['type']));
400
        }
401
        if (isset($data['code'])) {
402
            $transactionSummary->setCode($data['code']);
403
        }
404
        if (isset($data['reference'])) {
405
            $transactionSummary->setReference($data['reference']);
406
        }
407
        if (isset($data['date'])) {
408
            $transactionSummary->setDate($data['date']);
409
        }
410
        if (isset($data['lastEventDate'])) {
411
            $transactionSummary->setLastEventDate($data['lastEventDate']);
412
        }
413
        if (isset($data['grossAmount'])) {
414
            $transactionSummary->setGrossAmount($data['grossAmount']);
415
        }
416
        if (isset($data['status'])) {
417
            $transactionSummary->setStatus(new PagSeguroTransactionStatus($data['status']));
418
        }
419
        if (isset($data["cancellationSource"])) {
420
            $transactionSummary->setCancellationSource(
421
                new PagSeguroTransactionCancellationSource($data["cancellationSource"])
422
            );
423
        }
424
        if (isset($data['netAmount'])) {
425
            $transactionSummary->setNetAmount($data['netAmount']);
426
        }
427
        if (isset($data['discountAmount'])) {
428
            $transactionSummary->setDiscountAmount($data['discountAmount']);
429
        }
430
        if (isset($data['feeAmount'])) {
431
            $transactionSummary->setFeeAmount($data['feeAmount']);
432
        }
433
        if (isset($data['extraAmount'])) {
434
            $transactionSummary->setExtraAmount($data['extraAmount']);
435
        }
436
        if (isset($data['lastEvent'])) {
437
            $transactionSummary->setLastEventDate($data['lastEvent']);
438
        }
439 View Code Duplication
        if (isset($data['paymentMethod'])) {
440
            $paymentMethod = new PagSeguroPaymentMethod();
441
            if (isset($data['paymentMethod']['type'])) {
442
                $paymentMethod->setType(new PagSeguroPaymentMethodType($data['paymentMethod']['type']));
443
            }
444
            if (isset($data['paymentMethod']['code'])) {
445
                $paymentMethod->setCode(new PagSeguroPaymentMethodCode($data['paymentMethod']['code']));
446
            }
447
            $transactionSummary->setPaymentMethod($paymentMethod);
448
        }
449
450
        if (isset($data["recoveryCode"])) {
451
            $transactionSummary->setRecoveryCode($data["recoveryCode"]);
452
        }
453
        
454
        return $transactionSummary;
455
    }
456
}
457