1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Place; |
4
|
|
|
|
5
|
|
|
use CultureFeed_Cdb_Data_Address; |
6
|
|
|
use CultuurNet\UDB3\Actor\ActorImportedFromUDB2; |
7
|
|
|
use CultuurNet\UDB3\Cdb\ActorItemFactory; |
8
|
|
|
use CultuurNet\UDB3\Offer\AbstractGeoCoordinatesProcessManager; |
9
|
|
|
use CultuurNet\UDB3\Place\Commands\UpdateGeoCoordinatesFromAddress; |
10
|
|
|
use CultuurNet\UDB3\Place\Events\AddressUpdated; |
11
|
|
|
use CultuurNet\UDB3\Place\Events\MajorInfoUpdated; |
12
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceCreated; |
13
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2; |
14
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2; |
15
|
|
|
|
16
|
|
|
class GeoCoordinatesProcessManager extends AbstractGeoCoordinatesProcessManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
protected function getEventHandlers() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
PlaceCreated::class => 'handlePlaceCreated', |
25
|
|
|
MajorInfoUpdated::class => 'handleMajorInfoUpdated', |
26
|
|
|
AddressUpdated::class => 'handleAddressUpdated', |
27
|
|
|
PlaceImportedFromUDB2::class => 'handleActorImportedFromUDB2', |
28
|
|
|
PlaceUpdatedFromUDB2::class => 'handleActorImportedFromUDB2', |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param PlaceCreated $placeCreated |
34
|
|
|
*/ |
35
|
|
|
protected function handlePlaceCreated(PlaceCreated $placeCreated) |
36
|
|
|
{ |
37
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
38
|
|
|
$placeCreated->getPlaceId(), |
39
|
|
|
$placeCreated->getAddress() |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->commandBus->dispatch($command); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param MajorInfoUpdated $majorInfoUpdated |
47
|
|
|
*/ |
48
|
|
|
protected function handleMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
49
|
|
|
{ |
50
|
|
|
// We don't know if the address has actually been updated because |
51
|
|
|
// MajorInfoUpdated is too coarse, but if we use the cached geocoding |
52
|
|
|
// service we won't be wasting much resources when using a naive |
53
|
|
|
// approach like this. |
54
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
55
|
|
|
$majorInfoUpdated->getPlaceId(), |
56
|
|
|
$majorInfoUpdated->getAddress() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->commandBus->dispatch($command); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param AddressUpdated $addressUpdated |
64
|
|
|
*/ |
65
|
|
|
protected function handleAddressUpdated(AddressUpdated $addressUpdated) |
66
|
|
|
{ |
67
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
68
|
|
|
$addressUpdated->getPlaceId(), |
69
|
|
|
$addressUpdated->getAddress() |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->commandBus->dispatch($command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param ActorImportedFromUDB2 $actorImportedFromUDB2 |
77
|
|
|
* @throws \CultureFeed_Cdb_ParseException |
78
|
|
|
*/ |
79
|
|
|
protected function handleActorImportedFromUDB2(ActorImportedFromUDB2 $actorImportedFromUDB2) |
80
|
|
|
{ |
81
|
|
|
$actor = ActorItemFactory::createActorFromCdbXml( |
82
|
|
|
$actorImportedFromUDB2->getCdbXmlNamespaceUri(), |
83
|
|
|
$actorImportedFromUDB2->getCdbXml() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$contactInfo = $actor->getContactInfo(); |
87
|
|
|
|
88
|
|
|
// Do nothing if no contact info is found. |
89
|
|
|
if (!$contactInfo) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Get all physical locations from the list of addresses. |
94
|
|
|
$addresses = array_map( |
95
|
|
|
function (CultureFeed_Cdb_Data_Address $address) { |
96
|
|
|
return $address->getPhysicalAddress(); |
97
|
|
|
}, |
98
|
|
|
$contactInfo->getAddresses() |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// Filter out addresses without physical location. |
102
|
|
|
$addresses = array_filter($addresses); |
103
|
|
|
|
104
|
|
|
// Do nothing if no address is found. |
105
|
|
|
if (empty($addresses)) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* @var \CultureFeed_Cdb_Data_Address_PhysicalAddress $cdbAddress */ |
110
|
|
|
$cdbAddress = $addresses[0]; |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
// Convert the cdbxml address to a udb3 address. |
114
|
|
|
$address = $this->addressFactory->fromCdbAddress($cdbAddress); |
115
|
|
|
} catch (\InvalidArgumentException $e) { |
116
|
|
|
// If conversion failed, log an error and do nothing. |
117
|
|
|
$this->logger->error( |
118
|
|
|
'Could not convert a cdbxml address to a udb3 address for geocoding.', |
119
|
|
|
[ |
120
|
|
|
'placeId' => $actorImportedFromUDB2->getActorId(), |
121
|
|
|
'error' => $e->getMessage(), |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// We don't know if the address has actually been updated because |
128
|
|
|
// ActorImportedFromUDB2 is too coarse, but if we use the cached |
129
|
|
|
// geocoding service we won't be wasting much resources when using |
130
|
|
|
// a naive approach like this. |
131
|
|
|
$command = new UpdateGeoCoordinatesFromAddress( |
132
|
|
|
$actorImportedFromUDB2->getActorId(), |
133
|
|
|
$address |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->commandBus->dispatch($command); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|