src/EventSubscriber/CompanyConfigurationSubscriber.php line 23
<?phpnamespace App\EventSubscriber;use ApiPlatform\Symfony\EventListener\EventPriorities;use ApiPlatform\Util\RequestAttributesExtractor;use App\Document\Company\CompanyConfiguration;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\Exception\ConflictHttpException;use Symfony\Component\HttpKernel\KernelEvents;class CompanyConfigurationSubscriber implements EventSubscriberInterface{public static function getSubscribedEvents(): array{return [KernelEvents::VIEW => ['checkConfiguration', EventPriorities::POST_VALIDATE],];}public function checkConfiguration(ViewEvent $event): void{$request = $event->getRequest();$attributes = RequestAttributesExtractor::extractAttributes($request);$resourceClass = $attributes['resource_class'] ?? null;$configuration = $event->getControllerResult();if (CompanyConfiguration::class !== $resourceClassor !$configuration instanceof CompanyConfigurationor !$request->isMethod(Request::METHOD_POST)) {return;}$company = $configuration->getCompany();if ($company->getConfiguration()?->getId()) {throw new ConflictHttpException('The company already has a configuration.');}}}