Completed
Push — master ( 39f173...ce7098 )
by Leith
02:17
created

PurchaseRequest::getData()   F

Complexity

Conditions 17
Paths 514

Size

Total Lines 103

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 72
CRAP Score 17

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 72
cts 72
cp 1
rs 1.3798
c 0
b 0
f 0
cc 17
nc 514
nop 0
crap 17

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Omnipay\CapitaPay360\Message;
4
5
use SoapClient;
6
use Omnipay\Common\Message\AbstractRequest;
7
use Omnipay\CapitaPay360\ItemBag;
8
9
/**
10
 * CapitaPay360 Purchase Request
11
 */
12
class PurchaseRequest extends AbstractRequest
13
{
14
    /** @var string */
15
    protected $liveWSDL = 'https://sbs.e-paycapita.com/scp/scpws/scpSimpleClient.wsdl';
16
    /** @var string */
17
    protected $testWSDL = 'https://sbsctest.e-paycapita.com/scp/scpws/scpSimpleClient.wsdl';
18
    /** @var mixed Soap client, added as property for unit testing and error checking */
19
    public $soapClient = null;
20
21 8
    public function getHmacKey()
22
    {
23 8
        return $this->getParameter('hmacKey');
24
    }
25
26 11
    public function setHmacKey($value)
27
    {
28 11
        return $this->setParameter('hmacKey', $value);
29
    }
30
31 8
    public function getHmacKeyID()
32
    {
33 8
        return $this->getParameter('hmacKeyID');
34
    }
35
36 11
    public function setHmacKeyID($value)
37
    {
38 11
        return $this->setParameter('hmacKeyID', $value);
39
    }
40
41 8
    public function getScpId()
42
    {
43 8
        return $this->getParameter('scpId');
44
    }
45
46 11
    public function setScpId($value)
47
    {
48 11
        return $this->setParameter('scpId', $value);
49
    }
50
51 8
    public function getSiteId()
52
    {
53 8
        return $this->getParameter('siteId');
54
    }
55
56 11
    public function setSiteId($value)
57
    {
58 11
        return $this->setParameter('siteId', $value);
59
    }
60
61
    /**
62
     * Override {@see AbstractRequest::setItems()} to provide additional attributes
63
     */
64 1
    public function setItems($items)
65
    {
66 1
        if ($items && !$items instanceof ItemBag) {
67 1
            $items = new ItemBag($items);
68
        }
69
70 1
        return $this->setParameter('items', $items);
71
    }
72
73 4
    public function getData()
74
    {
75 4
        $this->validate('scpId', 'siteId', 'hmacKeyID', 'hmacKey', 'amount', 'currency');
76 4
        $timeStamp = gmdate("YmdHis");
77 4
        $uniqueReference = uniqid('PB');
78 4
        $subjectType = 'CapitaPortal';
79 4
        $algorithm = 'Original';
80 4
        $credentialsToHash = implode('!', array(
81 4
            $subjectType,
82 4
            $this->getScpId(),
83 4
            $uniqueReference,
84 4
            $timeStamp,
85 4
            $algorithm,
86 4
            $this->getHmacKeyId()
87
        ));
88 4
        $key = base64_decode($this->getHmacKey());
89 4
        $hash = hash_hmac('sha256', $credentialsToHash, $key, true);
90 4
        $digest = base64_encode($hash);
91
92
        $data = array(
93
            'credentials' => array(
94
                'subject' => array(
95 4
                    'subjectType' => $subjectType,
96 4
                    'identifier' => $this->getScpId(),
97 4
                    'systemCode' => 'SCP'
98
                ),
99
                'requestIdentification' => array(
100 4
                    'uniqueReference' => $uniqueReference,
101 4
                    'timeStamp' => $timeStamp
102
                ),
103
                'signature' => array(
104 4
                    'algorithm' => $algorithm,
105 4
                    'hmacKeyID' => $this->getHmacKeyId(),
106 4
                    'digest' => $digest
107
                )
108
            ),
109 4
            'requestType' => 'payOnly',
110 4
            'requestId' => $this->getTransactionId(),
111
            'routing' => array(
112 4
                'returnUrl' => $this->getReturnUrl(),
113 4
                'backUrl' => $this->getCancelUrl(),
114 4
                'siteId' => $this->getSiteId(),
115 4
                'scpId' => $this->getScpId()
116
            ),
117 4
            'panEntryMethod' => 'ECOM',
118
            'sale' => array(
119
                'saleSummary' => array(
120 4
                    'description' => substr($this->getDescription(), 0, 100),
121 4
                    'amountInMinorUnits' => $this->getAmountInteger()
122
                )
123
            )
124
        );
125
126
        // Create items array to return
127 4
        $items = $this->getItems();
128 4
        if ($items) {
129 1
            $saleItems = array();
130 1
            foreach ($items as $itemIndex => $item) {
131 1
                $reference = $item->getReference();
132 1
                $additionalReference = $item->getAdditionalReference();
133 1
                $fundCode = $item->getFundCode();
134 1
                $narrative = $item->getNarrative();
135 1
                $lgItemDetails = ($additionalReference ? array('additionalReference' => $additionalReference) : array())
136 1
                    + ($fundCode ? array('fundCode' => $fundCode) : array())
137 1
                    + ($narrative ? array('narrative' => $narrative) : array());
138 1
                $saleItems[] = array(
139 1
                    'itemSummary' => array(
140 1
                        'description' => substr($item->getName(), 0, 100),
141 1
                        'amountInMinorUnits' => (int) round(
142 1
                            $item->getQuantity() * $item->getPrice() * pow(10, $this->getCurrencyDecimalPlaces())
143
                        ),
144 1
                    ) + ($reference ? array('reference' => $reference) : array()),
145 1
                    'quantity' => $item->getQuantity(),
146 1
                    'lineId' => $itemIndex + 1
147 1
                ) + ($lgItemDetails ? array('lgItemDetails' => $lgItemDetails) : array());
148
            }
149
            // only supply item detail if there are any items
150 1
            $data['sale']['items'] = $saleItems;
151
        }
152
153
        // add card holder details if available
154 4
        $card = $this->getCard();
155 4
        if ($card) {
156 1
            $name = $card->getName();
157 1
            $address1 = $card->getAddress1();
158 1
            $address2 = $card->getAddress2();
159 1
            $address3 = $card->getCity();
160 1
            $country = $card->getCountry();
161 1
            $postcode = $card->getPostcode();
162 1
            $address = ($address1 ? array('address1' => $address1) : array())
163 1
                + ($address2 ? array('address2' => $address2) : array())
164 1
                + ($address3 ? array('address3' => $address3) : array())
165 1
                + ($country ? array('country' => $country) : array())
166 1
                + ($postcode ? array('postcode' => $postcode) : array());
167 1
            $cardHolderDetails = ($name ? array('cardHolderName' => $name) : array())
168 1
                + ($address ? array('address' => $address) : array());
169 1
            if ($cardHolderDetails) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cardHolderDetails of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
170 1
                $data['billing'] = array('cardHolderDetails' => $cardHolderDetails);
171
            }
172
        }
173
174 4
        return $data;
175
    }
176
177 1
    public function sendData($data)
178
    {
179 1
        $responseData = $this->getSoapClient()->scpSimpleInvoke($data);
180
181 1
        return $this->response = new PurchaseResponse($this, $responseData);
182
    }
183
184 2
    protected function getSoapClient()
185
    {
186 2
        return $this->soapClient === null ? new SoapClient($this->getEndpoint(), array()) : $this->soapClient;
187
    }
188
189 1
    public function getEndpoint()
190
    {
191 1
        return $this->getTestMode() ? $this->testWSDL : $this->liveWSDL;
192
    }
193
}
194