1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dolibarr\Client\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Dolibarr\Client\Domain\Resource\ApiResource; |
7
|
|
|
use Dolibarr\Client\Domain\Resource\ResourceId; |
8
|
|
|
use Dolibarr\Client\Exception\ApiException; |
9
|
|
|
use Dolibarr\Client\HttpClient\HttpClient; |
10
|
|
|
use Dolibarr\Client\HttpClient\HttpClientInterface; |
11
|
|
|
use GuzzleHttp\Exception\RequestException; |
12
|
|
|
use function GuzzleHttp\json_decode; |
13
|
|
|
use JMS\Serializer\DeserializationContext; |
14
|
|
|
use JMS\Serializer\SerializationContext; |
15
|
|
|
use JMS\Serializer\SerializerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Laurent De Coninck <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractService |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var HttpClient |
25
|
|
|
*/ |
26
|
|
|
protected $httpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SerializerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $serializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ApiResource |
35
|
|
|
*/ |
36
|
|
|
private $resource; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param HttpClientInterface $httpClient |
40
|
|
|
* @param SerializerInterface $serializerInterface |
41
|
|
|
* @param ApiResource $resource |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
HttpClientInterface $httpClient, |
45
|
|
|
SerializerInterface $serializerInterface, |
46
|
|
|
ApiResource $resource |
47
|
|
|
) { |
48
|
|
|
$this->httpClient = $httpClient; |
|
|
|
|
49
|
|
|
$this->serializer = $serializerInterface; |
50
|
|
|
$this->resource = $resource; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int|null $id |
55
|
|
|
* |
56
|
|
|
* @return bool|string |
57
|
|
|
* |
58
|
|
|
* @throws ApiException |
59
|
|
|
*/ |
60
|
|
|
protected function get($id = null) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
return $this->httpClient |
64
|
|
|
->get($this->resource->getResourceName().'/'.$id) |
65
|
|
|
->getBody() |
66
|
|
|
->getContents(); |
67
|
|
|
} catch (RequestException $exception) { |
68
|
|
|
throw new ApiException($exception->getMessage(), $exception->getCode(), $exception); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $options |
74
|
|
|
* |
75
|
|
|
* @return bool|string |
76
|
|
|
* |
77
|
|
|
* @throws ApiException |
78
|
|
|
*/ |
79
|
|
|
protected function getList(array $options = []) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
return $this->httpClient |
83
|
|
|
->get($this->resource->getResourceName(), $options) |
84
|
|
|
->getBody() |
85
|
|
|
->getContents(); |
86
|
|
|
} catch (RequestException $exception) { |
87
|
|
|
throw new ApiException($exception->getMessage(), $exception->getCode(), $exception); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Send a POST request with JSON-encoded parameters. |
93
|
|
|
* |
94
|
|
|
* @param array $json |
95
|
|
|
* @param array $options |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
* |
99
|
|
|
* @throws ApiException |
100
|
|
|
*/ |
101
|
|
|
protected function post($json, array $options = []) |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
|
|
$response = $this->httpClient |
105
|
|
|
->post($this->resource->getResourceName(), $json, $options); |
106
|
|
|
|
107
|
|
|
return $response->getBody()->getContents(); |
108
|
|
|
} catch (RequestException $exception) { |
109
|
|
|
throw new ApiException($exception->getMessage(), $exception->getCode(), $exception); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
115
|
|
|
* |
116
|
|
|
* @param int $id |
117
|
|
|
* @param array $options |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* |
121
|
|
|
* @throws ApiException |
122
|
|
|
*/ |
123
|
|
|
protected function delete($id, array $options = []) |
124
|
|
|
{ |
125
|
|
|
try { |
126
|
|
|
$response = $this->httpClient |
127
|
|
|
->delete($this->resource->getResourceName().'/'.$id, $options); |
128
|
|
|
|
129
|
|
|
return $response->getBody() |
130
|
|
|
->getContents(); |
131
|
|
|
} catch (RequestException $exception) { |
132
|
|
|
throw new ApiException(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $json |
138
|
|
|
* @param string $type |
139
|
|
|
* @param DeserializationContext $context |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
protected function deserialize($json, $type, DeserializationContext $context = null) |
144
|
|
|
{ |
145
|
|
|
return $this->serializer->deserialize($json, $type, 'json', $context); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $data |
150
|
|
|
* @param string $type |
151
|
|
|
* |
152
|
|
|
* @return ArrayCollection |
153
|
|
|
*/ |
154
|
|
|
protected function deserializeCollection($data, $type) |
155
|
|
|
{ |
156
|
|
|
$resources = $this->deserialize($data, "array<".$type.">"); |
157
|
|
|
|
158
|
|
|
return new ArrayCollection($resources); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $entity |
163
|
|
|
* @param SerializationContext $context |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function serialize($entity, SerializationContext $context = null) |
168
|
|
|
{ |
169
|
|
|
return $this->serializer->serialize($entity, 'json', $context); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.