| Conditions | 9 |
| Paths | 45 |
| Total Lines | 51 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 114 | public function setPropertiesFromEXIF( |
||
| 115 | Image $image, |
||
| 116 | bool $updateRelatedWander = true |
||
| 117 | ): void |
||
| 118 | { |
||
| 119 | if ($image->getMimeType() !== 'image/jpeg') { |
||
| 120 | $this->logger->info('Ignoring non-JPEG file when trying to set properties from EXIT.'); |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | |||
| 124 | try { |
||
| 125 | $exif = $this->reader->read($this->imagesDirectory . '/' . $image->getName()); |
||
| 126 | /** @var ExifHelperInterface */ |
||
| 127 | $exifHelper = new ExifHelper($exif); |
||
| 128 | |||
| 129 | $image->setTitle($exifHelper->getTitle()); |
||
| 130 | $image->setDescription($exifHelper->getDescription()); |
||
| 131 | $image->setLatlng($exifHelper->getGPS()); |
||
| 132 | $image->setTagsText(implode(",", $exifHelper->getKeywords() ?? [])); |
||
| 133 | $image->setRating($exifHelper->getRating()); |
||
| 134 | |||
| 135 | $neighbourhood = $exifHelper->getLocation(); |
||
| 136 | if ($neighbourhood === null && $image->hasLatlng()) { |
||
| 137 | // If we didn't set the location from the EXIF, this will try setting it |
||
| 138 | // from the GPS co-ordinates. |
||
| 139 | $neighbourhood = $this->locationService->getLocationName($image->getLatitude(), $image->getLongitude()); |
||
| 140 | } |
||
| 141 | if ($neighbourhood !== null) { |
||
| 142 | $image->setLocation($neighbourhood); |
||
| 143 | } |
||
| 144 | |||
| 145 | $capturedAt = $exifHelper->getCreationDate(); |
||
| 146 | if ($capturedAt instanceof \DateTime) { |
||
| 147 | $image->setCapturedAt($capturedAt); |
||
| 148 | if ($updateRelatedWander) { |
||
| 149 | // Try and find associated wander by looking for |
||
| 150 | // wanders whose timespan includes this image. |
||
| 151 | // TODO: Work out a way of adding some windage so images |
||
| 152 | // shot a little time either side of the track log still |
||
| 153 | // match. |
||
| 154 | $wander = $this->wanderRepository->findFirstWhereIncludesDate($capturedAt); |
||
| 155 | if ($wander !== null) { |
||
| 156 | $image->setWander($wander); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | catch(Exception $e) { |
||
| 162 | // We've started to rely on the information gathered here, so I think |
||
| 163 | // this should be a proper error now. |
||
| 164 | throw new Exception('Error getting image Exif information: ' . $e->getMessage(), $e->getCode(), $e); |
||
| 165 | } |
||
| 169 |