Complex classes like Server 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Server, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Server |
||
14 | { |
||
15 | /** |
||
16 | * Source file system. |
||
17 | * @var FilesystemInterface |
||
18 | */ |
||
19 | protected $source; |
||
20 | |||
21 | /** |
||
22 | * Source path prefix. |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $sourcePathPrefix; |
||
26 | |||
27 | /** |
||
28 | * Cache file system. |
||
29 | * @var FilesystemInterface |
||
30 | */ |
||
31 | protected $cache; |
||
32 | |||
33 | /** |
||
34 | * Cache path prefix. |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $cachePathPrefix; |
||
38 | |||
39 | /** |
||
40 | * Whether to group cache in folders. |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $groupCacheInFolders = true; |
||
44 | |||
45 | /** |
||
46 | * Image manipulation API. |
||
47 | * @var ApiInterface |
||
48 | */ |
||
49 | protected $api; |
||
50 | |||
51 | /** |
||
52 | * Response factory. |
||
53 | * @var ResponseFactoryInterface|null |
||
54 | */ |
||
55 | protected $responseFactory; |
||
56 | |||
57 | /** |
||
58 | * Base URL. |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $baseUrl; |
||
62 | |||
63 | /** |
||
64 | * Default image manipulations. |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $defaults = []; |
||
68 | |||
69 | /** |
||
70 | * Preset image manipulations. |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $presets = []; |
||
74 | |||
75 | /** |
||
76 | * Create Server instance. |
||
77 | * @param FilesystemInterface $source Source file system. |
||
78 | * @param FilesystemInterface $cache Cache file system. |
||
79 | * @param ApiInterface $api Image manipulation API. |
||
80 | */ |
||
81 | 144 | public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api) |
|
82 | { |
||
83 | 144 | $this->setSource($source); |
|
84 | 144 | $this->setCache($cache); |
|
85 | 144 | $this->setApi($api); |
|
86 | 144 | } |
|
87 | |||
88 | /** |
||
89 | * Set source file system. |
||
90 | * @param FilesystemInterface $source Source file system. |
||
91 | */ |
||
92 | 144 | public function setSource(FilesystemInterface $source) |
|
93 | { |
||
94 | 144 | $this->source = $source; |
|
95 | 144 | } |
|
96 | |||
97 | /** |
||
98 | * Get source file system. |
||
99 | * @return FilesystemInterface Source file system. |
||
100 | */ |
||
101 | 6 | public function getSource() |
|
105 | |||
106 | /** |
||
107 | * Set source path prefix. |
||
108 | * @param string $sourcePathPrefix Source path prefix. |
||
109 | */ |
||
110 | 15 | public function setSourcePathPrefix($sourcePathPrefix) |
|
114 | |||
115 | /** |
||
116 | * Get source path prefix. |
||
117 | * @return string Source path prefix. |
||
118 | */ |
||
119 | 6 | public function getSourcePathPrefix() |
|
123 | |||
124 | /** |
||
125 | * Get source path. |
||
126 | * @param string $path Image path. |
||
127 | * @return string The source path. |
||
128 | * @throws FileNotFoundException |
||
129 | */ |
||
130 | 66 | public function getSourcePath($path) |
|
148 | |||
149 | /** |
||
150 | * Check if a source file exists. |
||
151 | * @param string $path Image path. |
||
152 | * @return bool Whether the source file exists. |
||
153 | */ |
||
154 | 18 | public function sourceFileExists($path) |
|
158 | |||
159 | /** |
||
160 | * Set base URL. |
||
161 | * @param string $baseUrl Base URL. |
||
162 | */ |
||
163 | 12 | public function setBaseUrl($baseUrl) |
|
167 | |||
168 | /** |
||
169 | * Get base URL. |
||
170 | * @return string Base URL. |
||
171 | */ |
||
172 | 6 | public function getBaseUrl() |
|
176 | |||
177 | /** |
||
178 | * Set cache file system. |
||
179 | * @param FilesystemInterface $cache Cache file system. |
||
180 | */ |
||
181 | 144 | public function setCache(FilesystemInterface $cache) |
|
185 | |||
186 | /** |
||
187 | * Get cache file system. |
||
188 | * @return FilesystemInterface Cache file system. |
||
189 | */ |
||
190 | 6 | public function getCache() |
|
194 | |||
195 | /** |
||
196 | * Set cache path prefix. |
||
197 | * @param string $cachePathPrefix Cache path prefix. |
||
198 | */ |
||
199 | 12 | public function setCachePathPrefix($cachePathPrefix) |
|
203 | |||
204 | /** |
||
205 | * Get cache path prefix. |
||
206 | * @return string Cache path prefix. |
||
207 | */ |
||
208 | 6 | public function getCachePathPrefix() |
|
212 | |||
213 | /** |
||
214 | * Set the group cache in folders setting. |
||
215 | * @param bool $groupCacheInFolders Whether to group cache in folders. |
||
216 | */ |
||
217 | 15 | public function setGroupCacheInFolders($groupCacheInFolders) |
|
221 | |||
222 | /** |
||
223 | * Get the group cache in folders setting. |
||
224 | * @return bool Whether to group cache in folders. |
||
225 | */ |
||
226 | 6 | public function getGroupCacheInFolders() |
|
230 | |||
231 | /** |
||
232 | * Get cache path. |
||
233 | * @param string $path Image path. |
||
234 | * @param array $params Image manipulation params. |
||
235 | * @return string Cache path. |
||
236 | */ |
||
237 | 48 | public function getCachePath($path, array $params = []) |
|
238 | { |
||
239 | 48 | $sourcePath = $this->getSourcePath($path); |
|
240 | |||
241 | 48 | if ($this->sourcePathPrefix) { |
|
242 | 3 | $sourcePath = substr($sourcePath, strlen($this->sourcePathPrefix) + 1); |
|
243 | 3 | } |
|
244 | |||
245 | 48 | $params = $this->getAllParams($params); |
|
246 | 48 | unset($params['s'], $params['p']); |
|
247 | 48 | ksort($params); |
|
248 | |||
249 | 48 | $md5 = md5($sourcePath.'?'.http_build_query($params)); |
|
250 | |||
251 | 48 | $path = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; |
|
252 | |||
253 | 48 | if ($this->cachePathPrefix) { |
|
254 | 3 | $path = $this->cachePathPrefix.'/'.$path; |
|
255 | 3 | } |
|
256 | |||
257 | 48 | return $path; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Check if a cache file exists. |
||
262 | * @param string $path Image path. |
||
263 | * @param array $params Image manipulation params. |
||
264 | * @return bool Whether the cache file exists. |
||
265 | */ |
||
266 | 33 | public function cacheFileExists($path, array $params) |
|
267 | { |
||
268 | 33 | return $this->cache->has( |
|
269 | 33 | $this->getCachePath($path, $params) |
|
270 | 33 | ); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Delete cached manipulations for an image. |
||
275 | * @param string $path Image path. |
||
276 | * @return bool Whether the delete succeeded. |
||
277 | */ |
||
278 | 6 | public function deleteCache($path) |
|
279 | { |
||
280 | 6 | if (!$this->groupCacheInFolders) { |
|
281 | 3 | throw new InvalidArgumentException( |
|
282 | 'Deleting cached image manipulations is not possible when grouping cache into folders is disabled.' |
||
283 | 3 | ); |
|
284 | } |
||
285 | |||
286 | 3 | return $this->cache->deleteDir( |
|
287 | 3 | dirname($this->getCachePath($path)) |
|
288 | 3 | ); |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Set image manipulation API. |
||
293 | * @param ApiInterface $api Image manipulation API. |
||
294 | */ |
||
295 | 144 | public function setApi(ApiInterface $api) |
|
299 | |||
300 | /** |
||
301 | * Get image manipulation API. |
||
302 | * @return ApiInterface Image manipulation API. |
||
303 | */ |
||
304 | 6 | public function getApi() |
|
308 | |||
309 | /** |
||
310 | * Set default image manipulations. |
||
311 | * @param array $defaults Default image manipulations. |
||
312 | */ |
||
313 | 15 | public function setDefaults(array $defaults) |
|
317 | |||
318 | /** |
||
319 | * Get default image manipulations. |
||
320 | * @return array Default image manipulations. |
||
321 | */ |
||
322 | 6 | public function getDefaults() |
|
326 | |||
327 | /** |
||
328 | * Set preset image manipulations. |
||
329 | * @param array $presets Preset image manipulations. |
||
330 | */ |
||
331 | 15 | public function setPresets(array $presets) |
|
335 | |||
336 | /** |
||
337 | * Get preset image manipulations. |
||
338 | * @return array Preset image manipulations. |
||
339 | */ |
||
340 | 6 | public function getPresets() |
|
344 | |||
345 | /** |
||
346 | * Get all image manipulations params, including defaults and presets. |
||
347 | * @param array $params Image manipulation params. |
||
348 | * @return array All image manipulation params. |
||
349 | */ |
||
350 | 51 | public function getAllParams(array $params) |
|
364 | |||
365 | /** |
||
366 | * Set response factory. |
||
367 | * @param ResponseFactoryInterface|null $responseFactory Response factory. |
||
368 | */ |
||
369 | 15 | public function setResponseFactory(ResponseFactoryInterface $responseFactory = null) |
|
373 | |||
374 | /** |
||
375 | * Get response factory. |
||
376 | * @return ResponseFactoryInterface Response factory. |
||
377 | */ |
||
378 | 6 | public function getResponseFactory() |
|
382 | |||
383 | /** |
||
384 | * Generate and return image response. |
||
385 | * @param string $path Image path. |
||
386 | * @param array $params Image manipulation params. |
||
387 | * @return mixed Image response. |
||
388 | * @throws InvalidArgumentException |
||
389 | */ |
||
390 | 6 | public function getImageResponse($path, array $params) |
|
391 | { |
||
392 | 6 | if (is_null($this->responseFactory)) { |
|
393 | 3 | throw new InvalidArgumentException( |
|
394 | 'Unable to get image response, no response factory defined.' |
||
395 | 3 | ); |
|
396 | } |
||
397 | |||
398 | 3 | $path = $this->makeImage($path, $params); |
|
399 | |||
400 | 3 | return $this->responseFactory->create($this->cache, $path); |
|
401 | } |
||
402 | |||
403 | /** |
||
404 | * Generate and return Base64 encoded image. |
||
405 | * @param string $path Image path. |
||
406 | * @param array $params Image manipulation params. |
||
407 | * @return string Base64 encoded image. |
||
408 | * @throws FilesystemException |
||
409 | */ |
||
410 | 6 | public function getImageAsBase64($path, array $params) |
|
424 | |||
425 | /** |
||
426 | * Generate and output image. |
||
427 | * @param string $path Image path. |
||
428 | * @param array $params Image manipulation params. |
||
429 | * @throws InvalidArgumentException |
||
430 | */ |
||
431 | 3 | public function outputImage($path, array $params) |
|
448 | |||
449 | /** |
||
450 | * Generate manipulated image. |
||
451 | * @param string $path Image path. |
||
452 | * @param array $params Image manipulation params. |
||
453 | * @return string Cache path. |
||
454 | * @throws FileNotFoundException |
||
455 | * @throws FilesystemException |
||
456 | */ |
||
457 | 30 | public function makeImage($path, array $params) |
|
514 | } |
||
515 |