| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- declare(strict_types=1);
- namespace App\State\Processor\HelloAsso;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\Metadata\Post;
- use ApiPlatform\State\ProcessorInterface;
- use App\ApiResources\HelloAsso\ConnectionRequest;
- use App\Entity\Access\Access;
- use App\Service\HelloAsso\ConnectionService;
- use http\Client\Response;
- use Symfony\Bundle\SecurityBundle\Security;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- class ConnectionRequestProcessor implements ProcessorInterface
- {
- public function __construct(
- private readonly ConnectionService $connectionService,
- private Security $security,
- ) {
- }
- /**
- * @param ConnectionRequest $data
- * @param mixed[] $uriVariables
- * @param mixed[] $context
- *
- * @throws \Exception
- */
- public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): RedirectResponse
- {
- /**
- * @var ConnectionRequest $connectionRequest
- */
- $connectionRequest = $data;
- if (!$operation instanceof Post) {
- throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- /** @var Access $access */
- $access = $this->security->getUser();
- if ($connectionRequest->getOrganizationId() !== $access->getOrganization()->getId()) {
- throw new \RuntimeException('Forbidden');
- }
- $helloAssoEntity = $this->connectionService->connect(
- $connectionRequest->getOrganizationId(),
- $connectionRequest->getAuthorizationCode()
- );
- return $helloAssoEntity;
- }
- }
|