| @@ 11-75 (lines=65) @@ | ||
| 8 | use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler; |
|
| 9 | use CultuurNet\UDB3\Event\Commands\UpdateGeoCoordinatesFromAddress; |
|
| 10 | ||
| 11 | 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 | ||
| @@ 11-75 (lines=65) @@ | ||
| 8 | use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler; |
|
| 9 | use CultuurNet\UDB3\Place\Commands\UpdateGeoCoordinatesFromAddress; |
|
| 10 | ||
| 11 | class GeoCoordinatesCommandHandler extends Udb3CommandHandler |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var RepositoryInterface |
|
| 15 | */ |
|
| 16 | private $placeRepository; |
|
| 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 $placeRepository |
|
| 35 | * @param AddressFormatterInterface $defaultAddressFormatter |
|
| 36 | * @param AddressFormatterInterface $fallbackAddressFormatter |
|
| 37 | * @param GeocodingServiceInterface $geocodingService |
|
| 38 | */ |
|
| 39 | public function __construct( |
|
| 40 | RepositoryInterface $placeRepository, |
|
| 41 | AddressFormatterInterface $defaultAddressFormatter, |
|
| 42 | AddressFormatterInterface $fallbackAddressFormatter, |
|
| 43 | GeocodingServiceInterface $geocodingService |
|
| 44 | ) { |
|
| 45 | $this->placeRepository = $placeRepository; |
|
| 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 Place $place */ |
|
| 71 | $place = $this->placeRepository->load($updateGeoCoordinates->getItemId()); |
|
| 72 | $place->updateGeoCoordinates($coordinates); |
|
| 73 | $this->placeRepository->save($place); |
|
| 74 | } |
|
| 75 | } |
|
| 76 | ||