|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Place; |
|
4
|
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
|
6
|
|
|
use Broadway\Domain\DomainMessage; |
|
7
|
|
|
use Broadway\EventHandling\EventListenerInterface; |
|
8
|
|
|
use CultureFeed_Cdb_Data_Address; |
|
9
|
|
|
use CultuurNet\UDB3\Actor\ActorImportedFromUDB2; |
|
10
|
|
|
use CultuurNet\UDB3\Address\CultureFeedAddressFactoryInterface; |
|
11
|
|
|
use CultuurNet\UDB3\Cdb\ActorItemFactory; |
|
12
|
|
|
use CultuurNet\UDB3\Place\Commands\UpdateGeoCoordinatesFromAddress; |
|
13
|
|
|
use CultuurNet\UDB3\Place\Events\MajorInfoUpdated; |
|
14
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceCreated; |
|
15
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2; |
|
16
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class GeoCoordinatesProcessManager implements EventListenerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var CommandBusInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $commandBus; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var CultureFeedAddressFactoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $addressFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LoggerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $logger; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param CommandBusInterface $commandBus |
|
38
|
|
|
* @param CultureFeedAddressFactoryInterface $addressFactory |
|
39
|
|
|
* @param LoggerInterface $logger |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
CommandBusInterface $commandBus, |
|
43
|
|
|
CultureFeedAddressFactoryInterface $addressFactory, |
|
44
|
|
|
LoggerInterface $logger |
|
45
|
|
|
) { |
|
46
|
|
|
$this->commandBus = $commandBus; |
|
47
|
|
|
$this->addressFactory = $addressFactory; |
|
48
|
|
|
$this->logger = $logger; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
private function getEventHandlers() |
|
55
|
|
|
{ |
|
56
|
|
|
return [ |
|
57
|
|
|
PlaceCreated::class => 'handlePlaceCreated', |
|
58
|
|
|
MajorInfoUpdated::class => 'handleMajorInfoUpdated', |
|
59
|
|
|
PlaceImportedFromUDB2::class => 'handleActorImportedFromUDB2', |
|
60
|
|
|
PlaceUpdatedFromUDB2::class => 'handleActorImportedFromUDB2', |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param DomainMessage $domainMessage |
|
66
|
|
|
* |
|
67
|
|
|
* @uses handlePlaceCreated |
|
68
|
|
|
* @uses handleMajorInfoUpdated |
|
69
|
|
|
* @uses handleActorImportedFromUDB2 |
|
70
|
|
|
*/ |
|
71
|
|
|
public function handle(DomainMessage $domainMessage) |
|
72
|
|
|
{ |
|
73
|
|
|
$payload = $domainMessage->getPayload(); |
|
74
|
|
|
$className = get_class($payload); |
|
75
|
|
|
$eventHandlers = $this->getEventHandlers(); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($eventHandlers[$className])) { |
|
78
|
|
|
$eventHandler = $eventHandlers[$className]; |
|
79
|
|
|
call_user_func([$this, $eventHandler], $payload); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param PlaceCreated $placeCreated |
|
85
|
|
|
*/ |
|
86
|
|
|
private function handlePlaceCreated(PlaceCreated $placeCreated) |
|
87
|
|
|
{ |
|
88
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
|
89
|
|
|
$placeCreated->getPlaceId(), |
|
90
|
|
|
$placeCreated->getAddress() |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$this->commandBus->dispatch($command); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param MajorInfoUpdated $majorInfoUpdated |
|
98
|
|
|
*/ |
|
99
|
|
|
private function handleMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
|
100
|
|
|
{ |
|
101
|
|
|
// We don't know if the address has actually been updated because |
|
102
|
|
|
// MajorInfoUpdated is too coarse, but if we use the cached geocoding |
|
103
|
|
|
// service we won't be wasting much resources when using a naive |
|
104
|
|
|
// approach like this. |
|
105
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
|
106
|
|
|
$majorInfoUpdated->getPlaceId(), |
|
107
|
|
|
$majorInfoUpdated->getAddress() |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$this->commandBus->dispatch($command); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param ActorImportedFromUDB2 $actorImportedFromUDB2 |
|
115
|
|
|
*/ |
|
116
|
|
|
private function handleActorImportedFromUDB2(ActorImportedFromUDB2 $actorImportedFromUDB2) |
|
117
|
|
|
{ |
|
118
|
|
|
$actor = ActorItemFactory::createActorFromCdbXml( |
|
119
|
|
|
$actorImportedFromUDB2->getCdbXmlNamespaceUri(), |
|
120
|
|
|
$actorImportedFromUDB2->getCdbXml() |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$contactInfo = $actor->getContactInfo(); |
|
124
|
|
|
|
|
125
|
|
|
// Do nothing if no contact info is found. |
|
126
|
|
|
if (!$contactInfo) { |
|
127
|
|
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Get all physical locations from the list of addresses. |
|
131
|
|
|
$addresses = array_map( |
|
132
|
|
|
function (CultureFeed_Cdb_Data_Address $address) { |
|
133
|
|
|
return $address->getPhysicalAddress(); |
|
134
|
|
|
}, |
|
135
|
|
|
$contactInfo->getAddresses() |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
// Filter out addresses without physical location. |
|
139
|
|
|
$addresses = array_filter($addresses); |
|
140
|
|
|
|
|
141
|
|
|
// Do nothing if no address is found. |
|
142
|
|
|
if (empty($addresses)) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/* @var \CultureFeed_Cdb_Data_Address_PhysicalAddress $cdbAddress */ |
|
147
|
|
|
$cdbAddress = $addresses[0]; |
|
148
|
|
|
|
|
149
|
|
|
try { |
|
150
|
|
|
// Convert the cdbxml address to a udb3 address. |
|
151
|
|
|
$address = $this->addressFactory->fromCdbAddress($cdbAddress); |
|
152
|
|
|
} catch (\InvalidArgumentException $e) { |
|
153
|
|
|
// If conversion failed, log an error and do nothing. |
|
154
|
|
|
$this->logger->error( |
|
155
|
|
|
'Could not convert a cdbxml address to a udb3 address for geocoding.', |
|
156
|
|
|
[ |
|
157
|
|
|
'placeId' => $actorImportedFromUDB2->getActorId(), |
|
158
|
|
|
'error' => $e->getMessage(), |
|
159
|
|
|
] |
|
160
|
|
|
); |
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// We don't know if the address has actually been updated because |
|
165
|
|
|
// ActorImportedFromUDB2 is too coarse, but if we use the cached |
|
166
|
|
|
// geocoding service we won't be wasting much resources when using |
|
167
|
|
|
// a naive approach like this. |
|
168
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
|
169
|
|
|
$actorImportedFromUDB2->getActorId(), |
|
170
|
|
|
$address |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
$this->commandBus->dispatch($command); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|