| Total Complexity | 48 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CategoryProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CategoryProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CategoryProvider |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var HttpClient |
||
| 23 | */ |
||
| 24 | protected $httpClient; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var CacheInterface |
||
| 28 | */ |
||
| 29 | protected $cache; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $cachePrefix = 'category'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * @param HttpClient $httpClient |
||
| 40 | * @param CacheInterface|null $cache |
||
| 41 | */ |
||
| 42 | public function __construct(HttpClient $httpClient, CacheInterface $cache = null) |
||
| 43 | { |
||
| 44 | $this->httpClient = $httpClient; |
||
| 45 | |||
| 46 | $this->cache = $cache; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Return a category based on ID |
||
| 51 | * |
||
| 52 | * @param int $categoryId ID of the category |
||
| 53 | * |
||
| 54 | * @throws EntityNotFoundException if the API call fails |
||
| 55 | * |
||
| 56 | * @return Category |
||
| 57 | */ |
||
| 58 | public function getItem($categoryId) |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns an array of categories |
||
| 92 | * |
||
| 93 | * @param int $limit |
||
| 94 | * @param int $offset |
||
| 95 | * |
||
| 96 | * @throws ApiException if the API call fails |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function getItems($limit = 1000, $offset = 0) |
||
| 101 | { |
||
| 102 | // Endpoint URI |
||
| 103 | $uri = 'v1/categories'; |
||
| 104 | |||
| 105 | $options = [ |
||
| 106 | 'query' => [ |
||
| 107 | 'limit' => $limit, |
||
| 108 | 'offset' => $offset, |
||
| 109 | ], |
||
| 110 | ]; |
||
| 111 | |||
| 112 | $categories = []; |
||
| 113 | |||
| 114 | try { |
||
| 115 | $data = null; |
||
| 116 | |||
| 117 | // try to get from cache |
||
| 118 | if ($this->cache) { |
||
| 119 | $data = $this->cache->get($this->cachePrefix, $uri, $options); |
||
| 120 | } |
||
| 121 | |||
| 122 | // load from API |
||
| 123 | if (!$data) { |
||
| 124 | $data = $this->httpClient->get($uri, $options)->getBody()->getContents(); |
||
| 125 | |||
| 126 | if ($this->cache and $data) { |
||
| 127 | $this->cache->put($this->cachePrefix, $uri, $options, $data); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $classArray = json_decode($data); |
||
| 132 | |||
| 133 | foreach ($classArray as $class) { |
||
| 134 | $categories[] = CategoryHydrator::fromStdClass($class); |
||
| 135 | } |
||
| 136 | } catch (ClientException $e) { |
||
| 137 | $response = $e->getResponse(); |
||
| 138 | $responseBody = $response->getBody()->getContents(); |
||
| 139 | $responseCode = $response->getStatusCode(); |
||
| 140 | |||
| 141 | throw ApiException::translate($responseBody, $responseCode); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $categories; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns an array of categories for a Data Provider |
||
| 149 | * |
||
| 150 | * @param int $dataProviderId |
||
| 151 | * @param int $limit |
||
| 152 | * @param int $offset |
||
| 153 | * |
||
| 154 | * @throws ApiException if the API call fails |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function getItemsDataProvider($dataProviderId, $limit = 1000, $offset = 0) |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns an array of categories for a Data Consumer |
||
| 207 | * |
||
| 208 | * @param int $dataConsumerId |
||
| 209 | * @param int $limit |
||
| 210 | * @param int $offset |
||
| 211 | * |
||
| 212 | * @throws ApiException if the API call fails |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getItemsDataConsumer($dataConsumerId, $limit = 1000, $offset = 0) |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Create a category |
||
| 265 | * |
||
| 266 | * @param Category $category |
||
| 267 | * |
||
| 268 | * @throws EntityInvalidException if the API returns a validation error |
||
| 269 | * @throws ApiException if the API call fails |
||
| 270 | * |
||
| 271 | * @return Category |
||
| 272 | */ |
||
| 273 | public function create(Category $category) |
||
| 274 | { |
||
| 275 | // Endpoint URI |
||
| 276 | $uri = 'v1/categories'; |
||
| 277 | |||
| 278 | $options = [ |
||
| 279 | 'json' => $category, |
||
| 280 | ]; |
||
| 281 | |||
| 282 | try { |
||
| 283 | $data = $this->httpClient->post($uri, $options)->getBody()->getContents(); |
||
| 284 | |||
| 285 | $category = CategoryHydrator::fromStdClass(json_decode($data)); |
||
| 286 | |||
| 287 | if ($this->cache and $data) { |
||
| 288 | $this->cache->invalidate($this->cachePrefix); |
||
| 289 | $this->cache->put($this->cachePrefix, $uri.'/'.$category->getId(), [], $data); |
||
| 290 | } |
||
| 291 | |||
| 292 | return $category; |
||
| 293 | |||
| 294 | } catch (ClientException $e) { |
||
| 295 | $this->manageClientException($e); |
||
| 296 | } |
||
| 297 | |||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Update a category |
||
| 302 | * |
||
| 303 | * @param Category $category |
||
| 304 | * |
||
| 305 | * @throws EntityInvalidException if the API returns a validation error |
||
| 306 | * @throws ApiException if the API call fails |
||
| 307 | * |
||
| 308 | * @return Category |
||
| 309 | */ |
||
| 310 | public function update(Category $category) |
||
| 311 | { |
||
| 312 | // Endpoint URI |
||
| 313 | $uri = sprintf('v1/categories/%d', $category->getId()); |
||
| 314 | |||
| 315 | $options = [ |
||
| 316 | 'json' => $category, |
||
| 317 | ]; |
||
| 318 | |||
| 319 | try { |
||
| 320 | $data = $this->httpClient->put($uri, $options)->getBody()->getContents(); |
||
| 321 | |||
| 322 | $category = CategoryHydrator::fromStdClass(json_decode($data)); |
||
| 323 | |||
| 324 | if ($this->cache and $data) { |
||
| 325 | $this->cache->invalidate($this->cachePrefix); |
||
| 326 | $this->cache->put($this->cachePrefix, $uri.'/'.$category->getId(), [], $data); |
||
| 327 | } |
||
| 328 | |||
| 329 | return $category; |
||
| 330 | } catch (ClientException $e) { |
||
| 331 | $this->manageClientException($e); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Delete a category |
||
| 337 | * |
||
| 338 | * @param Category $category |
||
| 339 | * |
||
| 340 | * @throws ApiException if the API call fails |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function delete(Category $category) |
||
| 345 | { |
||
| 346 | // Endpoint URI |
||
| 347 | $uri = sprintf('v1/categories/%d', $category->getId()); |
||
| 348 | |||
| 349 | try { |
||
| 350 | $this->httpClient->delete($uri); |
||
| 351 | |||
| 352 | if ($this->cache) { |
||
| 353 | $this->cache->invalidate($this->cachePrefix); |
||
| 354 | } |
||
| 355 | } catch (ClientException $e) { |
||
| 356 | $response = $e->getResponse(); |
||
| 357 | $responseBody = $response->getBody()->getContents(); |
||
| 358 | $responseCode = $response->getStatusCode(); |
||
| 359 | |||
| 360 | throw ApiException::translate($responseBody, $responseCode); |
||
| 361 | } |
||
| 362 | |||
| 363 | return true; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param ClientException $exception |
||
| 368 | * |
||
| 369 | * @throws EntityInvalidException |
||
| 370 | * @throws ApiException |
||
| 371 | */ |
||
| 372 | protected function manageClientException(ClientException $exception) |
||
| 413 | } |
||
| 414 | } |
||
| 415 |