1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Zoho\Subscription\Api; |
5
|
|
|
|
6
|
|
|
use Zoho\Subscription\Client\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Hang Pham <[email protected]> |
10
|
|
|
* @author Tristan Bessoussa <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @link https://www.zoho.com/subscriptions/api/v1/#invoices |
13
|
|
|
*/ |
14
|
|
|
class Invoice extends Client |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $customerId The customer's id |
18
|
|
|
* |
19
|
|
|
* @throws \Exception |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function listInvoicesByCustomer(string $customerId): array |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$cacheKey = sprintf('zoho_invoices_%s', $customerId); |
26
|
|
|
$hit = $this->getFromCache($cacheKey); |
27
|
|
|
|
28
|
|
|
if (false === $hit) { |
29
|
|
|
$response = $this->sendRequest('GET', sprintf('invoices?customer_id=%s', $customerId)); |
30
|
|
|
|
31
|
|
|
$result = $this->processResponse($response); |
32
|
|
|
|
33
|
|
|
$invoices = $result['invoices']; |
34
|
|
|
|
35
|
|
|
$this->saveToCache($cacheKey, $invoices); |
36
|
|
|
|
37
|
|
|
return $invoices; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $hit; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $invoiceId The invoice's id |
45
|
|
|
* |
46
|
|
|
* @throws \Exception |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function getInvoice(string $invoiceId) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$cacheKey = sprintf('zoho_invoice_%s', $invoiceId); |
53
|
|
|
$hit = $this->getFromCache($cacheKey); |
54
|
|
|
|
55
|
|
|
if (false === $hit) { |
56
|
|
|
$response = $this->sendRequest('GET', sprintf('invoices/%s', $invoiceId)); |
57
|
|
|
|
58
|
|
|
$result = $this->processResponse($response); |
59
|
|
|
|
60
|
|
|
$invoice = $result['invoice']; |
61
|
|
|
|
62
|
|
|
$this->saveToCache($cacheKey, $invoice); |
63
|
|
|
|
64
|
|
|
return $invoice; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $hit; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $invoiceId The invoice's id |
72
|
|
|
* |
73
|
|
|
* @throws \Exception |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getInvoicePdf(string $invoiceId) |
78
|
|
|
{ |
79
|
|
|
$response = $this->sendRequest('GET', sprintf('invoices/%s', $invoiceId), [ |
80
|
|
|
'query' => ['accept' => 'pdf'], |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.