src/Atd/Supplier/Bokun/Domain/EventListeners/PickupPlacesRequestListener.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Atd\Supplier\Bokun\Domain\EventListeners;
  3. use App\Atd\Domain\Config\ConfigDto;
  4. use App\Atd\Domain\RequestDto;
  5. use App\Atd\Supplier\Bokun\Auth\Domain\AuthCommand;
  6. use App\Atd\Supplier\Bokun\Domain\CommandRunnerContext;
  7. use App\Atd\Supplier\Bokun\Domain\Events\EventTypes;
  8. use App\Atd\Supplier\Bokun\Domain\Events\PickupPlacesRequestEvent;
  9. use App\Atd\Supplier\Bokun\Domain\Location;
  10. use App\Atd\Supplier\Bokun\KeyGenerator\Service\KeyGeneratorService;
  11. use App\Atd\Supplier\Bokun\PickupPlaces\Domain\PickupPlacesCommand;
  12. use App\Atd\Supplier\Bokun\PickupPlaces\Domain\PickupPlacesCommandFactory;
  13. use App\EventListener\SupplierEventSubscriber;
  14. use App\Service\AuxDataStorageService;
  15. use App\Service\ConfigDtoService;
  16. use App\Service\Connections\RedisConnection;
  17. use Psr\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  19. class PickupPlacesRequestListener extends SupplierEventSubscriber
  20. {
  21.     /** Odd that even though this is domain logic we still need to identify the domain */
  22.     public const PROVIDER_CODE 'bkn';
  23.     public function __construct(
  24.         private readonly CommandRunnerContext $context,
  25.         private readonly AuthCommand $authCommand,
  26.         private readonly PickupPlacesCommandFactory $pickupPlacesCommandFactory,
  27.         private readonly ConfigDtoService $configDtoService,
  28.         private readonly AuxDataStorageService $auxDataStorageService,
  29.         private readonly ParameterBagInterface $parameterBag,
  30.         private readonly KeyGeneratorService $keyGeneratorService,
  31.         protected EventDispatcherInterface $dispatcher
  32.     ) {
  33.         parent::__construct($dispatcher);
  34.     }
  35.     /**
  36.      * @return string[]
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             EventTypes::PICKUP_PLACES_REQUEST => 'handleEvent',
  42.         ];
  43.     }
  44.     /**
  45.      * @throws \Exception
  46.      */
  47.     public function handleEvent(PickupPlacesRequestEvent $event): void
  48.     {
  49.         try {
  50.             $payload = [
  51.                 'activityId' => $event->getActivityId(),
  52.             ];
  53.             $this->setEndpointRequestDto($payload);
  54.             $this->authCommand->execute($this->contextPickupPlacesCommand::CMD_NAME$payload);
  55.             $pickupPlacesCommand $this->pickupPlacesCommandFactory->create();
  56.             $pickupPlacesCommand->execute($this->context);
  57.             $placesResponse $this->context->getPickupPlacesResponseDao();
  58.             $this->cacheLocations($event->getActivityId(), $placesResponse->getLocations());
  59.             $this->sendInterruptResponse($placesResponse->toArray());
  60.         } catch (\Throwable $exception) {
  61.             throw new \Exception($exception->getMessage());
  62.         }
  63.     }
  64.     private function getConfigDto(): ConfigDto
  65.     {
  66.         return $this->configDtoService->configDtoFactory(self::PROVIDER_CODE);
  67.     }
  68.     /**
  69.      * @throws \Exception
  70.      */
  71.     private function setEndpointRequestDto(array $requestData = []): void
  72.     {
  73.         $this->context->setRequestDto(
  74.             RequestDto::factory([
  75.                 'method' => 'PUT',
  76.                 'query' => null,
  77.                 'data' => $requestData,
  78.                 'provider' => self::PROVIDER_CODE,
  79.                 'nocache' => true,
  80.                 'configDto' => $this->getConfigDto(),
  81.                 'updateDatastore' => false,
  82.             ])
  83.         );
  84.     }
  85.     private function getConnectedAuxCacheStoreService(): AuxDataStorageService
  86.     {
  87.         $conn = new RedisConnection(
  88.             $this->parameterBag->get('app.aux.redis.ip'),
  89.             $this->parameterBag->get('app.aux.redis.port')
  90.         );
  91.         return $this->auxDataStorageService->connect($conn);
  92.     }
  93.     /**
  94.      * @param array|Location[] $locations
  95.      *
  96.      * @throws \Exception
  97.      */
  98.     private function cacheLocations(string $activityId, array $locations): void
  99.     {
  100.         $key $this->keyGeneratorService->generatePickupLocationsKey(
  101.             self::PROVIDER_CODE,
  102.             $activityId
  103.         );
  104.         $this->getConnectedAuxCacheStoreService()
  105.              ->store(
  106.                  $key,
  107.                  $locations
  108.              );
  109.     }
  110. }