Passed
Push — master ( e3f7f1...6131eb )
by Emmanuel
02:18
created

Payant::getPaymentHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 12
loc 12
rs 9.4285
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
use Exception;
26
class Payant {
27
    
28
29
    /**
30
     * @var $private_key
31
    */
32
    protected $private_key;
33
    
34
35
    /**
36
     *
37
     * @var $base_uri
38
     *
39
    */
40
    protected $base_uri = 'https://api.demo.payant.ng';
41
    
42
    
43
    /**
44
     * @var $client
45
     *
46
    */
47
    protected $client;
48
49
    
50
    /**
51
     * [__construct sets the needed variables got from Payant config file]
52
     */
53
    public function __construct()
54
    {
55
        $this->setKey();
56
        $this->setBaseUrl();
57
        $this->setRequestOptions();        
58
    }
59
60
    /**
61
    * Get Base Url from Payant config file
62
    */
63
    public function setBaseUrl()
64
    {
65
        if(Config::get('payant.mode') == 'LIVE')
66
        {
67
            $this->base_uri = "https://api.payant.ng";
68
        }
69
    }
70
71
    /**
72
     * Get private key from Payant config file
73
     */
74
    public function setKey()
75
    {
76
        $this->private_key = Config::get('payant.private_key');
77
    }
78
79
80
    /**
81
     * Set options for making the Client request
82
     */
83
    private function setRequestOptions()
84
    {       
85
       $authorization_string = 'Bearer '. $this->private_key;
86
87
        //Set up Guzzle
88
        $this->client = new Client( [
89
            'base_uri' => $this->base_uri,
90
            'protocols' => ['https'],
91
            'headers' => [
92
                'Authorization' => $authorization_string,
93
                'Content-Type'  => 'application/json',
94
                'Accept'        => 'application/json'
95
            ]
96
        ]);
97
    }
98
99
100
    /**
101
     * [getStates Get States in a country (Nigeria)]
102
     * @return object [list of banks and their respective bank_ids]
103
    */
104
    public function getBanks(){
105
        return $this->sendRequest('get', '/banks');
106
    }
107
    
108
109
    
110
    /**
111
     * [resolveAccount description]
112
     * @param array $client_data [description]
113
     * Required fields - 'settlement_bank', 'account_number'
114
    */
115
    public function resolveAccount( array $client_data){
116
        // Mandatory fields
117
        $required_values = ['settlement_bank', 'account_number'];
118
119
        if(!array_keys_exist($client_data, $required_values)){
120
         throw new RequiredValuesMissing("Missing required values :(");
121
        }
122
123
        $url = '/resolve-account';
124
125
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
126
    }
127
128
129
130
    
131
    /**
132
     * [addClient description]
133
     * @param array $client_data [description]
134
     * Required fields - 'first_name', 'last_name', 'email', 'phone'
135
     * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
136
    */
137
    public function addClient( array $client_data){
138
        // Mandatory fields
139
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
140
141
        if(!array_keys_exist($client_data, $required_values)){
142
         throw new RequiredValuesMissing("Missing required values :(");
143
        }
144
145
        $url = '/clients';
146
147
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
148
    }
149
150
    
151
152
153
    /**
154
     * [getClient Get client Details]
155
     * @param  string $client_id
156
     * @return object
157
    */
158 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...
159
        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...
160
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
161
        }
162
163
        $url = "/clients/{$client_id}";
164
165
        return $this->sendRequest('get', $url);
166
    }
167
168
    
169
170
171
172
    /**
173
    * [editClient - Edit Existing Client]
174
    * @param string $client_id
175
    * @param array $client_data
176
    *        Required fields - 'first_name', 'last_name', 'email', 'phone'
177
    *        Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
178
    */
179
    public function editClient( $client_id, array $client_data){
180
        if(!$client_id){
181
           throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
182
        }
183
184
        $url = "/clients/{$client_id}";
185
186
        // Mandatory fields
187
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
188
189
        if(!array_keys_exist($client_data, $required_values)){
190
             throw new RequiredValuesMissing("Missing required values :(");
191
        }
192
193
        return $this->sendRequest('put', $url, ['form_params' => $client_data]);
194
    }
195
196
    
197
198
199
200
    /**
201
     * [deleteClient]
202
     * @param  string $client_id [description]
203
     */
204 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...
205
        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...
206
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
207
        }
208
209
        $url = "/clients/{$client_id}";
210
211
        return $this->sendRequest('delete', $url);
212
    }
213
214
215
216
217
218
    /**
219
     * [addInvoice description]
220
     * @param string      $client_id   [Optional - if client_data is supplied]
221
     * @param array|null    $client_data [Optional - if client_id is supplied]
222
     *      Required Keys - 'first_name', 'last_name', 'email', 'phone'
223
     *      Optional - 'address', 'company_name', 'lga', 'state'                        
224
     * @param string      $due_date    [Mandatory, Format - DD/MM/YYYY]
225
     * @param string      $fee_bearer  [Mandatory]
226
     * @param array         $items       [Mandatory]
227
     */
228
    public function addInvoice($client_id, array $client_data, $due_date, $fee_bearer, array $items){
229
        // Mandatory Client fields
230
        $required_client_values = ['first_name', 'last_name', 'email', 'phone'];
231
        
232
233
        // Vaild fee_bearer types: 'account' and 'client'
234
        $valid_fee_bearers = ['account', 'client'];
235
236
        
237
        // Either the client Id is supplied or a new client data is provided
238
        if(!$client_id && !array_keys_exist($client_data, $required_client_values)){
239
            throw new RequiredValuesMissing("Missing required values :( - Provide client_id or client_data");
240
        }
241
242
        if(!$due_date){
243
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Due Date");
244
        }
245
246
        if(!$fee_bearer){
247
            throw new IsNull("Error Processing Request - Null Fee Bearer");
248
        }elseif (!in_array($fee_bearer, $valid_fee_bearers)) {
249
            throw new InvalidFeeBearer("Invalid Fee Bearer - Use either 'account' or 'client'");
250
        }
251
252
        if(!is_array($items)){
253
            throw new IsInvalid("Error Processing Request - Invalid Items");
254
        }
255
256
        $url = "/invoices";
257
258
        $post_data = [
259
            'due_date' => $due_date,
260
            'fee_bearer' => $fee_bearer,
261
            'items' => $items
262
        ];
263
264
        ($client_id) ? $post_data['client_id'] = $client_id : null;
265
        ($client_data) ? $post_data['client'] = $client_data : null;
266
267
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
268
    }
269
270
271
272
273
    /**
274
    * [getInvoice ]
275
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
276
    * @return object               
277
    */
278 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...
279
        if(!$reference_code){
280
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
281
        }
282
283
        $url = "/invoices/{$reference_code}";
284
285
        return $this->sendRequest('get', $url);
286
    }
287
288
    /**
289
    * [sendInvoice]
290
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
291
    * @return object               
292
    */
293 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...
294
        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...
295
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
296
        }
297
298
            $url = "/invoices/send/{$reference_code}";
299
300
            return $this->sendRequest('get', $url);
301
    }
302
303
304
305
306
307
    /**
308
    * [getInvoiceHistory]
309
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
310
    * @param  string $start  [Format - DD/MM/YYYY]
311
    * @param  string $end    [Format - DD/MM/YYYY]
312
    * @return object         
313
    */
314 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...
315
        if(!$period){
316
            throw new RequiredValueMissing("Error Processing Request - period Missing");
317
        }
318
319
        check($period, $start, $end);        
320
321
        $url = "/invoices/history";
322
323
        return $this->sendRequest('post', $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...
324
    }
325
326
327
328
329
330
    /**
331
    * [deleteInvoice]
332
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
333
    * @return object                 
334
    */
335 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...
336
        if(!$reference_code){
337
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
338
        }
339
340
        $url = "/invoices/{$reference_code}";
341
342
        return $this->sendRequest('delete', $url);
343
    }
344
345
346
347
348
349
    /**
350
     * [addTransfer description]
351
     * @param array $client_data [description]
352
     * Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number',
353
     * Optional - 'address', 'company_name', 'type',
354
     * @param string      $amount    [Mandatory]
355
     */
356
    public function addTransfer(array $client_data, string $amount){
357
        // Mandatory Client fields
358
        $required_client_values = ['first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number'];        
359
        
360
        if(!array_keys_exist($client_data, $required_client_values)){
361
            throw new RequiredValuesMissing("Missing required values :( - Provide client_data");
362
        }
363
364
        if(!$amount){
365
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
366
        }
367
368
        $url = "/transfers";
369
370
        $post_data = [
371
            'client' => $client_data,
372
            'amount' => $amount,
373
            ];
374
375
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
376
    }
377
378
379
380
381
382
    /**
383
    * [getTransfer ]
384
    * @param  string $reference_code [Mandatory - Transfer Reference Code]
385
    * @return object               
386
    */
387 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...
388
        if(!$reference_code){
389
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
390
        }
391
392
        $url = "/transfers/{$reference_code}";
393
394
        return $this->sendRequest('get', $url);
395
    }
396
397
398
399
400
401
402
    /**
403
    * [getTransferHistory]
404
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
405
    * @param  string $start  [Format - DD/MM/YYYY]
406
    * @param  string $end    [Format - DD/MM/YYYY]
407
    * @return object         
408
    */
409 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...
410
        if(!$period){
411
            throw new RequiredValueMissing("Error Processing Request - period Missing");
412
        }
413
414
        // validate period
415
        check($period, $start, $end);
416
417
        $url = "/transfers/history";
418
419
        return $this->sendRequest('post', $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...
420
    }
421
422
423
424
425
426
    /**
427
    * [deleteTransfer]
428
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
429
    * @return object                 
430
    */
431 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...
432
        if(!$reference_code){
433
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
434
        }
435
436
        $url = "/transfers/{$reference_code}";
437
438
        return $this->sendRequest('delete', $url);
439
    }
440
441
442
443
444
445
446
    /**
447
    * [addPayment]
448
    * @param string $reference_code [Mandatory - Invoice Reference Code]
449
    * @param string $due_date           [Mandatory - [Format - DD/MM/YYYY]]
450
    * @param string $amount         [Mandatory]
451
    * @param string $channel        [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]]
452
    */
453 View Code Duplication
    public function addPayment(string $reference_code, string $due_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...
454
        if(!$reference_code){
455
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
456
        }
457
458
        if(!$due_date){
459
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid date");
460
        }
461
462
        if(!$amount){
463
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
464
        }
465
466
        $valid_channels = ["Cash", "BankTransfer", "POS", "Cheque"];
467
468
        if(!$channel){
469
            throw new IsNull("Error Processing Request - Null/Invalid amount");
470
        }elseif (!in_array(ucfirst($channel), $valid_channels)) {
471
            throw new IsInvalid("Invalid Channel - Cash, BankTransfer, POS or Cheque");
472
        }
473
474
        $url = "/payments";
475
476
        $post_data = [
477
            'reference_code' => $reference_code,
478
            'date' => $due_date,
479
            'amount' => $amount,
480
            'channel' => $channel
481
        ];
482
483
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
484
    }
485
486
487
488
489
490
    /**
491
    * [getPayment]
492
    * @param string $reference_code [Mandatory - Invoice Reference Code]
493
    */
494 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...
495
        if(!$reference_code){
496
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
497
        }
498
499
        $url = "/payments/{$reference_code}";
500
501
        return $this->sendRequest('get', $url);
502
    }
503
504
    
505
506
507
508
    /**
509
    * [getPaymentHistory]
510
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
511
    * @param  string $start  [Format - DD/MM/YYYY || Optional if $period !== 'custom']
512
    * @param  string $end    [Format - DD/MM/YYYY || Optional if $period !== 'custom']
513
    * @return object         
514
    */
515 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...
516
        if(!$period){
517
            throw new RequiredValueMissing("Error Processing Request - period Missing");
518
        }
519
520
        // validate period
521
        check($period, $start, $end);
522
523
        $url = "/payments/history";
524
525
        return $this->sendRequest('post', $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...
526
    }
527
528
    
529
530
531
532
    /**
533
    * [addProduct]
534
    * @param string $name        [Mandatory - Product's name]
535
    * @param string $description [Mandatory - Product's description]
536
    * @param string $unit_cost   [Mandatory - Product's unit cost]
537
    * @param string $type        [Mandatory - Product type 'product' or 'service']
538
    */
539 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...
540
        if(!$name){
541
            throw new IsNull("Error Processing Request - Null/Invalid name");
542
        }
543
544
        if(!$description){
545
            throw new IsNull("Error Processing Request - Null/Invalid description");
546
        }
547
548
        if(!$unit_cost){
549
            throw new IsNull("Error Processing Request - Null/Invalid unit_cost");
550
        }
551
552
        //Validate Product Type
553
        $valid_product_type = ["product", "service"];
554
555
        if(!$type){
556
            throw new IsNull("Error Processing Request - Null/Invalid type");
557
        }elseif (!in_array(strtolower($type), $valid_product_type)) {
558
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
559
        }
560
561
        $url = "/products";
562
563
        $post_data = [
564
            'name' => $name,
565
            'description' => $description,
566
            'unit_cost' => $unit_cost,
567
            'type' => $type
568
        ];
569
570
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
571
    }
572
573
        
574
575
576
577
    /**
578
    * [getProduct]
579
    * @param  int $product_id [Mandatory - Product ID]
580
    * @return object 
581
    */
582 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...
583
        if(!$product_id){
584
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid product_id");
585
        }
586
587
        $url = "/products/{$product_id}";
588
589
        return $this->sendRequest('get', $url);
590
    }
591
592
    
593
594
595
596
    /**
597
    * [editProduct]
598
    * @param  string $product_id   [Mandatory - Product ID]
599
    * @param  array  $product_data [description]
600
    * @return object               
601
    */
602
    public function editProduct($product_id, array $product_data){
603
        if(!$product_id){
604
               throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
605
        }
606
607
        //Validate Type
608
        $product_type = strtolower(array_get($product_data, 'type', 'none'));
609
610
        $valid_product_type = ["product", "service"];
611
612
        if(!$product_type){
613
            throw new IsNull("Error Processing Request - Null/Invalid type");
614
        }elseif (!in_array($product_type, $valid_product_type)) {
615
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
616
        }
617
618
       $url = "/products/{$product_id}";
619
620
       // Mandatory fields
621
       $required_values = ['name', 'description', 'unit_cost', 'type'];
622
623
        if(!array_keys_exist($product_data, $required_values)){
624
             throw new RequiredValuesMissing("Missing required values :(");
625
        }
626
627
        return $this->sendRequest('put', $url, ['form_params' => $product_data]);
628
    }
629
630
    
631
632
633
    /**
634
    * [getProducts]
635
    * @return object
636
    */
637
    public function getProducts(){
638
        $url = "/products";
639
640
        return $this->sendRequest('get', $url);
641
    }
642
643
        
644
645
646
    /**
647
    * [deleteProduct]
648
    * @param $product_id [Mandatory - Product ID]
649
    * @return object           
650
    */
651 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...
652
        if(!$product_id){
653
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
654
        }
655
656
        $url = "/products/{$product_id}";
657
658
        return $this->sendRequest('delete', $url);
659
    }
660
661
662
663
    /**
664
    * [addPayment]
665
    * @param string $method       [Mandatory - request method <get | post | put | delete> ]
666
    * @param string $url           [Mandatory - url to send request to]
667
    * @param array $params         [data to post to request url]
668
    */
669
    public function sendRequest($method, $url, $params=[])
670
    {
671
        try{
672
            if (strtolower($method) == 'get'){
673
                $result = $this->client->request('GET', $url);
674
            }elseif (strtolower($method) == 'post'){
675
                $result = $this->client->request('POST', $url, $params);
676
            }elseif (strtolower($method) == 'put'){
677
                $result = $this->client->request('PUT', $url, $params);
678
            }elseif (strtolower($method) == 'delete'){
679
                $result = $this->client->request('DELETE', $url);
680
            }
681
682
            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...
683
        }
684
        catch( Exception $e){
685
            throw $e;
686
        }
687
    }
688
}
689