src/Form/EventListener/Admin/Webhook/AddEventsFieldSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Admin\Webhook;
  4. use App\Entity\Admin\Webhook\WebhookInterface;
  5. use App\Form\FormHelper;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. class AddEventsFieldSubscriber extends FormHelper implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  15.     }
  16.     public function preSetData(FormEvent $event): void
  17.     {
  18.         $choices = [];
  19.         $webhook $event->getData();
  20.         $events null !== $webhook->getId() ? $this->getEventStates($webhook->getObject()) : array_merge(WebhookInterface::ORDER_EVENTSWebhookInterface::SYNC_EVENTS);
  21.         foreach ($events as $state) {
  22.             $choices['app.admin.pages.webhook.states.' $state] = $state;
  23.         }
  24.         $event->getForm()->add('events'ChoiceType::class, $this->getChoiceParameters(
  25.             'app.admin.pages.webhook.list.events',
  26.             self::PLACEHOLDER_NONE,
  27.             true,
  28.             [
  29.                 'choices' => $choices,
  30.                 'data' => \count($webhook->getEvents()) > $webhook->getEvents() : [],
  31.                 'multiple' => true,
  32.                 'expanded' => false,
  33.                 'choice_attr' => static fn ($choice$key$value) => true === \in_array($choiceWebhookInterface::ORDER_EVENTStrue) ? ['data-event-type' => 'order'] : ['data-event-type' => 'sync'],
  34.             ]
  35.         ));
  36.     }
  37.     private function getEventStates(string $object): array
  38.     {
  39.         return WebhookInterface::OBJECT_ORDER === $object WebhookInterface::ORDER_EVENTS WebhookInterface::SYNC_EVENTS;
  40.     }
  41. }