Advertisement
Guest User

SignInCustomerByGoogleAuthHandler

a guest
May 24th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Identity\Application\Customer\UseCase\SignInCustomerByGoogleAuth;
  6.  
  7. use App\Identity\Application\Customer\CustomerEntityManager;
  8. use App\Identity\Application\Customer\Query\CustomerByGoogleAuthQuery;
  9. use App\Identity\Application\Customer\UseCase\CreateCustomer\CreateCustomerCommand;
  10. use App\Identity\Application\Customer\UseCase\CreateCustomer\CreateCustomerHandler;
  11. use App\Identity\Application\Security\Security;
  12. use App\Identity\Application\User\UseCase\LinkGoogleAuth\GoogleAuth;
  13. use App\Identity\Domain\Customer\Customer;
  14.  
  15. final class SignInCustomerByGoogleAuthHandler
  16. {
  17.     private CustomerByGoogleAuthQuery $customerByGoogleAuthQuery;
  18.  
  19.     private GoogleAuth $googleAuth;
  20.  
  21.     private CreateCustomerHandler $createCustomerHandler;
  22.  
  23.     private CustomerEntityManager $customerEntityManager;
  24.  
  25.     private Security $security;
  26.  
  27.     public function __construct(CustomerByGoogleAuthQuery $customerByGoogleAuthQuery,
  28.                                 CreateCustomerHandler $createCustomerHandler,
  29.                                 CustomerEntityManager $customerEntityManager,
  30.                                 Security $security,
  31.                                 GoogleAuth $googleAuth)
  32.     {
  33.         $this->customerByGoogleAuthQuery = $customerByGoogleAuthQuery;
  34.         $this->googleAuth = $googleAuth;
  35.         $this->createCustomerHandler = $createCustomerHandler;
  36.         $this->customerEntityManager = $customerEntityManager;
  37.         $this->security = $security;
  38.     }
  39.  
  40.     public function handle(SignInCustomerByGoogleAuthCommand $command): Customer
  41.     {
  42.         $code = $command->code();
  43.  
  44.         $oauthUser = $this->googleAuth->userByOneTimeCode($code);
  45.         $customer = $this->customerByGoogleAuthQuery->queryByGoogleUser($oauthUser);
  46.         $username = $this->customerEntityManager->nextId()->value();
  47.         $password = $this->security->randomPassword();
  48.  
  49.         if (!$customer) {
  50.             $command = new CreateCustomerCommand(
  51.                 $oauthUser->email(),
  52.                 $username,
  53.                 $password->hash(),
  54.                 $oauthUser->firstname(),
  55.                 $oauthUser->lastname(),
  56.             );
  57.             $customer = $this->createCustomerHandler->handle($command);
  58.         }
  59.  
  60.         // TODO $customer->allowAuthVendor();
  61.  
  62.         return $customer;
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement