RedirectFlowRequest::setLanguage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Omnipay\GoCardless\Message;
4
5
class RedirectFlowRequest extends AbstractRequest
6
{
7
    public function getSessionToken()
8
    {
9
        return $this->getParameter('sessionToken');
10
    }
11
12
    public function setSessionToken($value)
13
    {
14
        return $this->setParameter('sessionToken', $value);
15
    }
16
17
    public function getAccountType()
18
    {
19
        return $this->getParameter('accountType');
20
    }
21
22
    public function setAccountType($value)
23
    {
24
        return $this->setParameter('accountType', $value);
25
    }
26
27
    public function getLanguage()
28
    {
29
        return $this->getParameter('language');
30
    }
31
32
    /**
33
     * @param string $value ISO 639-1 code, {@see http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes}
34
     */
35
    public function setLanguage($value)
36
    {
37
        return $this->setParameter('language', $value);
38
    }
39
40
    public function getDanishIdentityNumber()
41
    {
42
        return $this->getParameter('danishIdentityNumber');
43
    }
44
45
    public function setDanishIdentityNumber($value)
46
    {
47
        return $this->setParameter('danishIdentityNumber', $value);
48
    }
49
50
    public function getSwedishIdentityNumber()
51
    {
52
        return $this->getParameter('swedishIdentityNumber');
53
    }
54
  
55
    public function setSwedishIdentityNumber($value)
56
    {
57
        return $this->setParameter('swedishIdentityNumber', $value);
58
    }
59
60
    public function getMetaData()
61
    {
62
        return $this->getParameter('metadata');
63
    }
64
65
    /**
66
     * Meta data parameter is a key-value store
67
     *
68
     * Up to 3 keys are permitted, with key names up to 50 characters and values up to 500 characters
69
     *
70
     * @todo validate input and parse into a valid format
71
     */
72
    public function setMetaData($value)
73
    {
74
        return $this->setParameter('metadata', $value);
75
    }
76
77
    public function getScheme()
78
    {
79
        return $this->getParameter('scheme');
80
    }
81
82
    public function setScheme($value)
83
    {
84
        return $this->setParameter('scheme', $value);
85
    }
86
87
    public function getCreditorId()
88
    {
89
        return $this->getParameter('creditorId');
90
    }
91
92
    public function setCreditorId($value)
93
    {
94
        return $this->setParameter('creditorId', $value);
95
    }
96
97
    public function getData()
98
    {
99
        $this->validate('sessionToken', 'returnUrl');
100
101
        // Required values
102
        $data = [
103
            'session_token' => $this->getSessionToken(),
104
            'success_redirect_url' => $this->getReturnUrl(),
105
        ];
106
107
        // Optional values
108
        $prefilledBankAccount = [
109
            'account_type' => $this->getAccountType(),
110
        ];
111
        $prefilledCustomer = [
112
            'language' => $this->getLanguage(),
113
            // @todo validate against country?
114
            'danish_identity_number' => $this->getDanishIdentityNumber(),
115
            // @todo validate against country?
116
            'swedish_identity_number' => $this->getSwedishIdentityNumber(),
117
        ];
118
        $card = $this->getCard();
119
        if ($card) {
0 ignored issues
show
introduced by
$card is of type Omnipay\Common\CreditCard, thus it always evaluated to true.
Loading history...
120
            $prefilledCustomer += [
121
                'address_line1' => $card->getAddress1(),
122
                'address_line2' => $card->getAddress2(),
123
                'address_line3' => $card->getAddress3(),
124
                'city' => $card->getCity(),
125
                'postal_code' => $card->getPostcode(),
126
                'region' => $card->getState(),
127
                // ISO 3166-1 alpha-2 code
128
                'country_code' => $card->getCountry(),
129
                'email' => $card->getEmail(),
130
                'family_name' => $card->getLastName(),
131
                'given_name' => $card->getFirstName(),
132
                 // phone number is for New Zealand customers only
133
                'phone_number' => $card->getCountry() == 'NZ' ? $card->getPhone() : null,
134
            ];
135
            if ($card->getLastName() == null && $card->getLastName() == null) {
136
                $prefilledCustomer['company_name'] = $card->getCompany();
137
            }
138
        }
139
        $links = [
140
            'creditor' => $this->getCreditorId(),
141
        ];
142
        $data += array_filter([
143
            'description' => $this->getDescription(),
144
            // key-value store, ends up as native JSON rather than encoded string
145
            'metadata' => $this->getMetaData(),
146
            'prefilled_bank_account' => array_filter($prefilledBankAccount),
147
            'prefilled_customer' => array_filter($prefilledCustomer),
148
            'scheme' => $this->getScheme(),
149
            'links' => array_filter($links),
150
        ]);
151
152
        return $data;
153
    }
154
155
    /**
156
     * Send the request with specified data
157
     *
158
     * @param  mixed $data The data to send
159
     *
160
     * @return RedirectFlowResponse
161
     */
162
    public function sendData($data)
163
    {
164
        $response = $this->sendRequest(['redirect_flows' => $data]);
165
166
        return $this->response = new RedirectFlowResponse(
167
            $this,
168
            json_decode($response->getBody()->getContents(), true)
169
        );
170
    }
171
}
172