| @@ 11-79 (lines=69) @@ | ||
| 8 | use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler; |
|
| 9 | use CultuurNet\UDB3\Offer\Commands\AbstractUpdateGeoCoordinatesFromAddress; |
|
| 10 | ||
| 11 | abstract class AbstractGeoCoordinatesCommandHandler extends Udb3CommandHandler |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * @var RepositoryInterface |
|
| 15 | */ |
|
| 16 | private $offerRepository; |
|
| 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->offerRepository = $placeRepository; |
|
| 46 | $this->defaultAddressFormatter = $defaultAddressFormatter; |
|
| 47 | $this->fallbackAddressFormatter = $fallbackAddressFormatter; |
|
| 48 | $this->geocodingService = $geocodingService; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * @param AbstractUpdateGeoCoordinatesFromAddress $updateGeoCoordinates |
|
| 53 | */ |
|
| 54 | protected function updateGeoCoordinatesFromAddress(AbstractUpdateGeoCoordinatesFromAddress $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 | if ($coordinates === null) { |
|
| 71 | return; |
|
| 72 | } |
|
| 73 | ||
| 74 | /** @var Offer $offer */ |
|
| 75 | $offer = $this->offerRepository->load($updateGeoCoordinates->getItemId()); |
|
| 76 | $offer->updateGeoCoordinates($coordinates); |
|
| 77 | $this->offerRepository->save($offer); |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| @@ 13-79 (lines=67) @@ | ||
| 10 | use CultuurNet\UDB3\Organizer\Organizer; |
|
| 11 | use CultuurNet\UDB3\Organizer\OrganizerRepository; |
|
| 12 | ||
| 13 | class UpdateGeoCoordinatesCommandHandler extends Udb3CommandHandler |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var OrganizerRepository |
|
| 17 | */ |
|
| 18 | private $organizerRepository; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @var AddressFormatterInterface |
|
| 22 | */ |
|
| 23 | private $defaultAddressFormatter; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @var AddressFormatterInterface |
|
| 27 | */ |
|
| 28 | private $fallbackAddressFormatter; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @var GeocodingServiceInterface |
|
| 32 | */ |
|
| 33 | private $geocodingService; |
|
| 34 | ||
| 35 | ||
| 36 | public function __construct( |
|
| 37 | RepositoryInterface $placeRepository, |
|
| 38 | AddressFormatterInterface $defaultAddressFormatter, |
|
| 39 | AddressFormatterInterface $fallbackAddressFormatter, |
|
| 40 | GeocodingServiceInterface $geocodingService |
|
| 41 | ) { |
|
| 42 | $this->organizerRepository = $placeRepository; |
|
| 43 | $this->defaultAddressFormatter = $defaultAddressFormatter; |
|
| 44 | $this->fallbackAddressFormatter = $fallbackAddressFormatter; |
|
| 45 | $this->geocodingService = $geocodingService; |
|
| 46 | } |
|
| 47 | ||
| 48 | ||
| 49 | protected function handleUpdateGeoCoordinates(UpdateGeoCoordinates $updateGeoCoordinates) |
|
| 50 | { |
|
| 51 | $coordinates = $this->geocodingService->getCoordinates( |
|
| 52 | $this->defaultAddressFormatter->format( |
|
| 53 | $updateGeoCoordinates->address() |
|
| 54 | ) |
|
| 55 | ); |
|
| 56 | ||
| 57 | if ($coordinates === null) { |
|
| 58 | $coordinates = $this->geocodingService->getCoordinates( |
|
| 59 | $this->fallbackAddressFormatter->format( |
|
| 60 | $updateGeoCoordinates->address() |
|
| 61 | ) |
|
| 62 | ); |
|
| 63 | } |
|
| 64 | ||
| 65 | if ($coordinates === null) { |
|
| 66 | return; |
|
| 67 | } |
|
| 68 | ||
| 69 | /** @var Organizer $organizer */ |
|
| 70 | $organizer = $this->loadOrganizer($updateGeoCoordinates->organizerId()); |
|
| 71 | $organizer->updateGeoCoordinates($coordinates); |
|
| 72 | $this->organizerRepository->save($organizer); |
|
| 73 | } |
|
| 74 | ||
| 75 | protected function loadOrganizer(string $id) |
|
| 76 | { |
|
| 77 | return $this->organizerRepository->load($id); |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||