Completed
Pull Request — master (#436)
by
unknown
02:59
created

loadOrganizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace CultuurNet\UDB3\Organizer\CommandHandler;
4
5
use Broadway\Repository\RepositoryInterface;
6
use CultuurNet\Geocoding\GeocodingServiceInterface;
7
use CultuurNet\UDB3\Address\AddressFormatterInterface;
8
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler;
9
use CultuurNet\UDB3\Organizer\Commands\UpdateGeoCoordinates;
10
use CultuurNet\UDB3\Organizer\Organizer;
11
use CultuurNet\UDB3\Organizer\OrganizerRepository;
12
13 View Code Duplication
class UpdateGeoCoordinatesCommandHandler extends Udb3CommandHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * @var OrganizerRepository
17
     */
18
    private $organizerRepository;
19
20
    /**
21
     * @var AddressFormatterInterface
22
     */
23
    private $defaultAddressFormatter;
24
25
    /**
26
     * @var AddressFormatterInterface
27
     */
28
    private $fallbackAddressFormatter;
29
30
    /**
31
     * @var GeocodingServiceInterface
32
     */
33
    private $geocodingService;
34
35
36
    public function __construct(
37
        RepositoryInterface $placeRepository,
38
        AddressFormatterInterface $defaultAddressFormatter,
39
        AddressFormatterInterface $fallbackAddressFormatter,
40
        GeocodingServiceInterface $geocodingService
41
    ) {
42
        $this->organizerRepository = $placeRepository;
0 ignored issues
show
Documentation Bug introduced by
$placeRepository is of type object<Broadway\Repository\RepositoryInterface>, but the property $organizerRepository was declared to be of type object<CultuurNet\UDB3\O...er\OrganizerRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
43
        $this->defaultAddressFormatter = $defaultAddressFormatter;
44
        $this->fallbackAddressFormatter = $fallbackAddressFormatter;
45
        $this->geocodingService = $geocodingService;
46
    }
47
48
49
    protected function handleUpdateGeoCoordinates(UpdateGeoCoordinates $updateGeoCoordinates)
50
    {
51
        $coordinates = $this->geocodingService->getCoordinates(
52
            $this->defaultAddressFormatter->format(
53
                $updateGeoCoordinates->address()
54
            )
55
        );
56
57
        if ($coordinates === null) {
58
            $coordinates = $this->geocodingService->getCoordinates(
59
                $this->fallbackAddressFormatter->format(
60
                    $updateGeoCoordinates->address()
61
                )
62
            );
63
        }
64
65
        if ($coordinates === null) {
66
            return;
67
        }
68
69
        /** @var Organizer $organizer */
70
        $organizer = $this->loadOrganizer($updateGeoCoordinates->organizerId());
71
        $organizer->updateGeoCoordinates($coordinates);
72
        $this->organizerRepository->save($organizer);
73
    }
74
75
    protected function loadOrganizer(string $id)
76
    {
77
        return $this->organizerRepository->load($id);
78
    }
79
}
80