SubdomainProcessor.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Processor\Organization;
  4. use ApiPlatform\Metadata\Delete;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\Metadata\Post;
  7. use ApiPlatform\Metadata\Put;
  8. use ApiPlatform\State\ProcessorInterface;
  9. use App\Entity\Access\Access;
  10. use App\Entity\Organization\Subdomain;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use App\Repository\Organization\SubdomainRepository;
  13. use App\Service\Typo3\SubdomainService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Bundle\SecurityBundle\Security;
  16. /**
  17. * Custom Processor gérant la resource Subdomain
  18. */
  19. class SubdomainProcessor implements ProcessorInterface
  20. {
  21. public function __construct(
  22. private readonly SubdomainService $subdomainService,
  23. private Security $security
  24. ) {}
  25. /**
  26. * Persiste l'entité et déclenche les différents hooks de la classe OnChangeInterface définie par le data persister
  27. *
  28. * @param Subdomain $data
  29. * @param Operation $operation
  30. * @param array<mixed> $uriVariables
  31. * @param array<mixed> $context
  32. * @return object
  33. */
  34. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) {
  35. if($operation instanceof Delete){
  36. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  37. }
  38. /** @var Access $access */
  39. $access = $this->security->getUser();
  40. if ($data->getOrganization()->getId() !== $access->getOrganization()->getId()) {
  41. // TODO: voir à déplacer dans un voter?
  42. throw new \RuntimeException('forbidden', Response::HTTP_FORBIDDEN);
  43. }
  44. if ($operation instanceof Post) {
  45. // Create a new subdomain
  46. $subdomain = $this->subdomainService->addNewSubdomain(
  47. $data->getOrganization(),
  48. $data->getSubdomain(),
  49. $data->isActive()
  50. );
  51. } else if ($operation instanceof Put && $data->isActive()) {
  52. // Activate a subdomain
  53. $data->setActive(false); // On triche : c'est le service qui va activer ce sous-domaine, pas le processor
  54. $subdomain = $this->subdomainService->activateSubdomain($data);
  55. } else {
  56. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  57. }
  58. return $subdomain;
  59. }
  60. }