| Total Complexity | 71 |
| Total Lines | 476 |
| 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) |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $paramString |
||
| 178 | * @internal |
||
| 179 | */ |
||
| 180 | private function parseParams(string $paramString): void |
||
| 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->isValidSmartCrop()) { |
||
| 244 | $params[] = self::PARAM_SMART_CROP; |
||
| 245 | } elseif ($this->squareCrop) { |
||
| 246 | $params[] = self::PARAM_SQUARE_CROP; |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($this->variantOfUrlWithFileName) { |
||
| 250 | if (empty($params)) { |
||
| 251 | return $this->url; |
||
| 252 | } |
||
| 253 | return $this->url . implode('-', $params) . '/'; |
||
| 254 | } |
||
| 255 | |||
| 256 | if ($this->border !== null) { |
||
| 257 | $params[] = self::PARAM_BORDER . $this->border; |
||
| 258 | } |
||
| 259 | |||
| 260 | if ($this->verticalFlip) { |
||
| 261 | $params[] = self::PARAM_FLIP_VERTICAL; |
||
| 262 | } |
||
| 263 | |||
| 264 | if ($this->horizontalFlip) { |
||
| 265 | $params[] = self::PARAM_FLIP_HORIZONTAL; |
||
| 266 | } |
||
| 267 | |||
| 268 | if (empty($params)) { |
||
| 269 | return $this->url; |
||
| 270 | } |
||
| 271 | return $this->url . '=' . implode('-', $params); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | private function isValidSmartCrop(): bool |
||
| 278 | { |
||
| 279 | return $this->smartCrop && ( |
||
| 280 | $this->size !== null || |
||
| 281 | ($this->width !== null && $this->height !== null) || |
||
| 282 | ($this->size === null && $this->width === null && $this->height === null) |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the URL with the best image size. |
||
| 288 | * |
||
| 289 | * @return string url |
||
| 290 | */ |
||
| 291 | public function getBestSizeUrl(): string |
||
| 292 | { |
||
| 293 | $params = [self::PARAM_SIZE . '0']; |
||
| 294 | if ($this->variantOfUrlWithFileName) { |
||
| 295 | return $this->url . implode('-', $params) . '/'; |
||
| 296 | } |
||
| 297 | return $this->url . '=' . implode('-', $params); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns a hash value for this object. |
||
| 302 | * Can be used to generate a file save path. |
||
| 303 | * |
||
| 304 | * @param string $hashAlgorithm Hash algorithm. Default is md5. |
||
| 305 | * @param int $parts Nesting path. Maximum 6. |
||
| 306 | * @param int $partLength The length of the nested path. |
||
| 307 | * @return string unique hash value of this object |
||
| 308 | */ |
||
| 309 | public function getHashUrl(string $hashAlgorithm = 'md5', int $parts = 1, int $partLength = 2): string |
||
| 310 | { |
||
| 311 | $hash = hash($hashAlgorithm, $this->getUrl()); |
||
| 312 | $hashLength = strlen($hash); |
||
| 313 | $parts = max(1, min(6, $parts)); |
||
| 314 | if ($parts > 1) { |
||
| 315 | $partLength = max(1, min($partLength, (int)($hashLength / $parts))); |
||
| 316 | $partsBuild = []; |
||
| 317 | for ($i = 0; $i < $parts; $i++) { |
||
| 318 | $partsBuild[] = substr($hash, $i * $partLength, $partLength); |
||
| 319 | } |
||
| 320 | $hash = implode('/', $partsBuild) . '/' . $hash; |
||
| 321 | } |
||
| 322 | return $hash; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Sets the original image size. |
||
| 327 | * |
||
| 328 | * @return GoogleImage |
||
| 329 | */ |
||
| 330 | public function useOriginalSize(): GoogleImage |
||
| 331 | { |
||
| 332 | $this->setSize(0); |
||
| 333 | return $this; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param int|null $size |
||
| 338 | * @return GoogleImage |
||
| 339 | */ |
||
| 340 | public function setSize(?int $size): GoogleImage |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param int|null $width |
||
| 350 | * @return GoogleImage |
||
| 351 | */ |
||
| 352 | public function setWidth(?int $width): GoogleImage |
||
| 353 | { |
||
| 354 | $this->width = $width; |
||
| 355 | $this->size = null; |
||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param int|null $height |
||
| 361 | * @return GoogleImage |
||
| 362 | */ |
||
| 363 | public function setHeight(?int $height): GoogleImage |
||
| 364 | { |
||
| 365 | $this->height = $height; |
||
| 366 | $this->size = null; |
||
| 367 | return $this; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param int $width |
||
| 372 | * @param int $height |
||
| 373 | * @return GoogleImage |
||
| 374 | */ |
||
| 375 | public function setWidthAndHeight(int $width, int $height): GoogleImage |
||
| 376 | { |
||
| 377 | $this->width = $width; |
||
| 378 | $this->height = $height; |
||
| 379 | $this->size = null; |
||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param int|null $border |
||
| 385 | * @return GoogleImage |
||
| 386 | */ |
||
| 387 | public function setBorder(?int $border): GoogleImage |
||
| 388 | { |
||
| 389 | $this->border = $border; |
||
| 390 | return $this; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param bool $squareCrop |
||
| 395 | * @return GoogleImage |
||
| 396 | */ |
||
| 397 | public function setSquareCrop(bool $squareCrop): GoogleImage |
||
| 398 | { |
||
| 399 | $this->squareCrop = $squareCrop; |
||
| 400 | $this->smartCrop = false; |
||
| 401 | return $this; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param bool $smartCrop |
||
| 406 | * @return GoogleImage |
||
| 407 | */ |
||
| 408 | public function setSmartCrop(bool $smartCrop): GoogleImage |
||
| 409 | { |
||
| 410 | $this->smartCrop = $smartCrop; |
||
| 411 | $this->squareCrop = false; |
||
| 412 | return $this; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param bool $verticalFlip |
||
| 417 | * @return GoogleImage |
||
| 418 | */ |
||
| 419 | public function setVerticalFlip(bool $verticalFlip): GoogleImage |
||
| 420 | { |
||
| 421 | $this->verticalFlip = $verticalFlip; |
||
| 422 | return $this; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param bool $horizontalFlip |
||
| 427 | * @return GoogleImage |
||
| 428 | */ |
||
| 429 | public function setHorizontalFlip(bool $horizontalFlip): GoogleImage |
||
| 430 | { |
||
| 431 | $this->horizontalFlip = $horizontalFlip; |
||
| 432 | return $this; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return int|null |
||
| 437 | */ |
||
| 438 | public function getSize(): ?int |
||
| 439 | { |
||
| 440 | return $this->size; |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return int|null |
||
| 445 | */ |
||
| 446 | public function getWidth(): ?int |
||
| 447 | { |
||
| 448 | return $this->width; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return int|null |
||
| 453 | */ |
||
| 454 | public function getHeight(): ?int |
||
| 455 | { |
||
| 456 | return $this->height; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return int|null |
||
| 461 | */ |
||
| 462 | public function getBorder(): ?int |
||
| 463 | { |
||
| 464 | return $this->border; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return bool |
||
| 469 | */ |
||
| 470 | public function isSquareCrop(): bool |
||
| 471 | { |
||
| 472 | return $this->squareCrop; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public function isSmartCrop(): bool |
||
| 479 | { |
||
| 480 | return $this->smartCrop; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | public function isVerticalFlip(): bool |
||
| 487 | { |
||
| 488 | return $this->verticalFlip; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return bool |
||
| 493 | */ |
||
| 494 | public function isHorizontalFlip(): bool |
||
| 495 | { |
||
| 496 | return $this->horizontalFlip; |
||
| 497 | } |
||
| 498 | |||
| 499 | public function reset(): void |
||
| 500 | { |
||
| 501 | $this->size = null; |
||
| 502 | $this->width = null; |
||
| 503 | $this->height = null; |
||
| 504 | $this->border = null; |
||
| 505 | $this->squareCrop = false; |
||
| 506 | $this->smartCrop = false; |
||
| 507 | $this->verticalFlip = false; |
||
| 508 | $this->horizontalFlip = false; |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $destPath |
||
| 513 | * @return ImageInfo |
||
| 514 | * @throws GooglePlayException |
||
| 515 | */ |
||
| 516 | public function saveAs(string $destPath): ImageInfo |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function __toString() |
||
| 552 | { |
||
| 553 | return $this->getUrl(); |
||
| 554 | } |
||
| 555 | } |
||
| 556 |