Total Complexity | 70 |
Total Lines | 469 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GoogleImage 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 GoogleImage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
78 | class GoogleImage |
||
79 | { |
||
80 | private const PARAM_SIZE = 's'; |
||
81 | private const PARAM_WIDTH = 'w'; |
||
82 | private const PARAM_HEIGHT = 'h'; |
||
83 | private const PARAM_BORDER = 'b'; |
||
84 | private const PARAM_SQUARE_CROP = 'c'; |
||
85 | private const PARAM_SMART_CROP = 'p'; |
||
86 | private const PARAM_FLIP_VERTICAL = 'fv'; |
||
87 | private const PARAM_FLIP_HORIZONTAL = 'fh'; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | */ |
||
92 | private $url; |
||
93 | /** |
||
94 | * Size longer of height or width to N pixels. |
||
95 | * If set, then GoogleImage::width and GoogleImage::height are set to null. |
||
96 | * |
||
97 | * @var int|null |
||
98 | */ |
||
99 | private $size; |
||
100 | /** |
||
101 | * Image width size up to N pixels. |
||
102 | * If set, then GoogleImage::size is null. |
||
103 | * |
||
104 | * @var int|null |
||
105 | */ |
||
106 | private $width; |
||
107 | /** |
||
108 | * Image height size up to N pixels. |
||
109 | * If set, then GoogleImage::size is null. |
||
110 | * |
||
111 | * @var int|null |
||
112 | */ |
||
113 | private $height; |
||
114 | /** |
||
115 | * @var int|null |
||
116 | */ |
||
117 | private $border; |
||
118 | /** |
||
119 | * @var bool |
||
120 | */ |
||
121 | private $squareCrop = false; |
||
122 | /** |
||
123 | * @var bool |
||
124 | */ |
||
125 | private $smartCrop = false; |
||
126 | /** |
||
127 | * @var bool |
||
128 | */ |
||
129 | private $verticalFlip = false; |
||
130 | /** |
||
131 | * @var bool |
||
132 | */ |
||
133 | private $horizontalFlip = false; |
||
134 | /** |
||
135 | * Variant URL with file name at the end. |
||
136 | * A special URL structure is used. |
||
137 | * URL starts with /- |
||
138 | * |
||
139 | * Example URL: |
||
140 | * https://lh3.googleusercontent.com/-LB59qNIqtS4/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rf3YR_W16kFTuh5tCgHpZ02_ndQOg/s100-no/photo.jpg |
||
141 | * |
||
142 | * @var bool |
||
143 | * @internal |
||
144 | */ |
||
145 | private $variantOfUrlWithFileName = false; |
||
146 | |||
147 | /** |
||
148 | * @param string $url google image url |
||
149 | * @param bool $keepParams keep url params |
||
150 | */ |
||
151 | public function __construct(string $url, bool $keepParams = true) |
||
152 | { |
||
153 | $httpComponents = parse_url($url); |
||
154 | $path = ltrim($httpComponents['path'], '/'); |
||
155 | $parts = explode('/', $path); |
||
156 | $paramString = null; |
||
157 | if (count($parts) > 4 && strpos($parts[0], '-') === 0) { |
||
158 | if (isset($parts[5]) || (isset($parts[4]) && strrpos($url, '/') === strlen($url) - 1)) { |
||
159 | $paramString = $parts[4]; |
||
160 | } |
||
161 | $parts = array_slice($parts, 0, 4); |
||
162 | $path = implode('/', $parts); |
||
163 | $url = $httpComponents['scheme'] . '://' . $httpComponents['host'] . '/' . $path . '/'; |
||
164 | $this->variantOfUrlWithFileName = true; |
||
165 | } elseif (($pos = strpos($url, '=')) !== false) { |
||
166 | $paramString = substr($url, $pos + 1); |
||
167 | $url = substr($url, 0, $pos); |
||
168 | } |
||
169 | |||
170 | $this->url = $url; |
||
171 | if ($keepParams && $paramString !== null) { |
||
172 | $this->parseParams($paramString); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $paramString |
||
178 | * @internal |
||
179 | */ |
||
180 | private function parseParams(string $paramString): void |
||
181 | { |
||
182 | $params = explode('-', $paramString); |
||
183 | foreach ($params as $param) { |
||
184 | if (empty($param)) { |
||
185 | continue; |
||
186 | } |
||
187 | |||
188 | $command = $param[0]; // 1 char |
||
189 | switch ($command) { |
||
190 | case self::PARAM_SIZE: |
||
191 | $arg = (int)substr($param, 1); |
||
192 | $this->setSize($arg); |
||
193 | break; |
||
194 | case self::PARAM_WIDTH: |
||
195 | $arg = (int)substr($param, 1); |
||
196 | $this->setWidth($arg); |
||
197 | break; |
||
198 | case self::PARAM_HEIGHT: |
||
199 | $arg = (int)substr($param, 1); |
||
200 | $this->setHeight($arg); |
||
201 | break; |
||
202 | case self::PARAM_BORDER: |
||
203 | $arg = (int)substr($param, 1); |
||
204 | $this->setBorder($arg); |
||
205 | break; |
||
206 | case self::PARAM_SQUARE_CROP: |
||
207 | $this->setSquareCrop(true); |
||
208 | break; |
||
209 | case self::PARAM_SMART_CROP: |
||
210 | $this->setSmartCrop(true); |
||
211 | break; |
||
212 | default: |
||
213 | switch ($param) { |
||
214 | case self::PARAM_FLIP_VERTICAL: |
||
215 | $this->setVerticalFlip(true); |
||
216 | break; |
||
217 | case self::PARAM_FLIP_HORIZONTAL: |
||
218 | $this->setHorizontalFlip(true); |
||
219 | break; |
||
220 | // ignore other parameters |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getUrl(): string |
||
230 | { |
||
231 | $params = []; |
||
232 | if ($this->size !== null) { |
||
233 | $params[] = self::PARAM_SIZE . $this->size; |
||
234 | } else { |
||
235 | if ($this->width !== null) { |
||
236 | $params[] = self::PARAM_WIDTH . $this->width; |
||
237 | } |
||
238 | if ($this->height !== null) { |
||
239 | $params[] = self::PARAM_HEIGHT . $this->height; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | if ($this->smartCrop && ( |
||
244 | $this->size !== null || |
||
245 | ($this->width !== null && $this->height !== null) || |
||
246 | ($this->size === null && $this->width === null && $this->height === null) |
||
247 | ) |
||
248 | ) { |
||
249 | $params[] = self::PARAM_SMART_CROP; |
||
250 | } elseif ($this->squareCrop) { |
||
251 | $params[] = self::PARAM_SQUARE_CROP; |
||
252 | } |
||
253 | |||
254 | if ($this->variantOfUrlWithFileName) { |
||
255 | if (empty($params)) { |
||
256 | return $this->url; |
||
257 | } |
||
258 | return $this->url . implode('-', $params) . '/'; |
||
259 | } |
||
260 | |||
261 | if ($this->border !== null) { |
||
262 | $params[] = self::PARAM_BORDER . $this->border; |
||
263 | } |
||
264 | |||
265 | if ($this->verticalFlip) { |
||
266 | $params[] = self::PARAM_FLIP_VERTICAL; |
||
267 | } |
||
268 | |||
269 | if ($this->horizontalFlip) { |
||
270 | $params[] = self::PARAM_FLIP_HORIZONTAL; |
||
271 | } |
||
272 | |||
273 | if (empty($params)) { |
||
274 | return $this->url; |
||
275 | } |
||
276 | return $this->url . '=' . implode('-', $params); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Returns the URL with the best image size. |
||
281 | * |
||
282 | * @return string url |
||
283 | */ |
||
284 | public function getBestSizeUrl(): string |
||
285 | { |
||
286 | $params = [self::PARAM_SIZE . '0']; |
||
287 | if ($this->variantOfUrlWithFileName) { |
||
288 | return $this->url . implode('-', $params) . '/'; |
||
289 | } |
||
290 | return $this->url . '=' . implode('-', $params); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Returns a hash value for this object. |
||
295 | * Can be used to generate a file save path. |
||
296 | * |
||
297 | * @param string $hashAlgorithm Hash algorithm. Default is md5. |
||
298 | * @param int $parts Nesting path. Maximum 6. |
||
299 | * @param int $partLength The length of the nested path. |
||
300 | * @return string unique hash value of this object |
||
301 | */ |
||
302 | public function getHashUrl(string $hashAlgorithm = 'md5', int $parts = 1, int $partLength = 2): string |
||
303 | { |
||
304 | $hash = hash($hashAlgorithm, $this->getUrl()); |
||
305 | $hashLength = strlen($hash); |
||
306 | $parts = max(1, min(6, $parts)); |
||
307 | if ($parts > 1) { |
||
308 | $partLength = max(1, min($partLength, (int)($hashLength / $parts))); |
||
309 | $partsBuild = []; |
||
310 | for ($i = 0; $i < $parts; $i++) { |
||
311 | $partsBuild[] = substr($hash, $i * $partLength, $partLength); |
||
312 | } |
||
313 | $hash = implode('/', $partsBuild) . '/' . $hash; |
||
314 | } |
||
315 | return $hash; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Sets the original image size. |
||
320 | * |
||
321 | * @return GoogleImage |
||
322 | */ |
||
323 | public function useOriginalSize(): GoogleImage |
||
324 | { |
||
325 | $this->setSize(0); |
||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param int|null $size |
||
331 | * @return GoogleImage |
||
332 | */ |
||
333 | public function setSize(?int $size): GoogleImage |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param int|null $width |
||
343 | * @return GoogleImage |
||
344 | */ |
||
345 | public function setWidth(?int $width): GoogleImage |
||
346 | { |
||
347 | $this->width = $width; |
||
348 | $this->size = null; |
||
349 | return $this; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param int|null $height |
||
354 | * @return GoogleImage |
||
355 | */ |
||
356 | public function setHeight(?int $height): GoogleImage |
||
357 | { |
||
358 | $this->height = $height; |
||
359 | $this->size = null; |
||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param int $width |
||
365 | * @param int $height |
||
366 | * @return GoogleImage |
||
367 | */ |
||
368 | public function setWidthAndHeight(int $width, int $height): GoogleImage |
||
369 | { |
||
370 | $this->width = $width; |
||
371 | $this->height = $height; |
||
372 | $this->size = null; |
||
373 | return $this; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param int|null $border |
||
378 | * @return GoogleImage |
||
379 | */ |
||
380 | public function setBorder(?int $border): GoogleImage |
||
381 | { |
||
382 | $this->border = $border; |
||
383 | return $this; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @param bool $squareCrop |
||
388 | * @return GoogleImage |
||
389 | */ |
||
390 | public function setSquareCrop(bool $squareCrop): GoogleImage |
||
391 | { |
||
392 | $this->squareCrop = $squareCrop; |
||
393 | $this->smartCrop = false; |
||
394 | return $this; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param bool $smartCrop |
||
399 | * @return GoogleImage |
||
400 | */ |
||
401 | public function setSmartCrop(bool $smartCrop): GoogleImage |
||
402 | { |
||
403 | $this->smartCrop = $smartCrop; |
||
404 | $this->squareCrop = false; |
||
405 | return $this; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @param bool $verticalFlip |
||
410 | * @return GoogleImage |
||
411 | */ |
||
412 | public function setVerticalFlip(bool $verticalFlip): GoogleImage |
||
413 | { |
||
414 | $this->verticalFlip = $verticalFlip; |
||
415 | return $this; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param bool $horizontalFlip |
||
420 | * @return GoogleImage |
||
421 | */ |
||
422 | public function setHorizontalFlip(bool $horizontalFlip): GoogleImage |
||
423 | { |
||
424 | $this->horizontalFlip = $horizontalFlip; |
||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @return int|null |
||
430 | */ |
||
431 | public function getSize(): ?int |
||
432 | { |
||
433 | return $this->size; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @return int|null |
||
438 | */ |
||
439 | public function getWidth(): ?int |
||
440 | { |
||
441 | return $this->width; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * @return int|null |
||
446 | */ |
||
447 | public function getHeight(): ?int |
||
448 | { |
||
449 | return $this->height; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @return int|null |
||
454 | */ |
||
455 | public function getBorder(): ?int |
||
456 | { |
||
457 | return $this->border; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @return bool |
||
462 | */ |
||
463 | public function isSquareCrop(): bool |
||
464 | { |
||
465 | return $this->squareCrop; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * @return bool |
||
470 | */ |
||
471 | public function isSmartCrop(): bool |
||
472 | { |
||
473 | return $this->smartCrop; |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @return bool |
||
478 | */ |
||
479 | public function isVerticalFlip(): bool |
||
480 | { |
||
481 | return $this->verticalFlip; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * @return bool |
||
486 | */ |
||
487 | public function isHorizontalFlip(): bool |
||
488 | { |
||
489 | return $this->horizontalFlip; |
||
490 | } |
||
491 | |||
492 | public function reset(): void |
||
493 | { |
||
494 | $this->size = null; |
||
495 | $this->width = null; |
||
496 | $this->height = null; |
||
497 | $this->border = null; |
||
498 | $this->squareCrop = false; |
||
499 | $this->smartCrop = false; |
||
500 | $this->verticalFlip = false; |
||
501 | $this->horizontalFlip = false; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param string $destPath |
||
506 | * @return ImageInfo |
||
507 | * @throws GooglePlayException |
||
508 | */ |
||
509 | public function saveAs(string $destPath): ImageInfo |
||
538 | } |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @return string |
||
543 | */ |
||
544 | public function __toString() |
||
545 | { |
||
546 | return $this->getUrl(); |
||
547 | } |
||
548 | } |
||
549 |