Complex classes like EntityIconService 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 EntityIconService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class EntityIconService { |
||
25 | use TimeUsing; |
||
26 | |||
27 | /** |
||
28 | * @var Config |
||
29 | */ |
||
30 | private $config; |
||
31 | |||
32 | /** |
||
33 | * @var PluginHooksService |
||
34 | */ |
||
35 | private $hooks; |
||
36 | |||
37 | /** |
||
38 | * @var Request |
||
39 | */ |
||
40 | private $request; |
||
41 | |||
42 | /** |
||
43 | * @var Logger |
||
44 | */ |
||
45 | private $logger; |
||
46 | |||
47 | /** |
||
48 | * @var EntityTable |
||
49 | */ |
||
50 | private $entities; |
||
51 | |||
52 | /** |
||
53 | * @var UploadService |
||
54 | */ |
||
55 | private $uploads; |
||
56 | |||
57 | /** |
||
58 | * Constructor |
||
59 | * |
||
60 | * @param Config $config Config |
||
61 | * @param PluginHooksService $hooks Hook registration service |
||
62 | * @param Request $request Http request |
||
63 | * @param Logger $logger Logger |
||
64 | * @param EntityTable $entities Entity table |
||
65 | * @param UploadService $uploads Upload service |
||
66 | */ |
||
67 | 63 | public function __construct(Config $config, PluginHooksService $hooks, Request $request, Logger $logger, EntityTable $entities, UploadService $uploads) { |
|
75 | |||
76 | /** |
||
77 | * Saves icons using an uploaded file as the source. |
||
78 | * |
||
79 | * @param ElggEntity $entity Entity to own the icons |
||
80 | * @param string $input_name Form input name |
||
81 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
82 | * @param array $coords An array of cropping coordinates x1, y1, x2, y2 |
||
83 | * @return bool |
||
84 | */ |
||
85 | 1 | public function saveIconFromUploadedFile(ElggEntity $entity, $input_name, $type = 'icon', array $coords = []) { |
|
86 | 1 | $input = $this->uploads->getFile($input_name); |
|
87 | 1 | if (empty($input)) { |
|
88 | return false; |
||
89 | } |
||
90 | |||
91 | 1 | $tmp_filename = time() . $input->getClientOriginalName(); |
|
92 | 1 | $tmp = new ElggFile(); |
|
93 | 1 | $tmp->owner_guid = $entity->guid; |
|
94 | 1 | $tmp->setFilename("tmp/$tmp_filename"); |
|
95 | 1 | $tmp->open('write'); |
|
96 | 1 | $tmp->close(); |
|
97 | // not using move_uploaded_file() for testing purposes |
||
98 | 1 | copy($input->getPathname(), $tmp->getFilenameOnFilestore()); |
|
99 | |||
100 | 1 | $tmp->mimetype = (new MimeTypeDetector())->getType($tmp_filename, $input->getClientMimeType()); |
|
101 | 1 | $tmp->simpletype = elgg_get_file_simple_type($tmp->mimetype); |
|
102 | |||
103 | 1 | $result = $this->saveIcon($entity, $tmp, $type, $coords); |
|
104 | |||
105 | 1 | unlink($input->getPathname()); |
|
106 | 1 | $tmp->delete(); |
|
107 | |||
108 | 1 | return $result; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Saves icons using a local file as the source. |
||
113 | * |
||
114 | * @param ElggEntity $entity Entity to own the icons |
||
115 | * @param string $filename The full path to the local file |
||
116 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
117 | * @param array $coords An array of cropping coordinates x1, y1, x2, y2 |
||
118 | * @return bool |
||
119 | * @throws InvalidParameterException |
||
120 | */ |
||
121 | 5 | public function saveIconFromLocalFile(ElggEntity $entity, $filename, $type = 'icon', array $coords = []) { |
|
143 | |||
144 | /** |
||
145 | * Saves icons using a file located in the data store as the source. |
||
146 | * |
||
147 | * @param ElggEntity $entity Entity to own the icons |
||
148 | * @param ElggFile $file An ElggFile instance |
||
149 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
150 | * @param array $coords An array of cropping coordinates x1, y1, x2, y2 |
||
151 | * @return bool |
||
152 | * @throws InvalidParameterException |
||
153 | */ |
||
154 | 40 | public function saveIconFromElggFile(ElggEntity $entity, ElggFile $file, $type = 'icon', array $coords = []) { |
|
176 | |||
177 | /** |
||
178 | * Saves icons using a created temporary file |
||
179 | * |
||
180 | * @param ElggEntity $entity Temporary ElggFile instance |
||
181 | * @param ElggFile $file Temporary ElggFile instance |
||
182 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
183 | * @param array $coords An array of cropping coordinates x1, y1, x2, y2 |
||
184 | * @return bool |
||
185 | */ |
||
186 | 44 | public function saveIcon(ElggEntity $entity, ElggFile $file, $type = 'icon', array $coords = []) { |
|
264 | |||
265 | /** |
||
266 | * Generate an icon for the given entity |
||
267 | * |
||
268 | * @param ElggEntity $entity Temporary ElggFile instance |
||
269 | * @param ElggFile $file Temporary ElggFile instance |
||
270 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
271 | * @param array $coords An array of cropping coordinates x1, y1, x2, y2 |
||
272 | * @param string $icon_size The icon size to generate (leave empty to generate all supported sizes) |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 43 | protected function generateIcon(ElggEntity $entity, ElggFile $file, $type = 'icon', $coords = [], $icon_size = '') { |
|
350 | |||
351 | /** |
||
352 | * Returns entity icon as an ElggIcon object |
||
353 | * The icon file may or may not exist on filestore |
||
354 | * |
||
355 | * @note Returned ElggIcon object may be a placeholder. Use ElggIcon::exists() to validate if file has been written to filestore |
||
356 | * |
||
357 | * @param ElggEntity $entity Entity that owns the icon |
||
358 | * @param string $size Size of the icon |
||
359 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
360 | * @param bool $generate Try to generate an icon based on master if size doesn't exists |
||
361 | * |
||
362 | * @return ElggIcon |
||
363 | * |
||
364 | * @throws InvalidParameterException |
||
365 | */ |
||
366 | 54 | public function getIcon(ElggEntity $entity, $size, $type = 'icon', $generate = true) { |
|
418 | |||
419 | /** |
||
420 | * Removes all icon files and metadata for the passed type of icon. |
||
421 | * |
||
422 | * @param ElggEntity $entity Entity that owns icons |
||
423 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
424 | * @param bool $retain_master Keep the master icon (default: false) |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | 44 | public function deleteIcon(ElggEntity $entity, $type = 'icon', $retain_master = false) { |
|
455 | |||
456 | /** |
||
457 | * Get the URL for this entity's icon |
||
458 | * |
||
459 | * Plugins can register for the 'entity:icon:url', <type> plugin hook to customize the icon for an entity. |
||
460 | * |
||
461 | * @param ElggEntity $entity Entity that owns the icon |
||
462 | * @param mixed $params A string defining the size of the icon (e.g. tiny, small, medium, large) |
||
463 | * or an array of parameters including 'size' |
||
464 | * @return string|void |
||
465 | */ |
||
466 | 3 | public function getIconURL(ElggEntity $entity, $params = []) { |
|
496 | |||
497 | /** |
||
498 | * Returns default/fallback icon |
||
499 | * |
||
500 | * @param ElggEntity $entity Entity |
||
501 | * @param array $params Icon params |
||
502 | * @return string |
||
503 | */ |
||
504 | public function getFallbackIconUrl(ElggEntity $entity, array $params = []) { |
||
527 | |||
528 | /** |
||
529 | * Returns the timestamp of when the icon was changed. |
||
530 | * |
||
531 | * @param ElggEntity $entity Entity that owns the icon |
||
532 | * @param string $size The size of the icon |
||
533 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
534 | * |
||
535 | * @return int|null A unix timestamp of when the icon was last changed, or null if not set. |
||
536 | */ |
||
537 | 1 | public function getIconLastChange(ElggEntity $entity, $size, $type = 'icon') { |
|
543 | |||
544 | /** |
||
545 | * Returns if the entity has an icon of the passed type. |
||
546 | * |
||
547 | * @param ElggEntity $entity Entity that owns the icon |
||
548 | * @param string $size The size of the icon |
||
549 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
550 | * @return bool |
||
551 | */ |
||
552 | 14 | public function hasIcon(\ElggEntity $entity, $size, $type = 'icon') { |
|
555 | |||
556 | /** |
||
557 | * Returns a configuration array of icon sizes |
||
558 | * |
||
559 | * @param string $entity_type Entity type |
||
560 | * @param string $entity_subtype Entity subtype |
||
561 | * @param string $type The name of the icon. e.g., 'icon', 'cover_photo' |
||
562 | * @return array |
||
563 | * @throws InvalidParameterException |
||
564 | */ |
||
565 | 51 | public function getSizes($entity_type = null, $entity_subtype = null, $type = 'icon') { |
|
598 | |||
599 | /** |
||
600 | * Handle request to /serve-icon handler |
||
601 | * |
||
602 | * @param bool $allow_removing_headers Alter PHP's global headers to allow caching |
||
603 | * @return BinaryFileResponse |
||
604 | */ |
||
605 | 3 | public function handleServeIconRequest($allow_removing_headers = true) { |
|
667 | |||
668 | } |
||
669 |