1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Event; |
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\Event\Commands\UpdateGeoCoordinatesFromAddress; |
10
|
|
|
|
11
|
|
View Code Duplication |
class GeoCoordinatesCommandHandler extends Udb3CommandHandler |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RepositoryInterface |
15
|
|
|
*/ |
16
|
|
|
private $eventRepository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var AddressFormatterInterface |
20
|
|
|
*/ |
21
|
|
|
private $defaultAddressFormatter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AddressFormatterInterface |
25
|
|
|
*/ |
26
|
|
|
private $fallbackAddressFormatter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var GeocodingServiceInterface |
30
|
|
|
*/ |
31
|
|
|
private $geocodingService; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RepositoryInterface $eventRepository |
35
|
|
|
* @param AddressFormatterInterface $defaultAddressFormatter |
36
|
|
|
* @param AddressFormatterInterface $fallbackAddressFormatter |
37
|
|
|
* @param GeocodingServiceInterface $geocodingService |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
RepositoryInterface $eventRepository, |
41
|
|
|
AddressFormatterInterface $defaultAddressFormatter, |
42
|
|
|
AddressFormatterInterface $fallbackAddressFormatter, |
43
|
|
|
GeocodingServiceInterface $geocodingService |
44
|
|
|
) { |
45
|
|
|
$this->eventRepository = $eventRepository; |
46
|
|
|
$this->defaultAddressFormatter = $defaultAddressFormatter; |
47
|
|
|
$this->fallbackAddressFormatter = $fallbackAddressFormatter; |
48
|
|
|
$this->geocodingService = $geocodingService; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param UpdateGeoCoordinatesFromAddress $updateGeoCoordinates |
53
|
|
|
*/ |
54
|
|
|
public function handleUpdateGeoCoordinatesFromAddress(UpdateGeoCoordinatesFromAddress $updateGeoCoordinates) |
55
|
|
|
{ |
56
|
|
|
$coordinates = $this->geocodingService->getCoordinates( |
57
|
|
|
$this->defaultAddressFormatter->format( |
58
|
|
|
$updateGeoCoordinates->getAddress() |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
if ($coordinates === null) { |
63
|
|
|
$coordinates = $this->geocodingService->getCoordinates( |
64
|
|
|
$this->fallbackAddressFormatter->format( |
65
|
|
|
$updateGeoCoordinates->getAddress() |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var Event $event */ |
71
|
|
|
$event = $this->eventRepository->load($updateGeoCoordinates->getItemId()); |
72
|
|
|
$event->updateGeoCoordinates($coordinates); |
73
|
|
|
$this->eventRepository->save($event); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.