src/Subscriber/Cinema/CinemaSubscriber.php line 92

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber\Cinema;
  4. use App\Action\Query\Cinema\GetCinemaDetailList\GetCinemaDetailListQuery;
  5. use App\Event\Cinema\CinemaContextOnDemandEvent;
  6. use App\Event\Cinema\CinemaWsdlLocationChangedEvent;
  7. use App\Exception\Cinema\CinemaNotFoundException;
  8. use App\Model\Cinema;
  9. use App\Event\Cinema\CinemaContextChangedEvent;
  10. use App\Service\Context\ContextInterface;
  11. use App\Service\Factory\Client\CinemaClientFactory;
  12. use App\Ws\Cinema\CinemaClient;
  13. use App\WsProxy\WsProxy;
  14. use Ramsey\Uuid\Uuid;
  15. use Ramsey\Uuid\UuidInterface;
  16. use ReflectionClass;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Messenger\MessageBusInterface;
  19. use Symfony\Component\Messenger\Stamp\HandledStamp;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. class CinemaSubscriber implements EventSubscriberInterface
  22. {
  23.     private WsProxy $wsProxy;
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     private UuidInterface $posTypeId;
  26.     private ContextInterface $context;
  27.     private CinemaClient $cinemaClient;
  28.     private MessageBusInterface $messageBus;
  29.     private CinemaClientFactory $factory;
  30.     public function __construct(
  31.         WsProxy                  $wsProxy,
  32.         EventDispatcherInterface $eventDispatcher,
  33.         string                   $posTypeId,
  34.         ContextInterface         $context,
  35.         CinemaClient             $cinemaClient,
  36.         MessageBusInterface      $messageBus,
  37.         CinemaClientFactory      $factory,
  38.     )
  39.     {
  40.         $this->wsProxy $wsProxy;
  41.         $this->eventDispatcher $eventDispatcher;
  42.         $this->posTypeId Uuid::fromString($posTypeId);
  43.         $this->context $context;
  44.         $this->cinemaClient $cinemaClient;
  45.         $this->messageBus $messageBus;
  46.         $this->factory $factory;
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             CinemaContextOnDemandEvent::class => 'onDemand',
  52.             CinemaWsdlLocationChangedEvent::class => 'onLocationChanged'
  53.         ];
  54.     }
  55.     public function onDemand(CinemaContextOnDemandEvent $event): void
  56.     {
  57.         $cinema $this->messageBus->dispatch(new GetCinemaDetailListQuery())->last(HandledStamp::class)->getResult()->filter(
  58.             fn(Cinema $cinema) => strtolower((string)$cinema->getId()) === strtolower($event->getCinemaId())
  59.         )->first();
  60.         if ($cinema !== false) {
  61.             if ($cinema->getEndPointUrl() !== null) {
  62.                 $this->eventDispatcher->dispatch(new CinemaWsdlLocationChangedEvent($cinema->getEndPointUrl()));
  63.             }
  64.             $this->wsProxy->setCinemaUrl($cinema->getEndPointUrl());
  65.             $this->wsProxy->setPosTypeId($this->posTypeId);
  66.             $this->context->setCinemaId($cinema->getId());
  67.             $this->context->setPOSTypeId($this->posTypeId);
  68.             $this->eventDispatcher->dispatch(new CinemaContextChangedEvent(
  69.                 Uuid::fromString($event->getCinemaId()),
  70.                 $cinema->getEndPointUrl()
  71.             ));
  72.         } else {
  73.             throw new CinemaNotFoundException();
  74.         }
  75.     }
  76.     /**
  77.      * This method replace SOAP cinema client by client with new location (location get from Cinema object)
  78.      * It is solution instead of changeSoapLocation in old version of phpro/soap-client library
  79.      */
  80.     public function onLocationChanged(CinemaWsdlLocationChangedEvent $event): void
  81.     {
  82.         /** @var CinemaClient $client */
  83.         $client $this->factory->createClient($event->getLocation());
  84.         $reflectionClass = new ReflectionClass(CinemaClient::class);
  85.         $callerProperty $reflectionClass->getProperty('caller');
  86.         $callerProperty->setAccessible(true);
  87.         $callerProperty->setValue($this->cinemaClient$callerProperty->getValue($client));
  88.     }
  89. }