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\UpdateGeoCoordinatesFromAddress; |
10
|
|
|
use CultuurNet\UDB3\Organizer\Organizer; |
11
|
|
|
use CultuurNet\UDB3\Organizer\OrganizerRepository; |
12
|
|
|
|
13
|
|
View Code Duplication |
class UpdateGeoCoordinatesFromAddressCommandHandler extends Udb3CommandHandler |
|
|
|
|
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; |
|
|
|
|
43
|
|
|
$this->defaultAddressFormatter = $defaultAddressFormatter; |
44
|
|
|
$this->fallbackAddressFormatter = $fallbackAddressFormatter; |
45
|
|
|
$this->geocodingService = $geocodingService; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
protected function handleUpdateGeoCoordinatesFromAddress(UpdateGeoCoordinatesFromAddress $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
|
|
|
|
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.