Completed
Push — develop ( 980f1c...19a8b8 )
by Luís
08:45
created

CheckoutSerializer::appendOrder()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 10
nc 8
nop 2
crap 4
1
<?php
2
namespace PHPSC\PagSeguro\Requests\Checkout;
3
4
use PHPSC\PagSeguro\Requests\Checkout\Checkout;
5
use PHPSC\PagSeguro\Requests\Serializer;
6
use SimpleXMLElement;
7
8
/**
9
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
10
 */
11
class CheckoutSerializer extends Serializer
12
{
13
    /**
14
     * @param Checkout $checkout
15
     *
16
     * @return SimpleXMLElement
17
     */
18 3
    public function serialize(Checkout $checkout)
19
    {
20 3
        $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><checkout />');
21
22 3
        $this->appendCheckoutData($xml, $checkout);
23
24 3
        return $xml;
25
    }
26
27
    /**
28
     * @param SimpleXMLElement $xml
29
     * @param Checkout $checkout
30
     */
31 3
    private function appendCheckoutData(SimpleXMLElement $xml, Checkout $checkout)
32
    {
33 3
        $this->appendOrder($xml, $checkout->getOrder());
34 3
        $this->appendCustomer($xml, $checkout->getCustomer());
35
36 3
        if ($redirectTo = $checkout->getRedirectTo()) {
37 1
            $xml->addChild('redirectURL', $redirectTo);
38 1
        }
39
40 3
        if ($maxUses = $checkout->getMaxUses()) {
41 1
            $xml->addChild('maxUses', $maxUses);
42 1
        }
43
44 3
        if ($maxAge = $checkout->getMaxAge()) {
45 1
            $xml->addChild('maxAge', $maxAge);
46 1
        }
47 3
48
        if ($notificationURL = $checkout->getNotificationURL()) {
49
            $xml->addChild('notificationURL', $notificationURL);
50
        }
51
    }
52
53 3
    /**
54
     * @param SimpleXMLElement $xml
55 3
     * @param Order $order
56
     */
57 3
    private function appendOrder(SimpleXMLElement $xml, Order $order)
58
    {
59 3
        $xml->addChild('currency', $order->getCurrency());
60 1
61 3
        $items = $xml->addChild('items');
62
63 3
        foreach ($order->getItems() as $item) {
64 1
            $this->appendItem($items, $item);
65 1
        }
66
67 3
        if ($reference = $order->getReference()) {
68 1
            $xml->addChild('reference', $reference);
69 1
        }
70
71 3
        if ($extraAmount = $order->getExtraAmount()) {
72 3
            $xml->addChild('extraAmount', $extraAmount);
73
        }
74
75
        $this->appendShipping($xml, $order->getShipping());
76
    }
77
}
78