<?php
declare(strict_types=1);
namespace App\Base\EventSubscriber;
use App\Base\Service\AbstractMutationService;
use Doctrine\Common\Collections\ArrayCollection;
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent;
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MutationSubscriber implements EventSubscriberInterface
{
/**
* @var ArrayCollection|null
*/
private ArrayCollection|null $serviceContainer;
public function __construct() {
$this->serviceContainer = new ArrayCollection();
}
/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
MutationEvents::PRE_BUILD => 'onPreBuild'
];
}
/**
* @param MutationTypeEvent $event
* @return void
* @throws \Exception
*/
public function onPreBuild(MutationTypeEvent $event)
{
$config = $event->getConfig();
foreach ($this->serviceContainer->getValues() as $service) {
/** @var AbstractMutationService $service */
$class = new \ReflectionClass($service);
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$name = $method->getName();
if (str_starts_with($name, 'mutation')) {
$mutationName = lcfirst(substr($name, 8));
$config['fields'][$mutationName] = $service->getByName($mutationName);
}
}
}
$event->setConfig($config);
}
}