ConnectionRequestProcessor.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Processor\HelloAsso;
  4. use ApiPlatform\Metadata\Operation;
  5. use ApiPlatform\Metadata\Post;
  6. use ApiPlatform\State\ProcessorInterface;
  7. use App\ApiResources\HelloAsso\ConnectionRequest;
  8. use App\Entity\Access\Access;
  9. use App\Service\HelloAsso\ConnectionService;
  10. use http\Client\Response;
  11. use Symfony\Bundle\SecurityBundle\Security;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. class ConnectionRequestProcessor implements ProcessorInterface
  14. {
  15. public function __construct(
  16. private readonly ConnectionService $connectionService,
  17. private Security $security,
  18. ) {
  19. }
  20. /**
  21. * @param ConnectionRequest $data
  22. * @param mixed[] $uriVariables
  23. * @param mixed[] $context
  24. *
  25. * @throws \Exception
  26. */
  27. public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): RedirectResponse
  28. {
  29. /**
  30. * @var ConnectionRequest $connectionRequest
  31. */
  32. $connectionRequest = $data;
  33. if (!$operation instanceof Post) {
  34. throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
  35. }
  36. /** @var Access $access */
  37. $access = $this->security->getUser();
  38. if ($connectionRequest->getOrganizationId() !== $access->getOrganization()->getId()) {
  39. throw new \RuntimeException('Forbidden');
  40. }
  41. $helloAssoEntity = $this->connectionService->connect(
  42. $connectionRequest->getOrganizationId(),
  43. $connectionRequest->getAuthorizationCode()
  44. );
  45. return $helloAssoEntity;
  46. }
  47. }