src/Services/OneTimeCodeService.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Services\{
  4.     EntityServices\OneTimeCodeEntityService,
  5.     EntityServices\UserEntityService
  6. };
  7. use App\Entity\{
  8.     OneTimeCode,
  9.     Tenant,
  10.     User
  11. };
  12. class OneTimeCodeService
  13. {
  14.     const MAX_PER_USER 10;
  15.     private $notifierService;
  16.     private $oneTimeCodeEntityService;
  17.     private $userEntityService;
  18.     public function __construct(
  19.         OneTimeCodeEntityService $oneTimeCodeEntityService,
  20.         UserEntityService $userEntityService
  21.     ) {
  22.         $this->oneTimeCodeEntityService $oneTimeCodeEntityService;
  23.         $this->userEntityService $userEntityService;
  24.     }
  25.     /**
  26.      * ask OneTimeCode
  27.      */
  28.     public function askCodeByEmail(Tenant $tenantstring $email): OneTimeCode
  29.     {
  30.         $user $this->userEntityService->findUser($email$tenant);
  31.         if ( is_null($user) ) {
  32.             throw new \Exception('User not found');
  33.         }
  34.         return $this->askCodeByUser($user);
  35.     }
  36.     /**
  37.      * ask OneTimeCode
  38.      */
  39.     public function askCodeByUser(User $user): OneTimeCode
  40.     {
  41.         if ( self::MAX_PER_USER <= $this->oneTimeCodeEntityService->cleanAndCount($user) ) {
  42.             throw new \Exception('Max per User exceeded');
  43.         }
  44.         return $this->oneTimeCodeEntityService->createCode($user);
  45.     }
  46.     /**
  47.      * use OneTimeCode
  48.      */
  49.     public function useCode(Tenant $tenantstring $code): User
  50.     {
  51.         $oneTimeCode $this->oneTimeCodeEntityService->findCode($code);
  52.         if ( is_null($oneTimeCode) ) {
  53.             throw new \Exception('Code not found');
  54.         }
  55.         $isExpired = (bool)((new \DateTimeImmutable())->diff($oneTimeCode->getExpiredAt()))->invert;
  56.         $user $oneTimeCode->getUser();
  57.         $isTenantMatch $tenant === $user->getTenant();
  58.         $this->oneTimeCodeEntityService->removeCode($oneTimeCode);
  59.         if ( $isExpired ) {
  60.             throw new \Exception('Code expired');
  61.         } else if ( ! $isTenantMatch ) {
  62.             throw new \Exception('URL failed');
  63.         }
  64.         $this->oneTimeCodeEntityService->removeByUser(
  65.             $this->userEntityService->setEmailConfirmed$user )
  66.         );
  67.         return $user;
  68.     }
  69.     /**
  70.      * find user by OneTimeCode
  71.      */
  72.     public function findUserByCode(Tenant $tenantstring $code): ?User
  73.     {
  74.         $oneTimeCode $this->oneTimeCodeEntityService->findCode($code);
  75.         if ( is_null($oneTimeCode) ) {
  76.             return null;
  77.         }
  78.         $isExpired = (bool)((new \DateTimeImmutable())->diff($oneTimeCode->getExpiredAt()))->invert;
  79.         $user $oneTimeCode->getUser();
  80.         $isTenantMatch $tenant === $user->getTenant();
  81.         if ( $isExpired or ! $isTenantMatch) {
  82. //            dd($isExpired, $isTenantMatch);
  83.             return null;
  84.         }
  85.         return $user;
  86.     }
  87. }