Complex classes like FileProvider 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 FileProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class FileProvider extends AbstractProvider |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $fileConstraintOptions = []; |
||
17 | |||
18 | /** |
||
19 | * @param array $fileConstraintOptions |
||
20 | */ |
||
21 | 16 | public function __construct(array $fileConstraintOptions = []) |
|
25 | |||
26 | /** |
||
27 | * @param FormMapper $formMapper |
||
28 | */ |
||
29 | 4 | public function buildProviderCreateForm(FormMapper $formMapper) |
|
30 | { |
||
31 | 4 | $this->addRequiredFileField($formMapper, 'binaryContent', 'file'); |
|
32 | 4 | } |
|
33 | |||
34 | /** |
||
35 | * @param FormMapper $formMapper |
||
36 | */ |
||
37 | 2 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
|
38 | { |
||
39 | 2 | $this->addFileField($formMapper, 'binaryContent', 'file'); |
|
40 | 2 | } |
|
41 | |||
42 | /** |
||
43 | * @param AbstractMedia $media |
||
44 | * @param bool $providerReferenceUpdated |
||
45 | */ |
||
46 | 2 | public function update(AbstractMedia $media, $providerReferenceUpdated) |
|
47 | { |
||
48 | 2 | if (!is_null($media->getBinaryContent())) { |
|
49 | 2 | if (empty($media->getImage())) { |
|
50 | 2 | $this->setFileImage($media); |
|
51 | } |
||
52 | 2 | $filename = $this->handleFileUpload($media); |
|
53 | 2 | if (!empty($filename)) { |
|
54 | 2 | $media->setProviderReference($filename); |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 2 | parent::update($media, $providerReferenceUpdated); |
|
59 | 2 | } |
|
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | 12 | public function getIcon() |
|
65 | { |
||
66 | 12 | return 'fa fa-file'; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | 13 | public function getName() |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | 4 | public function getType() |
|
84 | |||
85 | /** |
||
86 | * @param AbstractMedia $media |
||
87 | */ |
||
88 | 2 | protected function setFileImage(AbstractMedia $media) |
|
106 | |||
107 | /** |
||
108 | * @param FormMapper $formMapper |
||
109 | * @param string $name |
||
110 | * @param string $label |
||
111 | * @param array $options |
||
112 | */ |
||
113 | 2 | public function addFileField(FormMapper $formMapper, $name, $label, $options = []) |
|
117 | |||
118 | /** |
||
119 | * @param FormMapper $formMapper |
||
120 | * @param string $name |
||
121 | * @param string $label |
||
122 | * @param array $options |
||
123 | */ |
||
124 | 4 | public function addRequiredFileField(FormMapper $formMapper, $name, $label, $options = []) |
|
128 | |||
129 | /** |
||
130 | * @param array $options |
||
131 | * @return array |
||
132 | */ |
||
133 | 4 | private function getFileFieldConstraints(array $options) |
|
140 | |||
141 | /** |
||
142 | * @param array $options |
||
143 | * @return array |
||
144 | */ |
||
145 | 4 | protected function getFileConstraintOptions(array $options = []) |
|
152 | |||
153 | /** |
||
154 | * @param $object |
||
155 | * @param ExecutionContextInterface $context |
||
156 | */ |
||
157 | 4 | public function validateExtension($object, ExecutionContextInterface $context) |
|
170 | |||
171 | /** |
||
172 | * @param $extension |
||
173 | * @return string |
||
174 | */ |
||
175 | 3 | protected function getImageByExtension($extension) |
|
210 | |||
211 | /** |
||
212 | * @return string[] |
||
213 | */ |
||
214 | 3 | private function getArchiveExtensions() |
|
218 | |||
219 | /** |
||
220 | * @return string[] |
||
221 | */ |
||
222 | 3 | private function getAudioExtensions() |
|
226 | |||
227 | /** |
||
228 | * @return string[] |
||
229 | */ |
||
230 | 3 | private function getCodeExtensions() |
|
234 | |||
235 | /** |
||
236 | * @return string[] |
||
237 | */ |
||
238 | 3 | private function getSpreadsheetExtensions() |
|
242 | |||
243 | /** |
||
244 | * @return string[] |
||
245 | */ |
||
246 | 3 | private function getImageExtensions() |
|
250 | |||
251 | /** |
||
252 | * @return string[] |
||
253 | */ |
||
254 | 3 | private function getMovieExtensions() |
|
258 | |||
259 | /** |
||
260 | * @return string[] |
||
261 | */ |
||
262 | 3 | private function getPdfExtensions() |
|
266 | |||
267 | /** |
||
268 | * @return string[] |
||
269 | */ |
||
270 | 3 | private function getPresentationExtensions() |
|
274 | |||
275 | /** |
||
276 | * @return string[] |
||
277 | */ |
||
278 | 3 | private function getTextExtensions() |
|
282 | |||
283 | /** |
||
284 | * @return string[] |
||
285 | */ |
||
286 | 1 | private function getWordExtensions() |
|
290 | |||
291 | /** |
||
292 | * @param $imageFilename |
||
293 | * @return string |
||
294 | */ |
||
295 | 2 | protected function getImageLocation($imageFilename) |
|
304 | |||
305 | /** |
||
306 | * @return bool |
||
307 | */ |
||
308 | 3 | public function supportsDownload() |
|
312 | |||
313 | /** |
||
314 | * @return bool |
||
315 | */ |
||
316 | 3 | public function supportsEmbed() |
|
320 | |||
321 | /** |
||
322 | * @return bool |
||
323 | */ |
||
324 | 3 | public function supportsImage() |
|
328 | } |
||
329 |