Decoder::decode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace PHPSC\PagSeguro\Purchases\Transactions;
3
4
use DateTime;
5
use PHPSC\PagSeguro\Items\Item;
6
use PHPSC\PagSeguro\Items\Items;
7
use PHPSC\PagSeguro\Purchases\Decoder as BaseDecoder;
8
use PHPSC\PagSeguro\Shipping\Shipping;
9
use SimpleXMLElement;
10
11
/**
12
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
13
 */
14
class Decoder extends BaseDecoder
15
{
16
    /**
17
     * @param SimpleXMLElement $obj
18
     *
19
     * @return Transaction
20
     */
21 1
    public function decode(SimpleXMLElement $obj)
22
    {
23 1
        return new Transaction(
24 1
            $this->createDetails($obj),
25 1
            $this->createPayment($obj),
26 1
            (int) $obj->type,
27 1
            $this->createItems($obj->items),
28 1
            $this->createShipping($obj->shipping)
29
        );
30
    }
31
32
    /**
33
     * @param SimpleXMLElement $obj
34
     * @return Payment
35
     */
36 1
    protected function createPayment(SimpleXMLElement $obj)
37
    {
38 1
        return new Payment(
39 1
            new PaymentMethod(
40 1
                (int) $obj->paymentMethod->type,
41 1
                (int) $obj->paymentMethod->code
42
            ),
43 1
            (float) $obj->grossAmount,
44 1
            (float) $obj->discountAmount,
45 1
            (float) $obj->feeAmount,
46 1
            (float) $obj->netAmount,
47 1
            (float) $obj->extraAmount,
48 1
            (int) $obj->installmentCount,
49 1
            isset($obj->escrowEndDate) ? new DateTime((string) $obj->escrowEndDate) : null
50
        );
51
    }
52
53
    /**
54
     * @param SimpleXMLElement $itemsNode
55
     *
56
     * @return Items
57
     */
58 1
    protected function createItems(SimpleXMLElement $itemsNode)
59
    {
60 1
        $items = new Items();
61
62 1
        foreach ($itemsNode->item as $item) {
63 1
            $items->add(
64 1
                new Item(
65 1
                    (string) $item->id,
66 1
                    (string) $item->description,
67 1
                    (float) $item->amount,
68 1
                    (int) $item->quantity,
69 1
                    isset($item->shippingCost) ? (float) $item->shippingCost : null,
70 1
                    isset($item->weight) ? (int) $item->weight : null
71
                )
72
            );
73
        }
74
75 1
        return $items;
76
    }
77
78
    /**
79
     * @param SimpleXMLElement $shipping
80
     * @return Shipping
81
     */
82 1
    protected function createShipping(SimpleXMLElement $shipping)
83
    {
84 1
        return new Shipping(
85 1
            (int) $shipping->type,
86 1
            isset($shipping->address) ? $this->createAddress($shipping->address) : null,
87 1
            isset($shipping->cost) ? (float) $shipping->cost : null
88
        );
89
    }
90
}
91