vendor/dachcom-digital/formbuilder/src/FormBuilderBundle/EventSubscriber/SignalSubscribeHandler.php line 46

Open in your IDE?
  1. <?php
  2. namespace FormBuilderBundle\EventSubscriber;
  3. use FormBuilderBundle\Configuration\Configuration;
  4. use FormBuilderBundle\Event\OutputWorkflow\OutputWorkflowSignalEvent;
  5. use FormBuilderBundle\Event\OutputWorkflow\OutputWorkflowSignalsEvent;
  6. use FormBuilderBundle\EventSubscriber\SignalStorage\ArraySignalStorage;
  7. use FormBuilderBundle\EventSubscriber\SignalStorage\ProviderAwareStorageInterface;
  8. use FormBuilderBundle\EventSubscriber\SignalStorage\SignalStorageInterface;
  9. use FormBuilderBundle\FormBuilderEvents;
  10. use FormBuilderBundle\Registry\StorageProviderRegistry;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class SignalSubscribeHandler implements EventSubscriberInterface
  14. {
  15.     public const CHANNEL_FUNNEL_INITIATE 'funnel.initiate';
  16.     public const CHANNEL_FUNNEL_PROCESS 'funnel.process';
  17.     public const CHANNEL_OUTPUT_WORKFLOW 'output_workflow';
  18.     protected Configuration $configuration;
  19.     protected StorageProviderRegistry $storageProviderRegistry;
  20.     protected EventDispatcherInterface $eventDispatcher;
  21.     protected SignalStorageInterface $signalStorage;
  22.     protected ?string $channel null;
  23.     public function __construct(
  24.         Configuration $configuration,
  25.         StorageProviderRegistry $storageProviderRegistry,
  26.         EventDispatcherInterface $eventDispatcher
  27.     ) {
  28.         $this->configuration $configuration;
  29.         $this->storageProviderRegistry $storageProviderRegistry;
  30.         $this->eventDispatcher $eventDispatcher;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             OutputWorkflowSignalEvent::NAME => 'addSignal',
  36.         ];
  37.     }
  38.     public function addSignal(OutputWorkflowSignalEvent $signalEvent): void
  39.     {
  40.         if ($this->channel === null) {
  41.             throw new \Exception('Cannot add signal, no channel has been defined.');
  42.         }
  43.         $this->signalStorage->storeSignal($signalEvent);
  44.     }
  45.     public function listen(string $channel, array $context = []): void
  46.     {
  47.         if ($this->channel !== null) {
  48.             return;
  49.         }
  50.         $this->channel $channel;
  51.         $this->signalStorage $this->buildSignalStorage($channel$context);
  52.         $this->eventDispatcher->addSubscriber($this);
  53.     }
  54.     public function broadcast(array $context = []): void
  55.     {
  56.         $channelName $this->channel;
  57.         if ($channelName === null) {
  58.             return;
  59.         }
  60.         $this->channel null;
  61.         $this->eventDispatcher->removeSubscriber($this);
  62.         $this->eventDispatcher->dispatch(
  63.             new OutputWorkflowSignalsEvent(
  64.                 $channelName,
  65.                 $this->signalStorage->getSignals(),
  66.                 $context
  67.             ),
  68.             FormBuilderEvents::OUTPUT_WORKFLOW_SIGNALS
  69.         );
  70.     }
  71.     private function buildSignalStorage(string $channel, array $context): SignalStorageInterface
  72.     {
  73.         if ($channel === self::CHANNEL_OUTPUT_WORKFLOW) {
  74.             return new ArraySignalStorage();
  75.         }
  76.         if ($channel === self::CHANNEL_FUNNEL_INITIATE) {
  77.             return new ArraySignalStorage();
  78.         }
  79.         if ($channel === self::CHANNEL_FUNNEL_PROCESS) {
  80.             $funnelConfiguration $this->configuration->getConfig('funnel');
  81.             $signalStorageClass $funnelConfiguration['signal_storage_class'];
  82.             $signalStorageProvider $funnelConfiguration['storage_provider'];
  83.             /** @var SignalStorageInterface $signalStorageClass */
  84.             $signalStorage = new $signalStorageClass($context);
  85.             if ($signalStorage instanceof ProviderAwareStorageInterface) {
  86.                 $signalStorage->setStorageProvider($this->storageProviderRegistry->get($signalStorageProvider));
  87.             }
  88.             return $signalStorage;
  89.         }
  90.         throw new \Exception(sprintf('Cannot determinate signal storage for channel "%s"'$channel));
  91.     }
  92. }