Total Complexity | 51 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResizedImageRepository 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 ResizedImageRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class ResizedImageRepository |
||
38 | { |
||
39 | /** |
||
40 | * @var CKFinder |
||
41 | */ |
||
42 | protected $app; |
||
43 | |||
44 | /** |
||
45 | * @var Config |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | /** |
||
50 | * @var Acl |
||
51 | */ |
||
52 | protected $acl; |
||
53 | |||
54 | /** |
||
55 | * Event dispatcher. |
||
56 | * |
||
57 | * @var $dispatcher |
||
|
|||
58 | */ |
||
59 | protected $dispatcher; |
||
60 | |||
61 | /** |
||
62 | * @param CKFinder $app |
||
63 | */ |
||
64 | public function __construct(CKFinder $app) |
||
65 | { |
||
66 | $this->config = $app['config']; |
||
67 | $this->acl = $app['acl']; |
||
68 | $this->dispatcher = $app['dispatcher']; |
||
69 | $this->app = $app; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns a resized image for the provided source file. |
||
74 | * |
||
75 | * If an appropriate resized version already exists, it is reused. |
||
76 | * |
||
77 | * @param ResourceType $sourceFileResourceType |
||
78 | * @param string $sourceFileDir |
||
79 | * @param string $sourceFileName |
||
80 | * @param int $requestedWidth |
||
81 | * @param int $requestedHeight |
||
82 | * |
||
83 | * @return ResizedImage |
||
84 | * |
||
85 | * @throws \Exception |
||
86 | */ |
||
87 | public function getResizedImage(ResourceType $sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight) |
||
88 | { |
||
89 | $resizedImage = new ResizedImage( |
||
90 | $this, |
||
91 | $sourceFileResourceType, |
||
92 | $sourceFileDir, |
||
93 | $sourceFileName, |
||
94 | $requestedWidth, |
||
95 | $requestedHeight |
||
96 | ); |
||
97 | |||
98 | if (!$this->acl->isAllowed($sourceFileResourceType->getName(), $sourceFileDir, Permission::IMAGE_RESIZE_CUSTOM) && |
||
99 | !$this->isSizeAllowedInConfig($requestedWidth, $requestedHeight)) { |
||
100 | throw new UnauthorizedException('Provided size is not allowed in images.sizes configuration'); |
||
101 | } |
||
102 | |||
103 | if (!$resizedImage->exists() && $resizedImage->requestedSizeIsValid()) { |
||
104 | $resizedImage->create(); |
||
105 | |||
106 | $resizeImageEvent = new ResizeImageEvent($this->app, $resizedImage); |
||
107 | $this->dispatcher->dispatch(CKFinderEvent::CREATE_RESIZED_IMAGE, $resizeImageEvent); |
||
108 | |||
109 | if (!$resizeImageEvent->isPropagationStopped()) { |
||
110 | $resizedImage = $resizeImageEvent->getResizedImage(); |
||
111 | $resizedImage->save(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $resizedImage; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Returns an existing resized image. |
||
120 | * |
||
121 | * @param ResourceType $sourceFileResourceType |
||
122 | * @param string $sourceFileDir |
||
123 | * @param string $sourceFileName |
||
124 | * @param string $thumbnailFileName |
||
125 | * |
||
126 | * @return ResizedImage |
||
127 | * |
||
128 | * @throws FileNotFoundException |
||
129 | */ |
||
130 | public function getExistingResizedImage(ResourceType $sourceFileResourceType, $sourceFileDir, $sourceFileName, $thumbnailFileName) |
||
131 | { |
||
132 | $size = ResizedImage::getSizeFromFilename($thumbnailFileName); |
||
133 | |||
134 | $resizedImage = new ResizedImage( |
||
135 | $this, |
||
136 | $sourceFileResourceType, |
||
137 | $sourceFileDir, |
||
138 | $sourceFileName, |
||
139 | $size['width'], |
||
140 | $size['height'], |
||
141 | true |
||
142 | ); |
||
143 | |||
144 | if (!$resizedImage->exists()) { |
||
145 | throw new FileNotFoundException('Resized image not found'); |
||
146 | } |
||
147 | |||
148 | $resizedImage->load(); |
||
149 | |||
150 | return $resizedImage; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return CKFinder |
||
155 | */ |
||
156 | public function getContainer() |
||
157 | { |
||
158 | return $this->app; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Checks if the provided image size is allowed in the configuration. |
||
163 | * |
||
164 | * This is checked when `Permission::IMAGE_RESIZE_CUSTOM` |
||
165 | * is not allowed in the source file folder. |
||
166 | * |
||
167 | * @param int $width |
||
168 | * @param int $height |
||
169 | * |
||
170 | * @return bool `true` if the provided size is allowed in the configuration. |
||
171 | */ |
||
172 | protected function isSizeAllowedInConfig($width, $height) |
||
173 | { |
||
174 | $configSizes = $this->config->get('images.sizes'); |
||
175 | |||
176 | foreach ($configSizes as $size) { |
||
177 | if ($size['width'] === $width && $size['height'] === $height) { |
||
178 | return true; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | return false; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the size name defined in the configuration, where width |
||
187 | * or height are equal to those given in parameters. |
||
188 | * |
||
189 | * Resized images keep the original image aspect ratio. |
||
190 | * When an image is resized using the size from the configuration, |
||
191 | * at least one of the borders has the same length. |
||
192 | * |
||
193 | * @param int $width |
||
194 | * @param int $height |
||
195 | * |
||
196 | * @return bool `true` if the size from the configuration was used. |
||
197 | */ |
||
198 | protected function getSizeNameFromConfig($width, $height) |
||
199 | { |
||
200 | $configSizes = $this->config->get('images.sizes'); |
||
201 | |||
202 | foreach ($configSizes as $sizeName => $size) { |
||
203 | if ($size['width'] === $width || $size['height'] === $height) { |
||
204 | return $sizeName; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return null; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Deletes all resized images for a given file. |
||
213 | * |
||
214 | * @param ResourceType $sourceFileResourceType |
||
215 | * @param string $sourceFilePath |
||
216 | * @param string $sourceFileName |
||
217 | * |
||
218 | * @return bool `true` if deleted |
||
219 | */ |
||
220 | public function deleteResizedImages(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName) |
||
221 | { |
||
222 | $resizedImagesPath = Path::combine($sourceFileResourceType->getDirectory(), $sourceFilePath, ResizedImage::DIR, $sourceFileName); |
||
223 | |||
224 | $backend = $sourceFileResourceType->getBackend(); |
||
225 | |||
226 | if ($backend->hasDirectory($resizedImagesPath)) { |
||
227 | return $backend->deleteDir($resizedImagesPath); |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Copies all resized images for a given file. |
||
235 | * |
||
236 | * @param ResourceType $sourceFileResourceType |
||
237 | * @param string $sourceFilePath |
||
238 | * @param string $sourceFileName |
||
239 | * @param ResourceType $targetFileResourceType |
||
240 | * @param string $targetFilePath |
||
241 | * @param string $targetFileName |
||
242 | */ |
||
243 | public function copyResizedImages(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Renames all resized images created for a given file. |
||
272 | * |
||
273 | * @param ResourceType $sourceFileResourceType |
||
274 | * @param string $sourceFilePath |
||
275 | * @param string $originalSourceFileName |
||
276 | * @param string $newSourceFileName |
||
277 | */ |
||
278 | public function renameResizedImages(ResourceType $sourceFileResourceType, $sourceFilePath, $originalSourceFileName, $newSourceFileName) |
||
279 | { |
||
280 | $resizedImagesDir = Path::combine($sourceFileResourceType->getDirectory(), $sourceFilePath, ResizedImage::DIR); |
||
281 | $resizedImagesPath = Path::combine($resizedImagesDir, $originalSourceFileName); |
||
282 | $newResizedImagesPath = Path::combine($resizedImagesDir, $newSourceFileName); |
||
283 | |||
284 | $backend = $sourceFileResourceType->getBackend(); |
||
285 | |||
286 | if ($backend->hasDirectory($resizedImagesPath)) { |
||
287 | if ($backend->rename($resizedImagesPath, $newResizedImagesPath)) { |
||
288 | $resizedImages = $backend->listContents($newResizedImagesPath); |
||
289 | |||
290 | foreach ($resizedImages as $resizedImage) { |
||
291 | if (!isset($resizedImage['path'])) { |
||
292 | continue; |
||
293 | } |
||
294 | |||
295 | $sourceImageSize = ResizedImage::getSizeFromFilename($resizedImage['basename']); |
||
296 | $newResizedImageFilename = ResizedImage::createFilename($newSourceFileName, $sourceImageSize['width'], $sourceImageSize['height']); |
||
297 | |||
298 | $backend->rename($resizedImage['path'], Path::combine($newResizedImagesPath, $newResizedImageFilename)); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Returns a list of resized images generated for a given file. |
||
306 | * |
||
307 | * @param ResourceType $sourceFileResourceType source file resource type |
||
308 | * @param string $sourceFilePath source file backend-relative path |
||
309 | * @param string $sourceFileName source file name |
||
310 | * @param array $filterSizes array containing names of sizes defined |
||
311 | * in the `images.sizes` configuration |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public function getResizedImagesList(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, $filterSizes = array()) |
||
316 | { |
||
317 | $resizedImagesPath = Path::combine($sourceFileResourceType->getDirectory(), $sourceFilePath, ResizedImage::DIR, $sourceFileName); |
||
318 | |||
319 | $backend = $sourceFileResourceType->getBackend(); |
||
320 | |||
321 | $resizedImages = array(); |
||
322 | |||
323 | if (!$backend->hasDirectory($resizedImagesPath)) { |
||
324 | return $resizedImages; |
||
325 | } |
||
326 | |||
327 | $resizedImagesFiles = array_filter( |
||
328 | $backend->listContents($resizedImagesPath), |
||
329 | function ($v) { |
||
330 | return isset($v['type']) && $v['type'] === 'file'; |
||
331 | } |
||
332 | ); |
||
333 | |||
334 | foreach ($resizedImagesFiles as $resizedImage) { |
||
335 | $size = ResizedImage::getSizeFromFilename($resizedImage['basename']); |
||
336 | |||
337 | if ($sizeName = $this->getSizeNameFromConfig($size['width'], $size['height'])) { |
||
338 | if (empty($filterSizes) || in_array($sizeName, $filterSizes)) { |
||
339 | $resizedImages[$sizeName] = $this->createNodeValue($resizedImage); |
||
340 | } |
||
341 | continue; |
||
342 | } |
||
343 | |||
344 | if (empty($filterSizes)) { |
||
345 | if (!isset($resizedImages['__custom'])) { |
||
346 | $resizedImages['__custom'] = array(); |
||
347 | } |
||
348 | |||
349 | $resizedImages['__custom'][] = $this->createNodeValue($resizedImage); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | return $resizedImages; |
||
354 | } |
||
355 | |||
356 | protected function createNodeValue($resizedImage) |
||
357 | { |
||
358 | if (isset($resizedImage['url'])) { |
||
359 | return array( |
||
360 | 'name' => $resizedImage['basename'], |
||
361 | 'url' => $resizedImage['url'] |
||
362 | ); |
||
363 | } |
||
364 | |||
365 | return $resizedImage['basename']; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param ResourceType $sourceFileResourceType |
||
370 | * @param string $sourceFilePath |
||
371 | * @param string $sourceFileName |
||
372 | * @param int $width |
||
373 | * @param int $height |
||
374 | * |
||
375 | * @return ResizedImage|null |
||
376 | */ |
||
377 | public function getResizedImageBySize(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, $width, $height) |
||
421 | } |
||
422 | } |
||
423 |