Passed
Push — master ( ad0e1d...e3f7f1 )
by Emmanuel
02:33
created

Payant::addProduct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 19

Duplication

Lines 33
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 6
nop 4
dl 33
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Laravel Payant package.
5
 *
6
 * (c) Emmanuel Awotunde <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Olaoluwa98\Payant;
13
14
use GuzzleHttp\Client;
15
use Illuminate\Support\Facades\Config;
16
// use Olaoluwa98\Payant\Exceptions\ApiRequestError;
17
// use Olaoluwa98\Payant\Exceptions\InvalidCredentials;
18
use Olaoluwa98\Payant\Exceptions\InvalidFeeBearer;
19
// use Olaoluwa98\Payant\Exceptions\InvalidParameterType;
20
use Olaoluwa98\Payant\Exceptions\IsInvalid;
21
use Olaoluwa98\Payant\Exceptions\IsNull;
22
use Olaoluwa98\Payant\Exceptions\IsNullOrInvalid;
23
use Olaoluwa98\Payant\Exceptions\RequiredValueMissing;
24
use Olaoluwa98\Payant\Exceptions\RequiredValuesMissing;
25
26
use \Exception as phpException;
27
class Payant {
28
    
29
30
    /**
31
     * @var $private_key
32
    */
33
    protected $private_key;
34
    
35
36
    /**
37
     *
38
     * @var $base_uri
39
     *
40
    */
41
    protected $base_uri = 'https://api.demo.payant.ng';
42
    
43
    
44
    /**
45
     * @var $client
46
     *
47
    */
48
    protected $client;
49
50
    
51
    /**
52
     * [__construct sets the needed variables got from Payant config file]
53
     */
54
    public function __construct()
55
    {
56
        $this->setKey();
57
        $this->setBaseUrl();
58
        $this->setRequestOptions();        
59
    }
60
61
    /**
62
    * Get Base Url from Payant config file
63
    */
64
    public function setBaseUrl()
65
    {
66
        if(Config::get('payant.mode') == 'LIVE')
67
        {
68
            $this->base_uri = "https://api.payant.ng";
69
        }
70
    }
71
72
    /**
73
     * Get private key from Payant config file
74
     */
75
    public function setKey()
76
    {
77
        $this->private_key = Config::get('payant.private_key');
78
    }
79
80
81
    /**
82
     * Set options for making the Client request
83
     */
84
    private function setRequestOptions()
85
    {       
86
       $authorization_string = 'Bearer '. $this->private_key;
87
88
        //Set up Guzzle
89
        $this->client = new Client( [
90
            'base_uri' => $this->base_uri,
91
            'protocols' => ['https'],
92
            'headers' => [
93
                'Authorization' => $authorization_string,
94
                'Content-Type'  => 'application/json',
95
                'Accept'        => 'application/json'
96
            ]
97
        ]);
98
    }
99
100
101
    /**
102
     * [getStates Get States in a country (Nigeria)]
103
     * @return object [list of banks and their respective bank_ids]
104
    */
105
    public function getBanks(){
106
        return $this->sendRequest('get', '/banks');
107
    }
108
    
109
110
    
111
    /**
112
     * [resolveAccount description]
113
     * @param array $client_data [description]
114
     * Required fields - 'settlement_bank', 'account_number'
115
    */
116
    public function resolveAccount( array $client_data){
117
        // Mandatory fields
118
        $required_values = ['settlement_bank', 'account_number'];
119
120
        if(!array_keys_exist($client_data, $required_values)){
121
         throw new RequiredValuesMissing("Missing required values :(");
122
        }
123
124
        $url = '/resolve-account';
125
126
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
127
    }
128
129
130
131
    
132
    /**
133
     * [addClient description]
134
     * @param array $client_data [description]
135
     * Required fields - 'first_name', 'last_name', 'email', 'phone'
136
     * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
137
    */
138
    public function addClient( array $client_data){
139
        // Mandatory fields
140
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
141
142
        if(!array_keys_exist($client_data, $required_values)){
143
         throw new RequiredValuesMissing("Missing required values :(");
144
        }
145
146
        $url = '/clients';
147
148
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
149
    }
150
151
    
152
153
154
    /**
155
     * [getClient Get client Details]
156
     * @param  string $client_id
157
     * @return object
158
    */
159 View Code Duplication
    public function getClient($client_id = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
        if(!$client_id){
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
161
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
162
        }
163
164
        $url = "/clients/{$client_id}";
165
166
        return $this->sendRequest('get', $url);
167
    }
168
169
    
170
171
172
173
    /**
174
    * [editClient - Edit Existing Client]
175
    * @param string $client_id
176
    * @param array $client_data
177
    *        Required fields - 'first_name', 'last_name', 'email', 'phone'
178
    *        Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
179
    */
180
    public function editClient( $client_id, array $client_data){
181
        if(!$client_id){
182
           throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
183
        }
184
185
        $url = "/clients/{$client_id}";
186
187
        // Mandatory fields
188
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
189
190
        if(!array_keys_exist($client_data, $required_values)){
191
             throw new RequiredValuesMissing("Missing required values :(");
192
        }
193
194
        return $this->sendRequest('put', $url, ['form_params' => $client_data]);
195
    }
196
197
    
198
199
200
201
    /**
202
     * [deleteClient]
203
     * @param  string $client_id [description]
204
     */
205 View Code Duplication
    public function deleteClient($client_id = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
        if(!$client_id){
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
207
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
208
        }
209
210
        $url = "/clients/{$client_id}";
211
212
        return $this->sendRequest('delete', $url);
213
    }
214
215
216
217
218
219
    /**
220
     * [addInvoice description]
221
     * @param string      $client_id   [Optional - if client_data is supplied]
222
     * @param array|null    $client_data [Optional - if client_id is supplied]
223
     *      Required Keys - 'first_name', 'last_name', 'email', 'phone'
224
     *      Optional - 'address', 'company_name', 'lga', 'state'                        
225
     * @param string      $due_date    [Mandatory, Format - DD/MM/YYYY]
226
     * @param string      $fee_bearer  [Mandatory]
227
     * @param array         $items       [Mandatory]
228
     */
229
    public function addInvoice($client_id, array $client_data, $due_date, $fee_bearer, array $items){
230
        // Mandatory Client fields
231
        $required_client_values = ['first_name', 'last_name', 'email', 'phone'];
232
        
233
234
        // Vaild fee_bearer types: 'account' and 'client'
235
        $valid_fee_bearers = ['account', 'client'];
236
237
        
238
        // Either the client Id is supplied or a new client data is provided
239
        if(!$client_id && !array_keys_exist($client_data, $required_client_values)){
240
            throw new RequiredValuesMissing("Missing required values :( - Provide client_id or client_data");
241
        }
242
243
        if(!$due_date){
244
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Due Date");
245
        }
246
247
        if(!$fee_bearer){
248
            throw new IsNull("Error Processing Request - Null Fee Bearer");
249
        }elseif (!in_array($fee_bearer, $valid_fee_bearers)) {
250
            throw new InvalidFeeBearer("Invalid Fee Bearer - Use either 'account' or 'client'");
251
        }
252
253
        if(!is_array($items)){
254
            throw new IsInvalid("Error Processing Request - Invalid Items");
255
        }
256
257
        $url = "/invoices";
258
259
        $post_data = [
260
            'due_date' => $due_date,
261
            'fee_bearer' => $fee_bearer,
262
            'items' => $items
263
        ];
264
265
        ($client_id) ? $post_data['client_id'] = $client_id : null;
266
        ($client_data) ? $post_data['client'] = $client_data : null;
267
268
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
269
    }
270
271
272
273
274
    /**
275
    * [getInvoice ]
276
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
277
    * @return object               
278
    */
279 View Code Duplication
    public function getInvoice($reference_code){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
        if(!$reference_code){
281
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
282
        }
283
284
        $url = "/invoices/{$reference_code}";
285
286
        return $this->sendRequest('get', $url);
287
    }
288
289
    /**
290
    * [sendInvoice]
291
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
292
    * @return object               
293
    */
294 View Code Duplication
    public function sendInvoice($reference_code = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
        if(!$reference_code){
0 ignored issues
show
Bug Best Practice introduced by
The expression $reference_code of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
296
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
297
        }
298
299
            $url = "/invoices/send/{$reference_code}";
300
301
            return $this->sendRequest('get', $url);
302
    }
303
304
305
306
307
308
    /**
309
    * [getInvoiceHistory]
310
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
311
    * @param  string $start  [Format - DD/MM/YYYY]
312
    * @param  string $end    [Format - DD/MM/YYYY]
313
    * @return object         
314
    */
315 View Code Duplication
    public function getInvoiceHistory($period, $start = null, $end = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
        if(!$period){
317
            throw new RequiredValueMissing("Error Processing Request - period Missing");
318
        }
319
320
        //Validate Period
321
        $valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"];
322
323
        if (!in_array($period, $valid_period_options)) {
324
            throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom");
325
        }
326
327
        $post_data = [
328
            'period' => $period
329
        ];
330
331
        if ($period == 'custom'){
332
            if (!$start || !$end){
0 ignored issues
show
Bug Best Practice introduced by
The expression $start of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $end of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
333
                throw new IsNull("Invalid custom Start or End date");
334
            }
335
            $post_data['start'] = $start;
336
            $post_data['end'] = $end;
337
        }
338
339
        $url = "/invoices/history";
340
341
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
342
    }
343
344
345
346
347
348
    /**
349
    * [deleteInvoice]
350
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
351
    * @return object                 
352
    */
353 View Code Duplication
    public function deleteInvoice($reference_code){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
        if(!$reference_code){
355
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
356
        }
357
358
        $url = "/invoices/{$reference_code}";
359
360
        return $this->sendRequest('delete', $url);
361
    }
362
363
364
365
366
367
    /**
368
     * [addTransfer description]
369
     * @param array $client_data [description]
370
     * Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number',
371
     * Optional - 'address', 'company_name', 'type',
372
     * @param string      $amount    [Mandatory]
373
     */
374
    public function addTransfer(array $client_data, string $amount){
375
        // Mandatory Client fields
376
        $required_client_values = ['first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number'];        
377
        
378
        if(!array_keys_exist($client_data, $required_client_values)){
379
            throw new RequiredValuesMissing("Missing required values :( - Provide client_data");
380
        }
381
382
        if(!$amount){
383
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
384
        }
385
386
        $url = "/transfers";
387
388
        $post_data = [
389
            'client' => $client_data,
390
            'amount' => $amount,
391
            ];
392
393
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
394
    }
395
396
397
398
399
400
    /**
401
    * [getTransfer ]
402
    * @param  string $reference_code [Mandatory - Transfer Reference Code]
403
    * @return object               
404
    */
405 View Code Duplication
    public function getTransfer($reference_code){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
406
        if(!$reference_code){
407
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
408
        }
409
410
        $url = "/transfers/{$reference_code}";
411
412
        return $this->sendRequest('get', $url);
413
    }
414
415
416
417
418
419
420
    /**
421
    * [getTransferHistory]
422
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
423
    * @param  string $start  [Format - DD/MM/YYYY]
424
    * @param  string $end    [Format - DD/MM/YYYY]
425
    * @return object         
426
    */
427 View Code Duplication
    public function getTransferHistory($period, $start = null, $end = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
428
        if(!$period){
429
            throw new RequiredValueMissing("Error Processing Request - period Missing");
430
        }
431
432
        //Validate Period
433
        $valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"];
434
435
        if (!in_array($period, $valid_period_options)) {
436
            throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom");
437
        }
438
439
        $post_data = [
440
            'period' => $period
441
        ];
442
443
        if ($period == 'custom'){
444
            if (!$start || !$end){
0 ignored issues
show
Bug Best Practice introduced by
The expression $start of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $end of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
445
                throw new IsNull("Invalid custom Start or End date");
446
            }
447
            $post_data['start'] = $start;
448
            $post_data['end'] = $end;
449
        }
450
451
        $url = "/transfers/history";
452
453
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
454
    }
455
456
457
458
459
460
    /**
461
    * [deleteTransfer]
462
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
463
    * @return object                 
464
    */
465 View Code Duplication
    public function deleteTransfer($reference_code){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
466
        if(!$reference_code){
467
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
468
        }
469
470
        $url = "/transfers/{$reference_code}";
471
472
        return $this->sendRequest('delete', $url);
473
    }
474
475
476
477
478
479
480
    /**
481
    * [addPayment]
482
    * @param string $reference_code [Mandatory - Invoice Reference Code]
483
    * @param string $date           [Mandatory - [Format - DD/MM/YYYY]]
484
    * @param string $amount         [Mandatory]
485
    * @param string $channel        [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]]
486
    */
487 View Code Duplication
    public function addPayment(string $reference_code, string $date, string $amount, string $channel){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
488
        if(!$reference_code){
489
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
490
        }
491
492
        if(!$due_date){
0 ignored issues
show
Bug introduced by
The variable $due_date does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
493
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid date");
494
        }
495
496
        if(!$amount){
497
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
498
        }
499
500
        $valid_channels = ["Cash", "BankTransfer", "POS", "Cheque"];
501
502
        if(!$channel){
503
            throw new IsNull("Error Processing Request - Null/Invalid amount");
504
        }elseif (!in_array(ucfirst($channel), $valid_channels)) {
505
            throw new IsInvalid("Invalid Channel - Cash, BankTransfer, POS or Cheque");
506
        }
507
508
        $url = "/payments";
509
510
        $post_data = [
511
            'reference_code' => $reference_code,
512
            'date' => $date,
513
            'amount' => $amount,
514
            'channel' => $channel
515
        ];
516
517
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
518
    }
519
520
521
522
523
524
    /**
525
    * [getPayment]
526
    * @param string $reference_code [Mandatory - Invoice Reference Code]
527
    */
528 View Code Duplication
    public function getPayment($reference_code){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
        if(!$reference_code){
530
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
531
        }
532
533
        $url = "/payments/{$reference_code}";
534
535
        return $this->sendRequest('get', $url);
536
    }
537
538
    
539
540
541
542
    /**
543
    * [getPaymentHistory]
544
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
545
    * @param  string $start  [Format - DD/MM/YYYY || Optional if $period !== 'custom']
546
    * @param  string $end    [Format - DD/MM/YYYY || Optional if $period !== 'custom']
547
    * @return object         
548
    */
549 View Code Duplication
    public function getPaymentHistory(string $period, string $start, string $end){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
550
        if(!$period){
551
            throw new RequiredValueMissing("Error Processing Request - period Missing");
552
        }
553
554
        //Validate Period
555
        $valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"];
556
557
        if (!in_array(strtolower($period), $valid_period_options)) {
558
            throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom");
559
        }
560
561
        $post_data = [
562
            'period' => $period
563
        ];
564
565
        if ($period == 'custom'){
566
            if (!$start || !$end){
567
                throw new IsNull("Invalid custom Start or End date");
568
            }
569
            $post_data['start'] = $start;
570
            $post_data['end'] = $end;
571
        }
572
573
        $url = "/payments/history";
574
575
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
576
    }
577
578
    
579
580
581
582
    /**
583
    * [addProduct]
584
    * @param string $name        [Mandatory - Product's name]
585
    * @param string $description [Mandatory - Product's description]
586
    * @param string $unit_cost   [Mandatory - Product's unit cost]
587
    * @param string $type        [Mandatory - Product type 'product' or 'service']
588
    */
589 View Code Duplication
    public function addProduct(string $name, string $description, string $unit_cost, string $type){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
590
        if(!$name){
591
            throw new IsNull("Error Processing Request - Null/Invalid name");
592
        }
593
594
        if(!$description){
595
            throw new IsNull("Error Processing Request - Null/Invalid description");
596
        }
597
598
        if(!$unit_cost){
599
            throw new IsNull("Error Processing Request - Null/Invalid unit_cost");
600
        }
601
602
        //Validate Product Type
603
        $valid_product_type = ["product", "service"];
604
605
        if(!$type){
606
            throw new IsNull("Error Processing Request - Null/Invalid type");
607
        }elseif (!in_array(strtolower($type), $valid_product_type)) {
608
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
609
        }
610
611
        $url = "/products";
612
613
        $post_data = [
614
            'name' => $name,
615
            'description' => $description,
616
            'unit_cost' => $unit_cost,
617
            'type' => $type
618
        ];
619
620
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
621
    }
622
623
        
624
625
626
627
    /**
628
    * [getProduct]
629
    * @param  int $product_id [Mandatory - Product ID]
630
    * @return object 
631
    */
632 View Code Duplication
    public function getProduct($product_id){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
633
        if(!$product_id){
634
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid product_id");
635
        }
636
637
        $url = "/products/{$product_id}";
638
639
        return $this->sendRequest('get', $url);
640
    }
641
642
    
643
644
645
646
    /**
647
    * [editProduct]
648
    * @param  string $product_id   [Mandatory - Product ID]
649
    * @param  array  $product_data [description]
650
    * @return object               
651
    */
652
    public function editProduct($product_id, array $product_data){
653
        if(!$product_id){
654
               throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
655
        }
656
657
        //Validate Type
658
        $product_type = strtolower(array_get($product_data, 'type', 'none'));
659
660
        $valid_product_type = ["product", "service"];
661
662
        if(!$product_type){
663
            throw new IsNull("Error Processing Request - Null/Invalid type");
664
        }elseif (!in_array($product_type, $valid_product_type)) {
665
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
666
        }
667
668
       $url = "/products/{$product_id}";
669
670
       // Mandatory fields
671
       $required_values = ['name', 'description', 'unit_cost', 'type'];
672
673
        if(!array_keys_exist($client_data, $required_values)){
0 ignored issues
show
Bug introduced by
The variable $client_data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
674
             throw new RequiredValuesMissing("Missing required values :(");
675
        }
676
677
        return $this->sendRequest('put', $url, ['form_params' => $post_data]);
0 ignored issues
show
Bug introduced by
The variable $post_data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
678
    }
679
680
    
681
682
683
    /**
684
    * [getProducts]
685
    * @return object
686
    */
687
    public function getProducts(){
688
        $url = "/products";
689
690
        return $this->sendRequest('get', $url);
691
    }
692
693
        
694
695
696
    /**
697
    * [deleteProduct]
698
    * @param $product_id [Mandatory - Product ID]
699
    * @return object           
700
    */
701 View Code Duplication
    public function deleteProduct($product_id){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
702
        if(!$product_id){
703
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
704
        }
705
706
        $url = "/products/{$product_id}";
707
708
        return $this->sendRequest('delete', $url);
709
    }
710
711
712
713
    /**
714
    * [addPayment]
715
    * @param string $method       [Mandatory - request method <get | post | put | delete> ]
716
    * @param string $url           [Mandatory - url to send request to]
717
    * @param array $params         [data to post to request url]
718
    */
719
    public function sendRequest($method, $url, $params=[])
720
    {
721
        try{
722
            if (strtolower($method) == 'get'){
723
                $result = $this->client->request('GET', $url);
724
            }elseif (strtolower($method) == 'post'){
725
                $result = $this->client->request('POST', $url, $params);
726
            }elseif (strtolower($method) == 'put'){
727
                $result = $this->client->request('PUT', $url, $params);
728
            }elseif (strtolower($method) == 'delete'){
729
                $result = $this->client->request('DELETE', $url);
730
            }
731
732
            return cleanResponse($result);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
733
        }
734
        catch( Exception $e){
0 ignored issues
show
Bug introduced by
The class Olaoluwa98\Payant\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
735
            throw $e;
736
        }
737
    }
738
}
739