Completed
Push — master ( a0d1e1...4fda4b )
by Matt
04:38
created

LocationService::setImageLocation()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
nc 4
nop 2
dl 0
loc 24
rs 9.5555
c 1
b 0
f 0
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\Image;
6
use App\Entity\Neighbourhood;
7
use App\Repository\NeighbourhoodRepository;
8
use Doctrine\ORM\EntityManagerInterface;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\HandlerStack;
11
use Psr\Log\LoggerInterface;
12
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;
13
14
// TODO: We should probably make this a generic interface and have Nominatim
15
// as just one possible provider. But let's get it working first and see
16
// what happens.
17
class LocationService
18
{
19
    /** @var NeighbourhoodRepository */
20
    private $neighbourhoodRepository;
21
22
    /** @var LoggerInterface */
23
    private $logger;
24
25
    public function __construct(
26
        LoggerInterface $logger,
27
        NeighbourhoodRepository $neighbourhoodRepository)
28
    {
29
        $this->logger = $logger;
30
        $this->neighbourhoodRepository = $neighbourhoodRepository;
31
    }
32
33
    public function setImageLocation(
34
        Image $image,
35
        bool $overwriteExisting = false
36
        ): bool
37
    {
38
        if ($overwriteExisting === false && $image->hasLocation()) {
39
            $this->logger->info('Deliberately not overwriting existing image location');
40
            return false;
41
        }
42
43
        if (!$image->hasLatlng()) {
44
            $this->logger->info('Ignoring image that has no latlng.');
45
            return false;
46
        }
47
48
        $neighbourhood = $this->neighbourhoodRepository
49
            ->findByLatlng($image->getLatitude(), $image->getLongitude());
0 ignored issues
show
Bug introduced by
It seems like $image->getLatitude() can also be of type null; however, parameter $lat of App\Repository\Neighbour...ository::findByLatlng() does only seem to accept double, 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

49
            ->findByLatlng(/** @scrutinizer ignore-type */ $image->getLatitude(), $image->getLongitude());
Loading history...
Bug introduced by
It seems like $image->getLongitude() can also be of type null; however, parameter $lng of App\Repository\Neighbour...ository::findByLatlng() does only seem to accept double, 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

49
            ->findByLatlng($image->getLatitude(), /** @scrutinizer ignore-type */ $image->getLongitude());
Loading history...
50
51
        if ($neighbourhood === null) {
52
            return false;
53
        }
54
55
        $image->setLocation($neighbourhood->getLsoa11ln());
56
        return true;
57
    }
58
}