Advertisement
Guest User

CreateCustomerHandler

a guest
May 24th, 2021
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Identity\Application\Customer\UseCase\CreateCustomer;
  6.  
  7. use App\Identity\Application\Customer\CustomerEntityManager;
  8. use App\Identity\Application\Security\PasswordEncoder;
  9. use App\Identity\Domain\Customer\Customer;
  10. use App\Identity\Domain\Customer\Name;
  11. use App\Identity\Domain\Customer\Username;
  12. use App\Identity\Domain\User\Email;
  13. use App\Identity\Domain\User\Password;
  14.  
  15. final class CreateCustomerHandler
  16. {
  17.     private CustomerEntityManager $customerEntityManager;
  18.  
  19.     private PasswordEncoder $passwordEncoder;
  20.  
  21.     public function __construct(CustomerEntityManager $customerEntityManagerByActiveTenant,
  22.                                 PasswordEncoder $passwordEncoder)
  23.     {
  24.         $this->customerEntityManager = $customerEntityManagerByActiveTenant;
  25.         $this->passwordEncoder = $passwordEncoder;
  26.     }
  27.  
  28.     public function handle(CreateCustomerCommand $command): Customer
  29.     {
  30.         $customerId = $this->customerEntityManager->nextId();
  31.         $email = new Email($command->email());
  32.         $username = new Username($command->username());
  33.         $name = new Name($command->firstname(), $command->lastname());
  34.         $password = $this->passwordEncoder->encodePassword($command->password());
  35.         $password = new Password($password);
  36.  
  37.         $customer = new Customer(
  38.             $customerId,
  39.             $email,
  40.             $password,
  41.             $username,
  42.             $name,
  43.         );
  44.  
  45.         $this->customerEntityManager->create($customer);
  46.  
  47.         return $customer;
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement