Passed
Push — master ( e09aba...f9ce3a )
by Matt
04:00
created

ImageService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 46
c 8
b 0
f 0
dl 0
loc 99
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 2
A setCalculatedImageUris() 0 7 1
A setPropertiesFromEXIF() 0 36 6
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\Image;
6
use App\Repository\WanderRepository;
7
use App\Utils\ExifHelper;
8
use Deployer\Logger\Logger;
9
use Exception;
10
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
11
use PHPExif\Adapter\Exiftool;
12
use PHPExif\Reader\Reader;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
use Symfony\Component\Security\Core\Security;
16
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
17
use App\Utils\ExifHelperInterface;
18
19
class ImageService {
20
21
    /** @var UploaderHelper */
22
    private $uploaderHelper;
23
24
    /** @var CacheManager */
25
    private $imagineCacheManager;
26
27
    /** @var UrlGeneratorInterface */
28
    private $router;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    /** @var WanderRepository */
34
    private $wanderRepository;
35
36
    /** @var Reader */
37
    private $reader;
38
39
    /** @var string */
40
    private $imagesDirectory;
41
42
    /** @var string */
43
    private $exiftoolPath;
44
45
    public function __construct(
46
        UploaderHelper $uploaderHelper,
47
        CacheManager $imagineCacheManager,
48
        UrlGeneratorInterface $router,
49
        LoggerInterface $logger,
50
        WanderRepository $wanderRepository,
51
        string $imagesDirectory,
52
        ?string $exiftoolPath)
53
    {
54
        $this->uploaderHelper = $uploaderHelper;
55
        $this->imagineCacheManager = $imagineCacheManager;
56
        $this->router = $router;
57
        $this->logger = $logger;
58
        $this->wanderRepository = $wanderRepository;
59
        $this->imagesDirectory = $imagesDirectory;
60
        $this->exiftoolPath = $exiftoolPath;
61
62
        if ($exiftoolPath !== null) {
63
            // Will throw if path is wrong
64
            $adapter = new Exiftool([
65
                'toolpath' => $exiftoolPath
66
            ]);
67
            $this->reader = new Reader($adapter);
68
        } else {
69
            $this->reader = Reader::factory(Reader::TYPE_EXIFTOOL);
70
        }
71
    }
72
73
    public function setCalculatedImageUris(Image $image): void
74
    {
75
        $image_asset_path = $this->uploaderHelper->asset($image);
76
        $image->setImageUri($image_asset_path);
77
        $image->setMarkerImageUri($this->imagineCacheManager->getBrowserPath($image_asset_path, 'marker_thumb'));
78
        $image->setMediumImageUri($this->imagineCacheManager->getBrowserPath($image_asset_path, 'map_popup_image'));
79
        $image->setImageShowUri($this->router->generate('image_show', ['id' => $image->getId()]));
80
    }
81
82
    public function setPropertiesFromEXIF(
83
            Image $image,
84
            bool $updateRelatedWanders = true
85
        ): void
86
    {
87
        if ($image->getMimeType() == 'image/jpeg') {
88
            try {
89
                $exif = $this->reader->read($this->imagesDirectory . '/' . $image->getName());
90
                /** @var ExifHelperInterface */
91
                $exifHelper = new ExifHelper($exif);
92
93
                $image->setTitle($exifHelper->getTitle());
94
                $image->setDescription($exifHelper->getDescription());
95
                $image->setLatlng($exifHelper->getGPS());
1 ignored issue
show
Bug introduced by
Are you sure the usage of $exifHelper->getGPS() targeting App\Utils\ExifHelper::getGPS() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
                $image->setKeywords($exifHelper->getKeywords());
97
                $image->setRating($exifHelper->getRating());
98
99
                $capturedAt = $exifHelper->getCreationDate();
100
                $image->setCapturedAt($capturedAt);
0 ignored issues
show
Bug introduced by
It seems like $capturedAt can also be of type null; however, parameter $capturedAt of App\Entity\Image::setCapturedAt() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
                $image->setCapturedAt(/** @scrutinizer ignore-type */ $capturedAt);
Loading history...
101
102
                if ($capturedAt instanceof \DateTime && $updateRelatedWanders) {
103
                    // Try and find associated wander(s) by looking for
104
                    // wanders whose timespan includes this image.
105
                    $wanders = $this->wanderRepository->findWhereIncludesDate($capturedAt);
106
                    foreach ($wanders as $wander) {
107
                        $image->addWander($wander);
108
                    }
109
                }
110
            }
111
            catch(Exception $e) {
112
                // We've started to rely on the information gathered here, so I think
113
                // this should be a proper error now.
114
                throw new Exception('Error getting image Exif information: ' . $e->getMessage(), $e->getCode(), $e);
115
            }
116
        } else {
117
            $this->logger->info('Ignoring non-JPEG file when trying to set properties from EXIT.');
118
        }
119
    }
120
}
121
122