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

CheckoutSerializer::appendCheckoutData()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 8.7624
cc 5
eloc 11
nc 16
nop 2
crap 5
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