src/Base/EventSubscriber/MutationSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Base\EventSubscriber;
  4. use App\Base\Service\AbstractMutationService;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent;
  7. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class MutationSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var ArrayCollection|null
  13.      */
  14.     private ArrayCollection|null $serviceContainer;
  15.     public function __construct() {
  16.         $this->serviceContainer = new ArrayCollection();
  17.     }
  18.     /**
  19.      * @return string[]
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             MutationEvents::PRE_BUILD => 'onPreBuild'
  25.         ];
  26.     }
  27.     /**
  28.      * @param MutationTypeEvent $event
  29.      * @return void
  30.      * @throws \Exception
  31.      */
  32.     public function onPreBuild(MutationTypeEvent $event)
  33.     {
  34.         $config $event->getConfig();
  35.         foreach ($this->serviceContainer->getValues() as $service) {
  36.             /** @var AbstractMutationService $service */
  37.             $class = new \ReflectionClass($service);
  38.             foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  39.                 $name $method->getName();
  40.                 if (str_starts_with($name'mutation')) {
  41.                     $mutationName lcfirst(substr($name8));
  42.                     $config['fields'][$mutationName] = $service->getByName($mutationName);
  43.                 }
  44.             }
  45.         }
  46.         $event->setConfig($config);
  47.     }
  48. }