Passed
Push — master ( cbde3b...f22511 )
by Abishek R
02:33
created

Card::createRecurringTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenlyst\BaseCommerce\Models;
6
7
use Carbon\Carbon;
8
use Greenlyst\BaseCommerce\ClientException;
9
use Greenlyst\BaseCommerce\Core\Helpers;
10
use Greenlyst\BaseCommerce\LogicException;
11
use Greenlyst\BaseCommerce\Traits\{HasClient, HasErrorMessages};
12
use function ArrayHelpers\{array_get, array_has};
0 ignored issues
show
Bug introduced by
The type ArrayHelpers\array_get was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type ArrayHelpers\array_has was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class Card
15
{
16
    use HasClient, HasErrorMessages;
17
18
    private $name;
19
    private $cardNumber;
20
    private $cardExpirationMonth;
21
    private $cardExpirationYear;
22
    private $token;
23
    private $status;
24
    private $billingAddress;
25
    private $creationDate;
26
    private $alias;
27
    private $expirationDate;
0 ignored issues
show
introduced by
The private property $expirationDate is not used, and could be removed.
Loading history...
28
    private $amount;
29
    private $recurring;
30
31
    const CUSTOM_FIELD_PREFIX_BANK_CARD_TRANSACTION = 'bank_card_transaction';
32
    const CUSTOM_FIELD_PREFIX_RECURRING_TRANSACTION = 'recurring_transaction';
33
34
    const STATUS_DELETED = 'DELETED';
35
    const STATUS_FAILED = 'FAILED';
36
    const STATUS_ACTIVE = 'ACTIVE';
37
38
    const URI_ADD_BANK_CARD = '/pcms/?f=API_addBankCardV4';
39
    const URI_UPDATE_BANK_CARD = '/pcms/?f=API_updateBankCardV4';
40
    const URI_DELETE_BANK_CARD = '/pcms/?f=API_deleteBankCardV4';
41
42
    /**
43
     * @return mixed
44
     */
45
    public function getName()
46
    {
47
        return $this->name;
48
    }
49
50
    /**
51
     * @param mixed $name
52
     */
53
    public function setName($name): void
54
    {
55
        $this->name = $name;
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function getCardNumber()
62
    {
63
        return $this->cardNumber;
64
    }
65
66
    /**
67
     * @param mixed $cardNumber
68
     */
69
    public function setCardNumber($cardNumber): void
70
    {
71
        $this->cardNumber = $cardNumber;
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77
    public function getCardExpirationMonth()
78
    {
79
        return $this->cardExpirationMonth;
80
    }
81
82
    /**
83
     * @param mixed $cardExpirationMonth
84
     */
85
    public function setCardExpirationMonth($cardExpirationMonth): void
86
    {
87
        $this->cardExpirationMonth = $cardExpirationMonth;
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getCardExpirationYear()
94
    {
95
        return $this->cardExpirationYear;
96
    }
97
98
    /**
99
     * @param mixed $cardExpirationYear
100
     */
101
    public function setCardExpirationYear($cardExpirationYear): void
102
    {
103
        $this->cardExpirationYear = $cardExpirationYear;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getToken()
110
    {
111
        return $this->token;
112
    }
113
114
    /**
115
     * @param mixed $token
116
     */
117
    public function setToken($token): void
118
    {
119
        $this->token = $token;
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function getStatus()
126
    {
127
        return $this->status;
128
    }
129
130
    /**
131
     * @param mixed $status
132
     */
133
    public function setStatus($status): void
134
    {
135
        $this->status = $status;
136
    }
137
138
    /**
139
     * @return Address
140
     */
141
    public function getBillingAddress()
142
    {
143
        return $this->billingAddress;
144
    }
145
146
    /**
147
     * @param Address $billingAddress
148
     */
149
    public function setBillingAddress(Address $billingAddress): void
150
    {
151
        $this->billingAddress = $billingAddress;
152
    }
153
154
    /**
155
     * @return mixed
156
     */
157
    public function getCreationDate()
158
    {
159
        return $this->creationDate;
160
    }
161
162
    /**
163
     * @param mixed $creationDate
164
     */
165
    public function setCreationDate($creationDate): void
166
    {
167
        $this->creationDate = $creationDate;
168
    }
169
170
    /**
171
     * @return mixed
172
     */
173
    public function getAlias()
174
    {
175
        return $this->alias;
176
    }
177
178
    /**
179
     * @param mixed $alias
180
     */
181
    public function setAlias($alias): void
182
    {
183
        $this->alias = $alias;
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getAmount()
190
    {
191
        return $this->amount;
192
    }
193
194
    /**
195
     * @param mixed $amount
196
     */
197
    public function setAmount($amount): void
198
    {
199
        $this->amount = $amount;
200
    }
201
202
203
204
    /**
205
     * @return Recurring
206
     */
207
    public function getRecurring()
208
    {
209
        return $this->recurring;
210
    }
211
212
    /**
213
     * @param Recurring $recurring
214
     */
215
    public function setRecurring($recurring): void
216
    {
217
        $this->recurring = $recurring;
218
    }
219
220
    /**
221
     * @return Card
222
     *
223
     * @throws ClientException
224
     * @throws LogicException
225
     */
226
    public function add(): Card
227
    {
228
        validate_array($this->toCreateCardArray(), [
229
                'bank_card_name', 'bank_card_number', 'bank_card_expiration_month', 'bank_card_expiration_year'
230
            ]
231
        );
232
233
        $response = $this->client->postRequest(self::URI_ADD_BANK_CARD, json_encode($this->toCreateCardArray()));
234
235
        $instance = $this->fromCardArray($response['bank_card']);
236
237
        $this->handleMessages($response);
238
239
        return $instance;
240
    }
241
242
    /**
243
     * @return $this
244
     *
245
     * @throws ClientException
246
     * @throws LogicException
247
     */
248
    public function update(): Card
249
    {
250
        validate_array($this->toCreateCardArray(), [
251
                'bank_card_token', 'bank_card_expiration_month', 'bank_card_expiration_year'
252
            ]
253
        );
254
255
        $response = $this->client->postRequest(self::URI_UPDATE_BANK_CARD, json_encode($this->toUpdateCardArray()));
256
257
        $instance = $this->fromCardArray($response['bank_card']);
258
259
        $instance->handleMessages($response);
260
261
        return $instance;
262
    }
263
264
    /**
265
     * @return Card
266
     *
267
     * @throws ClientException
268
     * @throws LogicException
269
     */
270
    public function delete(): Card
271
    {
272
        validate_array($this->toCreateCardArray(), ['bank_card_token']);
273
274
        $response = $this->client->postRequest(self::URI_DELETE_BANK_CARD, array_get($this->toDeleteCardArray(), 'bank_card_token'));
275
276
        $instance = $this->fromCardArray($response['bank_card']);
277
278
        $instance->handleMessages($response);
279
280
        return $instance;
281
    }
282
283
    private function fromCardArray(array $data)
284
    {
285
        $instance = new static();
286
287
        $instance->setName(array_get($data, 'bank_card_name'));
288
        $instance->setCardNumber(array_get($data, 'bank_card_number'));
289
        $instance->setCardExpirationMonth(array_get($data, 'bank_card_expiration_month'));
290
        $instance->setCardExpirationYear(array_get($data, 'bank_card_expiration_year'));
291
        $instance->setToken(array_get($data, 'bank_card_token'));
292
        $instance->setCardNumber(array_get($data, 'bank_card_number'));
293
        $instance->setBillingAddress((new Address())->fromArray(array_get($data, 'bank_card_billing_address')));
0 ignored issues
show
Bug introduced by
It seems like array_get($data, 'bank_card_billing_address') can also be of type null; however, parameter $data of Greenlyst\BaseCommerce\Models\Address::fromArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

293
        $instance->setBillingAddress((new Address())->fromArray(/** @scrutinizer ignore-type */ array_get($data, 'bank_card_billing_address')));
Loading history...
294
295
        $instance->setCreationDate(Carbon::parse(array_get($data, 'bank_card_creation_date_24hr'))->toDateTime());
296
297
        if (array_has($data, 'bank_card_status')) {
298
            if (gettype(array_get($data, 'bank_card_status')) === "array") {
299
                $instance->setStatus(array_get($data, 'bank_card_status.bank_card_status_name'));
300
            } else {
301
                $instance->setStatus(array_get($data, 'bank_card_status'));
302
            }
303
        }
304
305
        if (array_has($data, 'bank_card_deleted')) {
306
            $instance->setStatus(self::STATUS_DELETED);
307
        }
308
309
        $instance->setAlias(array_get($data, 'bank_card_alias'));
310
311
        return $instance;
312
    }
313
314
    public function toCreateCardArray(): array
315
    {
316
        return clear_array([
317
            'bank_card_name' => $this->getName(),
318
            'bank_card_number' => $this->getCardNumber(),
319
            'bank_card_expiration_month' => $this->getCardExpirationMonth(),
320
            'bank_card_expiration_year' => $this->getCardExpirationYear(),
321
            'bank_card_token' => $this->getToken(),
322
            'bank_card_billing_address' => $this->getBillingAddress() ? $this->getBillingAddress()->toArray() : null,
323
            'bank_card_alias' => $this->getAlias()
324
        ]);
325
    }
326
327
    private function toUpdateCardArray(): array
328
    {
329
        return clear_array([
330
            'bank_card_expiration_month' => $this->getCardExpirationMonth(),
331
            'bank_card_expiration_year' => $this->getCardExpirationYear(),
332
            'bank_card_token' => $this->getToken(),
333
            'bank_card_billing_address' => $this->getBillingAddress() ? $this->getBillingAddress()->toArray() : null,
334
        ]);
335
    }
336
337
    private function toDeleteCardArray(): array
338
    {
339
        return clear_array([
340
            'bank_card_token' => $this->getToken()
341
        ]);
342
    }
343
}