| Total Complexity | 57 |
| Total Lines | 464 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Image |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @ORM\Id |
||
| 53 | * @ORM\GeneratedValue |
||
| 54 | * @ORM\Column(type="integer") |
||
| 55 | * |
||
| 56 | * @Groups({"image:list", "image:item"}) |
||
| 57 | */ |
||
| 58 | private $id; |
||
| 59 | |||
| 60 | // TODO: We probably don't want this massive field being returned |
||
| 61 | // as part of any API response, etc. |
||
| 62 | /** |
||
| 63 | * @Vich\UploadableField(mapping="image", fileNameProperty="name", size="sizeInBytes", |
||
| 64 | * mimeType="mimeType", originalName="originalName", dimensions="dimensions") |
||
| 65 | * |
||
| 66 | * @Ignore() |
||
| 67 | */ |
||
| 68 | private $imageFile; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 72 | * |
||
| 73 | * @Groups({"image:list", "image:item"}) |
||
| 74 | */ |
||
| 75 | private $name; // For Vich, not for us. We use Title. |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 79 | * |
||
| 80 | * @Groups({"image:list", "image:item"}) |
||
| 81 | */ |
||
| 82 | private $title; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @ORM\Column(type="text", nullable=true) |
||
| 86 | * |
||
| 87 | * @Groups({"image:list", "image:item"}) |
||
| 88 | */ |
||
| 89 | private $description; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @ORM\Column(type="integer", nullable=true) |
||
| 93 | * |
||
| 94 | * @Groups({"image:list", "image:item"}) |
||
| 95 | */ |
||
| 96 | private $sizeInBytes; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 100 | * |
||
| 101 | * @Groups({"image:list", "image:item"}) |
||
| 102 | */ |
||
| 103 | private $mimeType; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 107 | * |
||
| 108 | * @Groups({"image:list", "image:item"}) |
||
| 109 | */ |
||
| 110 | private $originalName; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @ORM\Column(type="simple_array", nullable=true) |
||
| 114 | * |
||
| 115 | * @Groups({"image:list", "image:item"}) |
||
| 116 | */ |
||
| 117 | private $dimensions = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @ORM\Column(type="datetime") |
||
| 121 | * |
||
| 122 | * @Groups({"image:list", "image:item"}) |
||
| 123 | * |
||
| 124 | * @var \DateTimeInterface|null |
||
| 125 | */ |
||
| 126 | private $updatedAt; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @ORM\Column(type="simple_array", nullable=true) |
||
| 130 | * @Assert\Count( |
||
| 131 | * min = 2, |
||
| 132 | * max = 2, |
||
| 133 | * minMessage = "There must be exactly two numbers in a latitude/longitude pair", |
||
| 134 | * maxMessage = "There must be exactly two numbers in a latitude/longitude pair", |
||
| 135 | * exactMessage = "Co-ordinates must consist of a latitude, longitude pair." |
||
| 136 | * ) |
||
| 137 | * |
||
| 138 | * @Groups({"image:list", "image:item"}) |
||
| 139 | * |
||
| 140 | */ |
||
| 141 | private $latlng = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @ORM\Column(type="array", nullable=true, ) |
||
| 145 | * |
||
| 146 | * @Groups({"image:list", "image:item"}) |
||
| 147 | */ |
||
| 148 | private $keywords = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @ORM\Column(type="datetime", nullable=true) |
||
| 152 | * |
||
| 153 | * @Groups({"image:list", "image:item"}) |
||
| 154 | */ |
||
| 155 | private $capturedAt; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @ORM\Column(type="integer", nullable=true) |
||
| 159 | * @Groups({"image:list", "image:item"}) |
||
| 160 | */ |
||
| 161 | private $rating; |
||
| 162 | |||
| 163 | |||
| 164 | // TODO: This @Ignore was here from when this was a many-to-many. Do we still |
||
| 165 | // need it? |
||
| 166 | /** |
||
| 167 | * @ORM\ManyToOne(targetEntity=Wander::class, inversedBy="images") |
||
| 168 | * |
||
| 169 | * @Ignore() |
||
| 170 | */ |
||
| 171 | private $wander; |
||
| 172 | |||
| 173 | public function __construct() |
||
| 174 | { |
||
| 175 | // $this->wanders = new ArrayCollection(); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * If manually uploading a file (i.e. not using Symfony Form) ensure an instance |
||
| 180 | * of 'UploadedFile' is injected into this setter to trigger the update. If this |
||
| 181 | * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter |
||
| 182 | * must be able to accept an instance of 'File' as the bundle will inject one here |
||
| 183 | * during Doctrine hydration. |
||
| 184 | * |
||
| 185 | * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile |
||
| 186 | */ |
||
| 187 | public function setImageFile(?File $imageFile = null): void |
||
| 188 | { |
||
| 189 | $this->imageFile = $imageFile; |
||
| 190 | |||
| 191 | if (null !== $imageFile) { |
||
| 192 | // It is required that at least one field changes if you are using doctrine |
||
| 193 | // otherwise the event listeners won't be called and the file is lost |
||
| 194 | $this->updatedAt = new \DateTimeImmutable(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @Ignore() |
||
| 200 | */ |
||
| 201 | public function getImageFile(): ?File |
||
| 202 | { |
||
| 203 | return $this->imageFile; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getId(): ?int |
||
| 207 | { |
||
| 208 | return $this->id; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getName(): ?string |
||
| 212 | { |
||
| 213 | return $this->name; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function setName(?string $name): self |
||
| 217 | { |
||
| 218 | $this->name = $name; |
||
| 219 | |||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getTitle(): ?string |
||
| 224 | { |
||
| 225 | return $this->title; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setTitle(?string $title): self |
||
| 229 | { |
||
| 230 | $this->title = $title; |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getDescription(): ?string |
||
| 236 | { |
||
| 237 | return $this->description; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function setDescription(?string $description): self |
||
| 241 | { |
||
| 242 | $this->description = $description; |
||
| 243 | |||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | public function getSizeInBytes(): ?int |
||
| 248 | { |
||
| 249 | return $this->sizeInBytes; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function setSizeInBytes(?int $sizeInBytes): self |
||
| 253 | { |
||
| 254 | $this->sizeInBytes = $sizeInBytes; |
||
| 255 | |||
| 256 | return $this; |
||
| 257 | } |
||
| 258 | |||
| 259 | public function getMimeType(): ?string |
||
| 260 | { |
||
| 261 | return $this->mimeType; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function setMimeType(?string $mimeType): self |
||
| 265 | { |
||
| 266 | $this->mimeType = $mimeType; |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function getOriginalName(): ?string |
||
| 272 | { |
||
| 273 | return $this->originalName; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function setOriginalName(?string $originalName): self |
||
| 277 | { |
||
| 278 | $this->originalName = $originalName; |
||
| 279 | |||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getDimensions(): ?array |
||
| 284 | { |
||
| 285 | return $this->dimensions; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function setDimensions(?array $dimensions): self |
||
| 289 | { |
||
| 290 | $this->dimensions = $dimensions; |
||
| 291 | |||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function getUpdatedAt(): ?\DateTimeInterface |
||
| 296 | { |
||
| 297 | return $this->updatedAt; |
||
| 298 | } |
||
| 299 | |||
| 300 | public function setUpdatedAt(\DateTimeInterface $updatedAt): self |
||
| 301 | { |
||
| 302 | $this->updatedAt = $updatedAt; |
||
| 303 | return $this; |
||
| 304 | } |
||
| 305 | |||
| 306 | public function getLatitude(): ?float |
||
| 307 | { |
||
| 308 | if ($this->latlng === null || |
||
| 309 | !is_array($this->latlng) || |
||
| 310 | empty($this->latlng)) { |
||
| 311 | return null; |
||
| 312 | } |
||
| 313 | return $this->latlng[0]; |
||
| 314 | } |
||
| 315 | |||
| 316 | public function getLongitude(): ?float |
||
| 317 | { |
||
| 318 | if ($this->latlng === null || |
||
| 319 | !is_array($this->latlng) || |
||
| 320 | empty($this->latlng)) { |
||
| 321 | return null; |
||
| 322 | } |
||
| 323 | return $this->latlng[1]; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function getLatlng(): ?array |
||
| 327 | { |
||
| 328 | return $this->latlng; |
||
| 329 | } |
||
| 330 | |||
| 331 | public function hasLatlng(): bool |
||
| 332 | { |
||
| 333 | return is_array($this->latlng) && count($this->latlng) == 2; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function setLatlng(?array $latlng): self |
||
| 337 | { |
||
| 338 | $this->latlng = $latlng; |
||
| 339 | |||
| 340 | return $this; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getKeywords(): ?array |
||
| 344 | { |
||
| 345 | return $this->keywords; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function setKeywords(?array $keywords): self |
||
| 349 | { |
||
| 350 | $this->keywords = $keywords; |
||
| 351 | |||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | public function getCapturedAt(): ?\DateTimeInterface |
||
| 356 | { |
||
| 357 | return $this->capturedAt; |
||
| 358 | } |
||
| 359 | |||
| 360 | public function setCapturedAt(\DateTimeInterface $capturedAt): self |
||
| 361 | { |
||
| 362 | $this->capturedAt = $capturedAt; |
||
| 363 | |||
| 364 | return $this; |
||
| 365 | } |
||
| 366 | |||
| 367 | // /** |
||
| 368 | // * @return Collection|Wander[] |
||
| 369 | // */ |
||
| 370 | // public function getWanders(): Collection |
||
| 371 | // { |
||
| 372 | // return $this->wanders; |
||
| 373 | // } |
||
| 374 | |||
| 375 | // public function addWander(Wander $wander): self |
||
| 376 | // { |
||
| 377 | // if (!$this->wanders->contains($wander)) { |
||
| 378 | // $this->wanders[] = $wander; |
||
| 379 | // $wander->addImage($this); |
||
| 380 | // } |
||
| 381 | |||
| 382 | // return $this; |
||
| 383 | // } |
||
| 384 | |||
| 385 | // public function removeWander(Wander $wander): self |
||
| 386 | // { |
||
| 387 | // if ($this->wanders->removeElement($wander)) { |
||
| 388 | // $wander->removeImage($this); |
||
| 389 | // } |
||
| 390 | |||
| 391 | // return $this; |
||
| 392 | // } |
||
| 393 | |||
| 394 | public function getWander(): ?Wander |
||
| 395 | { |
||
| 396 | return $this->wander; |
||
| 397 | } |
||
| 398 | |||
| 399 | public function setWander(?Wander $wander): self |
||
| 400 | { |
||
| 401 | $this->wander = $wander; |
||
| 402 | return $this; |
||
| 403 | } |
||
| 404 | |||
| 405 | public function getRating(): ?int |
||
| 406 | { |
||
| 407 | return $this->rating; |
||
| 408 | } |
||
| 409 | |||
| 410 | public function setRating(?int $rating): self |
||
| 411 | { |
||
| 412 | $this->rating = $rating; |
||
| 413 | |||
| 414 | return $this; |
||
| 415 | } |
||
| 416 | |||
| 417 | |||
| 418 | /* Computed (set up by Doctrine postLoad listener) */ |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @Groups({"image:list", "image:item"}) |
||
| 422 | */ |
||
| 423 | private $imageUri; |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @Groups({"image:list", "image:item"}) |
||
| 427 | */ |
||
| 428 | private $markerImageUri; |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @Groups({"image:list", "image:item"}) |
||
| 432 | */ |
||
| 433 | private $mediumImageUri; |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Groups({"image:list", "image:item"}) |
||
| 437 | */ |
||
| 438 | private $imageShowUri; |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @ORM\Column(type="array", nullable=true) |
||
| 442 | */ |
||
| 443 | private $auto_tags = []; |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 447 | */ |
||
| 448 | private $location; |
||
| 449 | |||
| 450 | |||
| 451 | public function setImageUri($imageUri) { |
||
| 452 | $this->imageUri = $imageUri; |
||
| 453 | } |
||
| 454 | |||
| 455 | public function getImageUri(): ?string { |
||
| 456 | return $this->imageUri; |
||
| 457 | } |
||
| 458 | |||
| 459 | public function setMarkerImageUri($markerImageUri) { |
||
| 460 | $this->markerImageUri = $markerImageUri; |
||
| 461 | } |
||
| 462 | public function getMarkerImageUri(): ?string { |
||
| 463 | return $this->markerImageUri; |
||
| 464 | } |
||
| 465 | |||
| 466 | public function setMediumImageUri($mediumImageUri) { |
||
| 467 | $this->mediumImageUri = $mediumImageUri; |
||
| 468 | } |
||
| 469 | public function getMediumImageUri(): ?string { |
||
| 470 | return $this->mediumImageUri; |
||
| 471 | } |
||
| 472 | |||
| 473 | public function setImageShowUri($imageShowUri) { |
||
| 474 | $this->imageShowUri = $imageShowUri; |
||
| 475 | } |
||
| 476 | public function getImageShowUri(): ?string { |
||
| 477 | return $this->imageShowUri; |
||
| 478 | } |
||
| 479 | |||
| 480 | public function getAutoTags(): ?array |
||
| 481 | { |
||
| 482 | return $this->auto_tags; |
||
| 483 | } |
||
| 484 | |||
| 485 | public function setAutoTags(?array $auto_tags): self |
||
| 486 | { |
||
| 487 | $this->auto_tags = $auto_tags; |
||
| 488 | |||
| 489 | return $this; |
||
| 490 | } |
||
| 491 | public function getAutoTagsCount(): int { |
||
| 492 | if (is_array($this->auto_tags)) { |
||
|
|
|||
| 493 | return count($this->auto_tags); |
||
| 494 | } |
||
| 495 | return 0; |
||
| 496 | } |
||
| 497 | |||
| 498 | public function getLocation(): ?string |
||
| 501 | } |
||
| 502 | |||
| 503 | public function hasLocation(): bool |
||
| 504 | { |
||
| 505 | return $this->location !== null && $this->location <> ''; |
||
| 506 | } |
||
| 507 | |||
| 508 | public function setLocation(?string $location): self |
||
| 509 | { |
||
| 510 | $this->location = $location; |
||
| 511 | |||
| 512 | return $this; |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 |