1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Event; |
4
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
6
|
|
|
use Broadway\Domain\DomainMessage; |
7
|
|
|
use Broadway\EventHandling\EventListenerInterface; |
8
|
|
|
use CultuurNet\UDB3\Address\CultureFeedAddressFactoryInterface; |
9
|
|
|
use CultuurNet\UDB3\Cdb\EventItemFactory; |
10
|
|
|
use CultuurNet\UDB3\Event\Commands\UpdateGeoCoordinatesFromAddress; |
11
|
|
|
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2; |
12
|
|
|
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
|
15
|
|
|
class GeoCoordinatesProcessManager implements EventListenerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CommandBusInterface |
19
|
|
|
*/ |
20
|
|
|
private $commandBus; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CultureFeedAddressFactoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $addressFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param CommandBusInterface $commandBus |
29
|
|
|
* @param CultureFeedAddressFactoryInterface $addressFactory |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
CommandBusInterface $commandBus, |
33
|
|
|
CultureFeedAddressFactoryInterface $addressFactory |
34
|
|
|
) { |
35
|
|
|
$this->commandBus = $commandBus; |
36
|
|
|
$this->addressFactory = $addressFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
private function getEventHandlers() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
EventImportedFromUDB2::class => 'handleEventImportedFromUDB2', |
46
|
|
|
EventUpdatedFromUDB2::class => 'handleEventUpdatedFromUDB2', |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param DomainMessage $domainMessage |
52
|
|
|
* |
53
|
|
|
* @uses handleEventImportedFromUDB2 |
54
|
|
|
* @uses handleEventUpdatedFromUDB2 |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function handle(DomainMessage $domainMessage) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$payload = $domainMessage->getPayload(); |
59
|
|
|
$className = get_class($payload); |
60
|
|
|
$eventHandlers = $this->getEventHandlers(); |
61
|
|
|
|
62
|
|
|
if (isset($eventHandlers[$className])) { |
63
|
|
|
$eventHandler = $eventHandlers[$className]; |
64
|
|
|
call_user_func([$this, $eventHandler], $payload); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param EventImportedFromUDB2 $eventImportedFromUDB2 |
70
|
|
|
* @throws \CultureFeed_Cdb_ParseException |
71
|
|
|
*/ |
72
|
|
|
private function handleEventImportedFromUDB2( |
73
|
|
|
EventImportedFromUDB2 $eventImportedFromUDB2 |
74
|
|
|
) { |
75
|
|
|
$this->dispatchCommand( |
76
|
|
|
$eventImportedFromUDB2->getCdbXmlNamespaceUri(), |
77
|
|
|
$eventImportedFromUDB2->getCdbXml(), |
78
|
|
|
$eventImportedFromUDB2->getEventId() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
84
|
|
|
* @throws \CultureFeed_Cdb_ParseException |
85
|
|
|
*/ |
86
|
|
|
private function handleEventUpdatedFromUDB2( |
87
|
|
|
EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
88
|
|
|
) { |
89
|
|
|
$this->dispatchCommand( |
90
|
|
|
$eventUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
91
|
|
|
$eventUpdatedFromUDB2->getCdbXml(), |
92
|
|
|
$eventUpdatedFromUDB2->getEventId() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $cdbXmlNamespaceUri |
98
|
|
|
* @param string $cdbXml |
99
|
|
|
* @param string $eventId |
100
|
|
|
* @throws \CultureFeed_Cdb_ParseException |
101
|
|
|
*/ |
102
|
|
|
private function dispatchCommand( |
103
|
|
|
$cdbXmlNamespaceUri, |
104
|
|
|
$cdbXml, |
105
|
|
|
$eventId |
106
|
|
|
) { |
107
|
|
|
$event = EventItemFactory::createEventFromCdbXml( |
108
|
|
|
$cdbXmlNamespaceUri, |
109
|
|
|
$cdbXml |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
// Location is required, else the create would fail. |
113
|
|
|
$location = $event->getLocation(); |
114
|
|
|
if ($location->getCdbid() || $location->getExternalId()) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Address is required, else the create would fail. |
119
|
|
|
$physicalAddress = $location->getAddress()->getPhysicalAddress(); |
120
|
|
|
if (!$physicalAddress) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Address is always valid, else the create would fail. |
125
|
|
|
$address = $this->addressFactory->fromCdbAddress($physicalAddress); |
126
|
|
|
|
127
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
128
|
|
|
$eventId, |
129
|
|
|
$address |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->commandBus->dispatch($command); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.