SubdomainProcessor.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Organization\Subdomain;
  10. use App\Repository\Organization\SubdomainRepository;
  11. use App\Service\Typo3\SubdomainService;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. /**
  14. * Custom Processor gérant la resource Subdomain
  15. */
  16. class SubdomainProcessor implements ProcessorInterface
  17. {
  18. public function __construct(
  19. private readonly SubdomainService $subdomainService,
  20. private EntityManagerInterface $entityManager,
  21. ) {}
  22. /**
  23. * Persiste l'entité et déclenche les différents hooks de la classe OnChangeInterface définie par le data persister
  24. *
  25. * @param Subdomain $data
  26. * @param Operation $operation
  27. * @param array $uriVariables
  28. * @param array $context
  29. * @return object
  30. */
  31. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) {
  32. if($operation instanceof Delete){
  33. throw new \RuntimeException('not supported', 500);
  34. }
  35. if ($operation instanceof Post) {
  36. // Create a new subdomain
  37. $subdomain = $this->subdomainService->addNewSubdomain(
  38. $data->getOrganization(),
  39. $data->getSubdomain(),
  40. $data->isActive()
  41. );
  42. } else if ($operation instanceof Put && $data->isActive()) {
  43. // Activate a subdomain
  44. $data->setActive(false); // On triche : c'est le service qui va activer ce sous-domaine, pas le processor
  45. $subdomain = $this->subdomainService->activateSubdomain($data);
  46. } else {
  47. throw new \RuntimeException('not supported', 500);
  48. }
  49. return $subdomain;
  50. }
  51. }