src/Adapter/AuthAdapter.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Adapter;
  3. use Exception;
  4. use Throwable;
  5. use App\Adapter\Request\DeleteRequest;
  6. use App\Adapter\Request\GetRequest;
  7. use App\Adapter\Request\PostRequest;
  8. use App\Dto\Input\AuthDTO;
  9. use App\Dto\Input\MediaDTO;
  10. use App\Dto\Input\RegisterDTO;
  11. use App\Dto\Input\ResetPasswordDTO;
  12. use App\Dto\Output\AuthOutputDTO;
  13. use App\Exception\ClientFromMediaNotFoundException;
  14. use App\Exception\ExpiredCodeException;
  15. use App\Mapper\ClientMapper;
  16. use App\Model\Client;
  17. use Psr\Log\LoggerInterface;
  18. use Symfony\Component\HttpClient\Exception\ClientException;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  21. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  22. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  23. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  24. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  25. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  27. use Symfony\Contracts\HttpClient\HttpClientInterface;
  28. class AuthAdapter extends AbstractAdapter
  29. {
  30.     public function __construct(
  31.         HttpClientInterface $carfitClient,
  32.         LoggerInterface $securityLogger,
  33.         array $carfitEndpoint
  34.     ) {
  35.         parent::__construct($carfitClient$securityLogger$carfitEndpoint);
  36.     }
  37.     /**
  38.      * @throws Exception|CustomUserMessageAuthenticationException
  39.      */
  40.     public function login(AuthDTO $dto): Client
  41.     {
  42.         $request = new PostRequest($this->carfitEndpoint['login']);
  43.         $request->setBody([
  44.             'email' => $dto->email,
  45.             'password' => $dto->password,
  46.         ]);
  47.         try {
  48.             $content $this->call($request);
  49.             $outputDto = new AuthOutputDTO();
  50.             $outputDto->token $content['token'];
  51.             $outputDto->email $dto->email;
  52.             $outputDto->password $dto->password;
  53.             return ClientMapper::fromAuthDTO($outputDto);
  54.         } catch (ClientException $exception) {
  55.             $this->logger->error($exception->getMessage());
  56.             throw new CustomUserMessageAuthenticationException('auth.not_found');
  57.         } catch (Throwable $exception) {
  58.             $this->logger->error($exception->getMessage());
  59.             throw new CustomUserMessageAuthenticationException('error.500');
  60.         }
  61.     }
  62.     /**
  63.      * @throws Exception
  64.      */
  65.     public function passwordRecover(ResetPasswordDTO $dto): bool
  66.     {
  67.         $request = new PostRequest($this->carfitEndpoint['password_recover']);
  68.         $request->setBody([
  69.             'email' => $dto->email,
  70.         ]);
  71.         return $this->sendRequest($request);
  72.     }
  73.     /**
  74.      * @throws Exception
  75.      */
  76.     public function passwordVerify(ResetPasswordDTO $dto): bool
  77.     {
  78.         $request = new GetRequest(str_replace('{hash}'$dto->hash$this->carfitEndpoint['password_hash']));
  79.         return $this->sendRequest($request);
  80.     }
  81.     /**
  82.      * @throws Exception
  83.      */
  84.     public function resetPassword(ResetPasswordDTO $dto): bool
  85.     {
  86.         $request = new PostRequest(str_replace('{hash}'$dto->hash$this->carfitEndpoint['password_reset']));
  87.         $request->setBody([
  88.             'email' => $dto->email,
  89.             'password' => $dto->password,
  90.         ]);
  91.         return $this->sendRequest($request);
  92.     }
  93.     /**
  94.      * @throws Exception
  95.      */
  96.     public function register(RegisterDTO $dto): bool
  97.     {
  98.         $request = new PostRequest($this->carfitEndpoint['register']);
  99.         $request->setBody([
  100.             'email' => $dto->email,
  101.             'password' => $dto->password,
  102.         ]);
  103.         return $this->sendRegisterRequest($request);
  104.     }
  105.     /**
  106.      * @throws Exception
  107.      */
  108.     public function registerCheck(RegisterDTO $dto): Client
  109.     {
  110.         $request = new PostRequest($this->carfitEndpoint['register_confirm']);
  111.         $request->setBody([
  112.             'email' => $dto->email,
  113.             'code' => $dto->code,
  114.         ]);
  115.         $outputDto = new AuthOutputDTO();
  116.         try {
  117.             $content $this->call($request);
  118.             $outputDto->token $content['token'];
  119.         } catch (ClientException $exception) {
  120.             if (Response::HTTP_BAD_REQUEST == $exception->getCode()) {
  121.                 throw new ExpiredCodeException('register.code.expired');
  122.             } else {
  123.                 throw new BadRequestHttpException($exception->getMessage());
  124.             }
  125.         } catch (Throwable $exception) {
  126.             $this->logger->error($exception->getMessage());
  127.             throw new Exception('Internal Server Error! A error occurred while working on the request.');
  128.         }
  129.         /* @todo remove it */
  130.         return ClientMapper::fromAuthDTO($outputDto);
  131.     }
  132.     /**
  133.      * @throws Exception
  134.      */
  135.     public function registerResend(RegisterDTO $dto): bool
  136.     {
  137.         $request = new PostRequest($this->carfitEndpoint['register_resend']);
  138.         $request->setBody([
  139.             'email' => $dto->email,
  140.         ]);
  141.         return $this->sendRequest($request);
  142.     }
  143.     /**
  144.      * @throws TransportExceptionInterface
  145.      * @throws Throwable
  146.      * @throws ServerExceptionInterface
  147.      * @throws RedirectionExceptionInterface
  148.      * @throws DecodingExceptionInterface
  149.      * @throws ClientExceptionInterface
  150.      */
  151.     public function logout(string $token): bool
  152.     {
  153.         $request = new DeleteRequest($this->carfitEndpoint['logout']);
  154.         $request->setAuthorization($token);
  155.         return $this->sendRequest($request);
  156.     }
  157.     /**
  158.      * @throws ClientFromMediaNotFoundException
  159.      */
  160.     public function userMediaLogin(MediaDTO $dto): ?Client
  161.     {
  162.         $request = new PostRequest(str_replace('{provider}'$dto->provider$this->carfitEndpoint['login_media']));
  163.         $request->setBody(['token' => $dto->token]);
  164.         try {
  165.             $content $this->call($request);
  166.             $outputDto = new AuthOutputDTO();
  167.             $outputDto->token $content['token'];
  168.             return ClientMapper::fromAuthDTO($outputDto);
  169.         } catch (ClientException $exception) {
  170.             throw new ClientFromMediaNotFoundException('auth.not_found');
  171.         } catch (Throwable $exception) {
  172.             $this->logger->error($exception->getMessage());
  173.             throw new CustomUserMessageAuthenticationException('error.500');
  174.         }
  175.     }
  176.     public function userMediaRegister(MediaDTO $dto): bool
  177.     {
  178.         $request = new PostRequest($this->carfitEndpoint['register_media']);
  179.         $request->setBody([
  180.             'token' => $dto->token
  181.         ]);
  182.         return $this->sendRegisterRequest($request);
  183.     }
  184.     /**
  185.      * @throws Exception
  186.      */
  187.     private function sendRegisterRequest(PostRequest $request): bool
  188.     {
  189.         try {
  190.             return $this->sendRequest($request);
  191.         } catch (ClientException $exception) {
  192.             $this->logger->error($exception->getMessage());
  193.             if (Response::HTTP_CONFLICT == $exception->getCode()) {
  194.                 throw new BadRequestHttpException('email.already_taken');
  195.             } else {
  196.                 throw new BadRequestHttpException($exception->getMessage());
  197.             }
  198.         } catch (Throwable $exception) {
  199.             $this->logger->error($exception->getMessage());
  200.             throw new Exception('Internal Server Error! A error occurred while working on the request.');
  201.         }
  202.     }
  203. }