| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- declare(strict_types=1);
- namespace App\State\Processor\Organization;
- use ApiPlatform\Metadata\Delete;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\Metadata\Post;
- use ApiPlatform\Metadata\Put;
- use ApiPlatform\State\ProcessorInterface;
- use App\Entity\Access\Access;
- use App\Entity\Organization\Subdomain;
- use Symfony\Component\HttpFoundation\Response;
- use App\Repository\Organization\SubdomainRepository;
- use App\Service\Typo3\SubdomainService;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Bundle\SecurityBundle\Security;
- /**
- * Custom Processor gérant la resource Subdomain
- */
- class SubdomainProcessor implements ProcessorInterface
- {
- public function __construct(
- private readonly SubdomainService $subdomainService,
- private Security $security
- ) {}
- /**
- * Persiste l'entité et déclenche les différents hooks de la classe OnChangeInterface définie par le data persister
- *
- * @param Subdomain $data
- * @param Operation $operation
- * @param array<mixed> $uriVariables
- * @param array<mixed> $context
- * @return object
- */
- public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) {
- if($operation instanceof Delete){
- throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- /** @var Access $access */
- $access = $this->security->getUser();
- if ($data->getOrganization()->getId() !== $access->getOrganization()->getId()) {
- // TODO: voir à déplacer dans un voter?
- throw new \RuntimeException('forbidden', Response::HTTP_FORBIDDEN);
- }
- if ($operation instanceof Post) {
- // Create a new subdomain
- $subdomain = $this->subdomainService->addNewSubdomain(
- $data->getOrganization(),
- $data->getSubdomain(),
- $data->isActive()
- );
- } else if ($operation instanceof Put && $data->isActive()) {
- // Activate a subdomain
- $data->setActive(false); // On triche : c'est le service qui va activer ce sous-domaine, pas le processor
- $subdomain = $this->subdomainService->activateSubdomain($data);
- } else {
- throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- return $subdomain;
- }
- }
|