src/Mobile/Doctor/CheckEmailAction.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Mobile\Doctor;
  4. use App\Entity\User\ShopUser;
  5. use Sylius\Component\Resource\Repository\RepositoryInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Validator\Validation;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use function mb_strtolower;
  13. use function trim;
  14. final readonly class CheckEmailAction
  15. {
  16.     public function __construct(
  17.         private RepositoryInterface $shopUserRepository,
  18.         private TranslatorInterface $translator,
  19.     ) {
  20.     }
  21.     public function __invoke(Request $request): JsonResponse
  22.     {
  23.         $data $request->toArray();
  24.         $email trim((string) ($data['email'] ?? ''));
  25.         $violations Validation::createValidator()->validate($email, [
  26.             new Assert\NotBlank(message$this->translator->trans('validator.error.field_required', [], 'validators')),
  27.             new Assert\Email(message$this->translator->trans('validator.error.email_not_valid', [], 'validators')),
  28.         ]);
  29.         if ($violations->count() > 0) {
  30.             return new JsonResponse([
  31.                 'status' => 'error',
  32.                 'code' => 'invalid_email',
  33.                 'message' => $violations->get(0)->getMessage(),
  34.             ], Response::HTTP_UNPROCESSABLE_ENTITY);
  35.         }
  36.         /** @var ShopUser|null $shopUser */
  37.         $shopUser $this->shopUserRepository->findOneBy(['usernameCanonical' => mb_strtolower($email)]);
  38.         if ($shopUser === null) {
  39.             return new JsonResponse([
  40.                 'status' => 'error',
  41.                 'code' => 'account_not_found',
  42.                 'message' => $this->translator->trans('validator.error.account_not_found', [], 'validators'),
  43.             ], Response::HTTP_NOT_FOUND);
  44.         }
  45.         $customer $shopUser->getCustomer();
  46.         $doctor $customer?->getDoctor();
  47.         if ($doctor === null || ! $doctor->isAppAccessSeriderm360()) {
  48.             return new JsonResponse([
  49.                 'status' => 'error',
  50.                 'code' => 'access_denied',
  51.                 'message' => $this->translator->trans('validator.error.access_denied', [], 'validators'),
  52.             ], Response::HTTP_FORBIDDEN);
  53.         }
  54.         return new JsonResponse([
  55.             'status' => 'success',
  56.             'data' => [
  57.                 'account_status' => $shopUser->getAccountStatus(),
  58.                 'app_access' => [
  59.                     'seriderm_360' => $doctor->isAppAccessSeriderm360(),
  60.                 ],
  61.             ],
  62.         ]);
  63.     }
  64. }