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

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