CreateUserHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A __invoke() 0 33 1
A assertEmailIsNotTaken() 0 4 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\CommandHandler\User;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\User\CreateUser;
16
use Sylius\Component\Core\Factory\AddressFactoryInterface;
17
use Sylius\Component\Core\Model\CustomerInterface;
18
use Sylius\Component\Core\Model\ShopUserInterface;
19
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\User\Repository\UserRepositoryInterface;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Webmozart\Assert\Assert;
24
25
final class CreateUserHandler implements MessageHandlerInterface
26
{
27
    /** @var FactoryInterface */
28
    private $customerFactory;
29
30
    /** @var CustomerRepositoryInterface */
31
    private $customerRepository;
32
33
    /** @var FactoryInterface */
34
    private $userFactory;
35
36
    /** @var UserRepositoryInterface */
37
    private $userRepository;
38
39
    /** @var AddressFactoryInterface */
40
    private $addressFactory;
41
42
    public function __construct(
43
        FactoryInterface $customerFactory,
44
        CustomerRepositoryInterface $customerRepository,
45
        FactoryInterface $userFactory,
46
        UserRepositoryInterface $userRepository,
47
        AddressFactoryInterface $addressFactory
48
    ) {
49
        $this->customerFactory = $customerFactory;
50
        $this->customerRepository = $customerRepository;
51
        $this->userFactory = $userFactory;
52
        $this->userRepository = $userRepository;
53
        $this->addressFactory = $addressFactory;
54
    }
55
56
    public function __invoke(CreateUser $command): void
57
    {
58
        $this->assertEmailIsNotTaken($command->customer()->email);
59
60
        /** @var CustomerInterface $customer */
61
        $customer = $this->customerFactory->createNew();
62
63
        $customer->setFirstName($command->customer()->firstname);
64
        $customer->setLastName($command->customer()->lastname);
65
        $customer->setEmail($command->customer()->email);
66
67
        $this->customerRepository->add($customer);
68
69
        /** @var ShopUserInterface $user */
70
        $user = $this->userFactory->createNew();
71
        $user->setPlainPassword($command->password());
72
        $user->setCustomer($customer);
73
        $user->setUsername($command->customer()->email);
74
        $user->setUsernameCanonical($command->customer()->email);
75
        $user->setEnabled(true);
76
77
        $this->userRepository->add($user);
78
79
        $address = $this->addressFactory->createForCustomer($customer);
80
        $address->setFirstName($command->customer()->firstname);
81
        $address->setLastName($command->customer()->lastname);
82
        $address->setStreet('');
83
        $address->setCountryCode('');
84
        $address->setPostcode('');
85
        $address->setCity('');
86
87
        $customer->setDefaultAddress($address);
88
    }
89
90
    private function assertEmailIsNotTaken(string $email): void
91
    {
92
        Assert::null($this->userRepository->findOneByEmail($email), 'User with given email already exists.');
93
    }
94
}
95