Passed
Push — main ( 19313d...a29eaf )
by Dan
01:56 queued 22s
created

PurchaseRequest::getData()   F

Complexity

Conditions 14
Paths 8192

Size

Total Lines 66
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 34
c 4
b 0
f 0
dl 0
loc 66
rs 2.1
cc 14
nc 8192
nop 0

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\WindcaveHpp\Message;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
7
/**
8
 * Windcave HPP Purchase Request
9
 */
10
class PurchaseRequest extends BaseRequest
11
{
12
    public function initialize(array $parameters = [])
13
    {
14
        return parent::initialize($parameters);
15
    }
16
17
    public function getData()
18
    {
19
        $this->validate('apiUsername', 'apiKey', 'amount', 'currency');
20
21
        $data = [];
22
23
        $data['type'] = $this->getType();
24
        $data['amount'] = $this->getAmount();
25
        $data['currency'] = $this->getCurrency();
26
        $data['callbackUrls'] = [];
27
28
        if ( $this->getStoreCard() ) {
29
            $data['storeCard'] = true;
30
        }
31
32
        if ( $this->getStoredCardIndicator() ) {
33
            $data['storedCardIndicator'] = $this->getStoredCardIndicator();
34
        }
35
36
        if ( $this->getRecurringExpiry() ) {
37
            $data['recurringExpiry'] = $this->getRecurringExpiry();
38
        }
39
40
        if ( $this->getRecurringFrequency() ) {
41
            $data['recurringFrequency'] = $this->getRecurringFrequency();
42
        }
43
44
        if ( $this->getToken() ) {
45
            $data['cardId'] = $this->getToken();
46
        }
47
48
        if ( is_array($this->getPaymentMethods()) ) {
49
            $data['methods'] = $this->getPaymentMethods();
50
        }
51
52
        if ( is_array($this->getCardTypes()) ) {
53
            $data['cardTypes'] = $this->getCardTypes();
54
        }
55
56
        if ( is_array($this->getMetadata()) ) {
57
            $data['metaData'] = $this->getMetadata();
58
        }
59
60
        $merchantReference = $this->getMerchantReference() ?? $this->getDescription();
61
62
        if ( $merchantReference ) {
63
            $data['merchantReference'] = $merchantReference;
64
        }
65
66
        if ( $this->getReturnUrl() ) {
67
            $data['callbackUrls']['approved'] = $this->getReturnUrl();
68
        }
69
70
        if ( $this->getDeclineUrl() ) {
71
            $data['callbackUrls']['declined'] = $this->getDeclineUrl();
72
        }
73
74
        if ( $this->getCancelUrl() ) {
75
            $data['callbackUrls']['cancelled'] = $this->getCancelUrl();
76
        }
77
78
        if ( $this->getNotifyUrl() ) {
79
            $data['notificationUrl'] = $this->getNotifyUrl();
80
        }
81
82
        return $data;
83
    }
84
85
    public function sendData($data)
86
    {
87
        $headers = [
88
            'Accept'        => 'application/json',
89
            'Content-Type'  => 'application/json',
90
            'Authorization' => 'Basic ' . $this->getAuthorization(),
91
        ];
92
93
        $httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
94
95
        try {
96
            $responseData = json_decode($httpResponse->getBody()->getContents());
97
        } catch (\Exception $exception) {
98
            $responseData = [];
99
        }
100
101
        return $this->response = new PurchaseResponse($this, $responseData ?? []);
102
    }
103
}