ShippingDetailsTrait::fillShippingAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 18
ccs 17
cts 17
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message\RequestData;
4
5
use Omnipay\Common\CreditCard;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
use SimpleXMLElement;
8
9
/**
10
 * Class ShippingDetailsTrait
11
 *
12
 * @author    Andreas Lange <[email protected]>
13
 * @copyright 2016, Connox GmbH
14
 * @license   MIT
15
 */
16
trait ShippingDetailsTrait
17
{
18
    /**
19
     * Get the card.
20
     *
21
     * @return CreditCard
22
     *
23
     * @codeCoverageIgnore
24
     */
25
    abstract public function getCard();
26
27
    /**
28
     * @param string $country
29
     *
30
     * @return string|null ISO-3166-1 Alpha3
31
     *
32
     * @codeCoverageIgnore
33
     */
34
    abstract public function getCountryCode($country);
35
36
    /**
37
     * @param SimpleXMLElement $data
38
     *
39
     * @throws InvalidRequestException
40
     */
41 11
    protected function appendShippingDetails(SimpleXMLElement $data)
42
    {
43 11
        if ($this->hasSharedAddress()) {
44 10
            $this->fillSharedShippingDetails($data);
45 10
        } else {
46 1
            $this->fillShippingAddress($data);
47
        }
48 11
    }
49
50
    /**
51
     * @return bool
52
     */
53 11
    protected function hasSharedAddress()
54
    {
55 11
        $card = $this->getCard();
56
57 11
        foreach (['Title', 'FirstName', 'LastName', 'Address1', 'Address2', 'Postcode', 'City', 'Country'] as $check) {
58 11
            if ($card->{'getBilling' . $check}() !== $card->{'getShipping' . $check}()) {
59 1
                return false;
60
            }
61 11
        }
62
63 10
        return true;
64
    }
65
66
    /**
67
     * Fills the shipping address for shared addresses
68
     *
69
     * @param SimpleXMLElement $data
70
     */
71 10
    private function fillSharedShippingDetails(SimpleXMLElement $data)
72
    {
73 10
        $data->addChild('shipping_details');
74 10
        $data->shipping_details[0]['useBillingAddress'] = 1;
75 10
        $data->shipping_details[0]['salutation'] = null;
76 10
        $data->shipping_details[0]['title'] = null;
77 10
        $data->shipping_details[0]['firstName'] = null;
78 10
        $data->shipping_details[0]['lastName'] = null;
79 10
        $data->shipping_details[0]['street'] = null;
80 10
        $data->shipping_details[0]['streetNo'] = null;
81 10
        $data->shipping_details[0]['addressAddition'] = null;
82 10
        $data->shipping_details[0]['zip'] = null;
83 10
        $data->shipping_details[0]['city'] = null;
84 10
        $data->shipping_details[0]['country'] = null;
85 10
        $data->shipping_details[0]['phone'] = null;
86 10
        $data->shipping_details[0]['cellPhone'] = null;
87 10
    }
88
89
    /**
90
     * Fills the xml nodes with the shipping address data
91
     *
92
     * @param SimpleXMLElement $data
93
     */
94 1
    private function fillShippingAddress(SimpleXMLElement $data)
95
    {
96 1
        $card = $this->getCard();
97 1
        $data->addChild('shipping_details');
98 1
        $data->shipping_details[0]['useBillingAddress'] = 0;
99 1
        $data->shipping_details[0]['salutation'] = $card->getGender();
100 1
        $data->shipping_details[0]['title'] = $card->getShippingTitle();
101 1
        $data->shipping_details[0]['firstName'] = $card->getShippingFirstName();
102 1
        $data->shipping_details[0]['lastName'] = $card->getShippingLastName();
103 1
        $data->shipping_details[0]['street'] = $card->getShippingAddress1();
104 1
        $data->shipping_details[0]['streetNo'] = null;
105 1
        $data->shipping_details[0]['addressAddition'] = $card->getShippingAddress2();
106 1
        $data->shipping_details[0]['zip'] = $card->getShippingPostcode();
107 1
        $data->shipping_details[0]['city'] = $card->getShippingCity();
108 1
        $data->shipping_details[0]['country'] = $this->getCountryCode($card->getShippingCountry());
109 1
        $data->shipping_details[0]['phone'] = $card->getShippingPhone();
110 1
        $data->shipping_details[0]['cellPhone'] = null;
111 1
    }
112
}
113