CustomerDetailsTrait::appendCustomerDetailsCard()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 17
ccs 16
cts 16
cp 1
crap 1
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message\RequestData;
4
5
use Omnipay\BillPay\Customer;
6
use Omnipay\BillPay\Message\AuthorizeRequest;
7
use Omnipay\Common\CreditCard;
8
use Omnipay\Common\Exception\InvalidRequestException;
9
use Omnipay\Common\Message\AbstractRequest;
10
use SimpleXMLElement;
11
12
/**
13
 * Class CustomerDetailsTrait
14
 *
15
 * @author    Andreas Lange <[email protected]>
16
 * @copyright 2016, Connox GmbH
17
 * @license   MIT
18
 */
19
trait CustomerDetailsTrait
20
{
21
    /**
22
     * Get the card.
23
     *
24
     * @return CreditCard
25
     *
26
     * @codeCoverageIgnore
27
     */
28
    abstract public function getCard();
29
30
    /**
31
     * Get the client IP address.
32
     *
33
     * @return string
34
     *
35
     * @codeCoverageIgnore
36
     */
37
    abstract public function getClientIp();
38
39
    /**
40
     * @param string $country
41
     *
42
     * @return string|null ISO-3166-1 Alpha3
43
     *
44
     * @codeCoverageIgnore
45
     */
46
    abstract public function getCountryCode($country);
47
48
    /**
49
     * @throws InvalidRequestException
50
     *
51
     * @return null|Customer
52
     */
53 13
    public function getCustomerDetails()
54
    {
55 13
        return $this->getParameter('customerDetails');
56
    }
57
58
    /**
59
     * Sets the customer detail information
60
     *
61
     * @param Customer $customer
62
     *
63
     * @return AuthorizeRequest
64
     */
65 18
    public function setCustomerDetails($customer)
66
    {
67 18
        return $this->setParameter('customerDetails', $customer);
68
    }
69
70
    /**
71
     * @param SimpleXMLElement $data
72
     *
73
     * @throws InvalidRequestException
74
     */
75 11
    protected function appendCustomerDetails(SimpleXMLElement $data)
76
    {
77 11
        $data->addChild('customer_details');
78 11
        $this->appendCustomerDetailsAdditional($data, $this->getCustomerDetails());
0 ignored issues
show
Bug introduced by
It seems like $this->getCustomerDetails() can be null; however, appendCustomerDetailsAdditional() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
79 11
        $this->appendCustomerDetailsCard($data, $this->getCard());
80 11
    }
81
82
    /**
83
     * Get a single parameter.
84
     *
85
     * @param string $key The parameter key
86
     *
87
     * @return mixed
88
     *
89
     * @codeCoverageIgnore
90
     */
91
    abstract protected function getParameter($key);
92
93
    /**
94
     * Set a single parameter
95
     *
96
     * @param string $key   The parameter key
97
     * @param mixed  $value The value to set
98
     *
99
     * @return AbstractRequest Provides a fluent interface
100
     *
101
     * @codeCoverageIgnore
102
     */
103
    abstract protected function setParameter($key, $value);
104
105
    /**
106
     * Fills additional data
107
     *
108
     * @param SimpleXMLElement $data
109
     * @param Customer         $customer
110
     */
111 11
    private function appendCustomerDetailsAdditional(SimpleXMLElement $data, Customer $customer)
112
    {
113 11
        $data->customer_details[0]['customerid'] = $customer->getId();
114 11
        $data->customer_details[0]['customertype'] = $customer->getType();
115 11
        $data->customer_details[0]['language'] = $customer->getLanguage();
116 11
        $data->customer_details[0]['ip'] = $this->getClientIp();
117 11
        $data->customer_details[0]['customerGroup'] = $customer->getGroup();
118 11
    }
119
120
    /**
121
     * Fills the card information
122
     *
123
     * @param SimpleXMLElement $data
124
     * @param CreditCard       $card
125
     */
126 11
    private function appendCustomerDetailsCard(SimpleXMLElement $data, CreditCard $card)
127
    {
128 11
        $data->customer_details[0]['salutation'] = $card->getGender();
129 11
        $data->customer_details[0]['title'] = $card->getBillingTitle();
130 11
        $data->customer_details[0]['firstName'] = $card->getBillingFirstName();
131 11
        $data->customer_details[0]['lastName'] = $card->getBillingLastName();
132 11
        $data->customer_details[0]['street'] = $card->getBillingAddress1();
133 11
        $data->customer_details[0]['streetNo'] = null;
134 11
        $data->customer_details[0]['addressAddition'] = $card->getBillingAddress2();
135 11
        $data->customer_details[0]['zip'] = $card->getBillingPostcode();
136 11
        $data->customer_details[0]['city'] = $card->getBillingCity();
137 11
        $data->customer_details[0]['country'] = $this->getCountryCode($card->getBillingCountry());
138 11
        $data->customer_details[0]['email'] = $card->getEmail();
139 11
        $data->customer_details[0]['phone'] = $card->getBillingPhone();
140 11
        $data->customer_details[0]['cellPhone'] = null;
141 11
        $data->customer_details[0]['birthday'] = $card->getBirthday('Ymd');
142 11
    }
143
}
144