laudeco /
dolibarr-api-client
| 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 | protected $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; |
||
|
0 ignored issues
–
show
|
|||
| 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 string $json |
||
| 95 | * @param array $options |
||
| 96 | * @param string $slug |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | * |
||
| 100 | * @throws ApiException |
||
| 101 | */ |
||
| 102 | protected function post($json, array $options = [], $slug = '') |
||
| 103 | { |
||
| 104 | try { |
||
| 105 | $response = $this->httpClient |
||
| 106 | ->post($this->resource->getResourceName().(!empty($slug) ? '/'.$slug : ''), $json, $options); |
||
| 107 | |||
| 108 | return $response->getBody()->getContents(); |
||
| 109 | } catch (RequestException $exception) { |
||
| 110 | throw new ApiException($exception->getMessage(), $exception->getCode(), $exception); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Send a DELETE request with JSON-encoded parameters. |
||
| 116 | * |
||
| 117 | * @param int $id |
||
| 118 | * @param array $options |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | * |
||
| 122 | * @throws ApiException |
||
| 123 | */ |
||
| 124 | protected function delete($id, array $options = []) |
||
| 125 | { |
||
| 126 | try { |
||
| 127 | $response = $this->httpClient |
||
| 128 | ->delete($this->resource->getResourceName().'/'.$id, $options); |
||
| 129 | |||
| 130 | return $response->getBody() |
||
| 131 | ->getContents(); |
||
| 132 | } catch (RequestException $exception) { |
||
| 133 | throw new ApiException(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $json |
||
| 139 | * @param string $type |
||
| 140 | * @param DeserializationContext $context |
||
| 141 | * |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | protected function deserialize($json, $type, DeserializationContext $context = null) |
||
| 145 | { |
||
| 146 | return $this->serializer->deserialize($json, $type, 'json', $context); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $data |
||
| 151 | * @param string $type |
||
| 152 | * |
||
| 153 | * @return ArrayCollection |
||
| 154 | */ |
||
| 155 | protected function deserializeCollection($data, $type) |
||
| 156 | { |
||
| 157 | $resources = $this->deserialize($data, "array<".$type.">"); |
||
| 158 | |||
| 159 | return new ArrayCollection($resources); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param mixed $entity |
||
| 164 | * @param SerializationContext $context |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function serialize($entity, SerializationContext $context = null) |
||
| 169 | { |
||
| 170 | return $this->serializer->serialize($entity, 'json', $context); |
||
| 171 | } |
||
| 172 | } |
||
| 173 |
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.