OrganizationDeletionRequestProcessor.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\State\ProcessorInterface;
  7. use App\ApiResources\Organization\OrganizationCreationRequest;
  8. use App\ApiResources\Organization\OrganizationDeletionRequest;
  9. use App\Message\Command\OrganizationCreationCommand;
  10. use App\Service\Organization\OrganizationFactory;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. class OrganizationDeletionRequestProcessor implements ProcessorInterface
  14. {
  15. public function __construct(
  16. private readonly MessageBusInterface $messageBus,
  17. private readonly OrganizationFactory $organizationFactory,
  18. ) {}
  19. /**
  20. * @param OrganizationDeletionRequest $organizationDeletionRequest
  21. * @param mixed[] $uriVariables
  22. * @param mixed[] $context
  23. *
  24. * @throws \Exception
  25. */
  26. public function process(mixed $organizationDeletionRequest, Operation $operation, array $uriVariables = [], array $context = []): OrganizationCreationRequest
  27. {
  28. if (!$operation instanceof Delete) {
  29. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  30. }
  31. if ($organizationDeletionRequest->isAsync()) {
  32. // Send the export request to Messenger (@see App\Message\Handler\OrganizationCreationHandler)
  33. $this->messageBus->dispatch(
  34. new OrganizationDeletionCommand($organizationDeletionRequest)
  35. );
  36. } else {
  37. // For testing purposes only
  38. $this->organizationFactory->delete($organizationDeletionRequest);
  39. }
  40. return $organizationDeletionRequest;
  41. }
  42. }