1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FAPI\Fortnox\Api; |
6
|
|
|
|
7
|
|
|
use FAPI\Fortnox\Model\ApiResponse; |
8
|
|
|
use FAPI\Fortnox\Model\Customer\CustomerCollection; |
9
|
|
|
use FAPI\Fortnox\Model\Invoice\Invoice as Model; |
10
|
|
|
use FAPI\Fortnox\Model\Invoice\InvoiceCollection; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@link https://developer.fortnox.se/documentation/resources/invoices/}. |
15
|
|
|
* |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
View Code Duplication |
class Invoice extends HttpApi |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @throws \FAPI\Fortnox\Exception\DomainException |
22
|
|
|
* |
23
|
|
|
* @return CustomerCollection|ResponseInterface |
24
|
|
|
*/ |
25
|
|
|
public function all(array $params = []) |
26
|
|
|
{ |
27
|
|
|
$response = $this->httpGet('/3/invoices', $params); |
28
|
|
|
|
29
|
|
|
if (!$this->hydrator) { |
30
|
|
|
return $response; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Use any valid status code here |
34
|
|
|
if (200 !== $response->getStatusCode()) { |
35
|
|
|
$this->handleErrors($response); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->hydrator->hydrate($response, InvoiceCollection::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws \FAPI\Fortnox\Exception\DomainException |
43
|
|
|
* |
44
|
|
|
* @return Model|ResponseInterface |
45
|
|
|
*/ |
46
|
|
|
public function get(int $invoice) |
47
|
|
|
{ |
48
|
|
|
$response = $this->httpGet('/3/invoices/'.$invoice); |
49
|
|
|
|
50
|
|
|
if (!$this->hydrator) { |
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Use any valid status code here |
55
|
|
|
if (200 !== $response->getStatusCode()) { |
56
|
|
|
$this->handleErrors($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws \FAPI\Fortnox\Exception\DomainException |
64
|
|
|
* |
65
|
|
|
* @return Model|ResponseInterface |
66
|
|
|
*/ |
67
|
|
|
public function create(array $data) |
68
|
|
|
{ |
69
|
|
|
$response = $this->httpPost('/3/invoices', ['Invoice' => $data]); |
70
|
|
|
|
71
|
|
|
if (!$this->hydrator) { |
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Use any valid status code here |
76
|
|
|
if (201 !== $response->getStatusCode()) { |
77
|
|
|
$this->handleErrors($response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws \FAPI\Fortnox\Exception\DomainException |
85
|
|
|
* |
86
|
|
|
* @return Model|ResponseInterface |
87
|
|
|
*/ |
88
|
|
|
public function update(int $invoice, array $data) |
89
|
|
|
{ |
90
|
|
|
$response = $this->httpPut('/3/invoices/'.$invoice, ['Invoice' => $data]); |
91
|
|
|
|
92
|
|
|
if (!$this->hydrator) { |
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Use any valid status code here |
97
|
|
|
if (200 !== $response->getStatusCode()) { |
98
|
|
|
$this->handleErrors($response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws \FAPI\Fortnox\Exception\DomainException |
106
|
|
|
* |
107
|
|
|
* @return ApiResponse|ResponseInterface |
108
|
|
|
*/ |
109
|
|
|
public function email(int $invoice) |
110
|
|
|
{ |
111
|
|
|
$response = $this->httpGet(sprintf('/3/invoices/%d/email', $invoice)); |
112
|
|
|
|
113
|
|
|
if (!$this->hydrator) { |
114
|
|
|
return $response; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Use any valid status code here |
118
|
|
|
if (200 !== $response->getStatusCode()) { |
119
|
|
|
$this->handleErrors($response); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->hydrator->hydrate($response, ApiResponse::class); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.