Total Complexity | 40 |
Total Lines | 363 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SegmentManager 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 SegmentManager, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
14 | class SegmentManager |
||
15 | { |
||
16 | /** @var HttpClient */ |
||
17 | protected $httpClient; |
||
18 | |||
19 | /** @var CacheInterface */ |
||
20 | protected $cache; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $cachePrefix = 'segment'; |
||
24 | |||
25 | public function __construct(HttpClient $httpClient, ?CacheInterface $cache = null) |
||
26 | { |
||
27 | $this->httpClient = $httpClient; |
||
28 | $this->cache = $cache; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Return a category based on ID |
||
33 | * |
||
34 | * @param int $segmentId ID of the category |
||
35 | * |
||
36 | * @throws EntityNotFoundException if the API call fails |
||
37 | * |
||
38 | * @return Segment |
||
39 | */ |
||
40 | public function getItem($segmentId): Segment |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Returns an array of segments |
||
77 | * |
||
78 | * @param int $limit |
||
79 | * @param int $offset |
||
80 | * |
||
81 | * @throws ApiException if the API call fails |
||
82 | * |
||
83 | * @return Segment[] |
||
84 | */ |
||
85 | public function getItems(int $limit = 1000, int $offset = 0): array |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Returns an array of segments for a Data Provider |
||
102 | * |
||
103 | * @param int $dataProviderId |
||
104 | * @param int $limit |
||
105 | * @param int $offset |
||
106 | |||
107 | * @throws ApiException if the API call fails |
||
108 | * |
||
109 | * @return Segment[] |
||
110 | */ |
||
111 | public function getItemsDataProvider(int $dataProviderId, int $limit = 1000, int $offset = 0): array |
||
112 | { |
||
113 | // Endpoint URI |
||
114 | $uri = sprintf('v1/dataproviders/%d/segments', $dataProviderId); |
||
115 | |||
116 | $options = [ |
||
117 | 'query' => [ |
||
118 | 'limit' => $limit, |
||
119 | 'offset' => $offset, |
||
120 | ], |
||
121 | ]; |
||
122 | |||
123 | return $this->getSegmentsFromListEndpoint($uri, $options); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns an array of segments for a Category |
||
128 | * |
||
129 | * @param int $categoryId |
||
130 | * @param int $limit |
||
131 | * @param int $offset |
||
132 | * |
||
133 | * @throws ApiException if the API call fails |
||
134 | * |
||
135 | * @return Segment[] |
||
136 | */ |
||
137 | public function getItemsCategory(int $categoryId, int $limit = 1000, int $offset = 0): array |
||
138 | { |
||
139 | // Endpoint URI |
||
140 | $uri = sprintf('v1/categories/%d/segments', $categoryId); |
||
141 | |||
142 | $options = [ |
||
143 | 'query' => [ |
||
144 | 'limit' => $limit, |
||
145 | 'offset' => $offset, |
||
146 | ], |
||
147 | ]; |
||
148 | |||
149 | return $this->getSegmentsFromListEndpoint($uri, $options); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns an array of segments for a Data Consumer |
||
154 | * |
||
155 | * @param int $dataConsumerId |
||
156 | * @param int $limit |
||
157 | * @param int $offset |
||
158 | * |
||
159 | * @throws ApiException if the API call fails |
||
160 | * |
||
161 | * @return Segment[] |
||
162 | */ |
||
163 | public function getItemsDataConsumer(int $dataConsumerId, int $limit = 1000, int $offset = 0): array |
||
164 | { |
||
165 | // Endpoint URI |
||
166 | $uri = sprintf('v1/dataconsumers/%d/segments', $dataConsumerId); |
||
167 | |||
168 | $options = [ |
||
169 | 'query' => [ |
||
170 | 'limit' => $limit, |
||
171 | 'offset' => $offset, |
||
172 | ], |
||
173 | ]; |
||
174 | |||
175 | return $this->getSegmentsFromListEndpoint($uri, $options); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $uri |
||
180 | * @param array $options |
||
181 | * |
||
182 | * @return array |
||
183 | * @throws ApiException |
||
184 | */ |
||
185 | private function getSegmentsFromListEndpoint(string $uri, array $options) |
||
186 | { |
||
187 | $segments = []; |
||
188 | |||
189 | try { |
||
190 | $data = null; |
||
191 | |||
192 | // try to get from cache |
||
193 | if ($this->cache) { |
||
194 | $data = $this->cache->get($this->cachePrefix, $uri, $options); |
||
195 | } |
||
196 | |||
197 | // load from API |
||
198 | if (!$data) { |
||
199 | $data = $this->httpClient->get($uri, $options)->getBody()->getContents(); |
||
200 | |||
201 | if ($this->cache && $data) { |
||
202 | $this->cache->put($this->cachePrefix, $uri, $options, $data); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | $classArray = \json_decode($data); |
||
207 | |||
208 | foreach ($classArray as $class) { |
||
209 | $segments[] = SegmentHydrator::fromStdClass($class); |
||
210 | } |
||
211 | } catch (ClientException $e) { |
||
212 | $response = $e->getResponse(); |
||
213 | if ($response === null) { |
||
214 | throw $e; |
||
215 | } |
||
216 | $responseBody = $response->getBody()->getContents(); |
||
217 | $responseCode = $response->getStatusCode(); |
||
218 | |||
219 | throw ApiException::translate($responseBody, $responseCode); |
||
220 | } |
||
221 | |||
222 | return $segments; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Create a category |
||
227 | * |
||
228 | * @param Segment $segment |
||
229 | * |
||
230 | * @throws EntityInvalidException if the API returns a validation error |
||
231 | * @throws ApiException if the API call fails |
||
232 | * |
||
233 | * @return Segment |
||
234 | */ |
||
235 | public function create(Segment $segment): Segment |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Update a category |
||
262 | * |
||
263 | * @param Segment $segment |
||
264 | * |
||
265 | * @throws EntityInvalidException if the API returns a validation error |
||
266 | * @throws ApiException if the API call fails |
||
267 | * |
||
268 | * @return Segment |
||
269 | */ |
||
270 | public function update(Segment $segment): Segment |
||
271 | { |
||
272 | // Endpoint URI |
||
273 | $uri = sprintf('v1/segments/%d', $segment->getId()); |
||
274 | |||
275 | $options = [ |
||
276 | 'json' => $segment, |
||
277 | ]; |
||
278 | |||
279 | try { |
||
280 | $data = $this->httpClient->put($uri, $options)->getBody()->getContents(); |
||
281 | |||
282 | $segment = SegmentHydrator::fromStdClass(json_decode($data)); |
||
283 | |||
284 | if ($this->cache && $data) { |
||
285 | $this->cache->invalidate($this->cachePrefix); |
||
286 | $this->cache->put($this->cachePrefix, $uri.'/'.$segment->getId(), [], $data); |
||
287 | } |
||
288 | } catch (ClientException $e) { |
||
289 | $this->manageClientException($e); |
||
290 | } |
||
291 | |||
292 | return $segment; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Delete a category |
||
297 | * |
||
298 | * @param Segment $segment |
||
299 | * |
||
300 | * @throws ApiException if the API call fails |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function delete(Segment $segment): bool |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @param ClientException $exception |
||
332 | * |
||
333 | * @throws EntityInvalidException |
||
334 | * @throws ApiException |
||
335 | */ |
||
336 | protected function manageClientException(ClientException $exception): void |
||
377 | } |
||
378 | } |
||
379 |