Failed Conditions
Pull Request — master (#229)
by
unknown
02:41
created

CreateAddressHandler::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Handler;
6
7
use Sylius\Component\Addressing\Model\ProvinceInterface;
8
use Sylius\Component\Core\Model\AddressInterface;
9
use Sylius\Component\Core\Model\ShopUserInterface;
10
use Sylius\Component\Resource\Factory\FactoryInterface;
11
use Sylius\Component\Resource\Repository\RepositoryInterface;
12
use Sylius\ShopApiPlugin\Command\CreateAddress;
13
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
14
use Webmozart\Assert\Assert;
15
16
final class CreateAddressHandler
17
{
18
    /**
19
     * @var RepositoryInterface
20
     */
21
    private $addressRepository;
22
23
    /**
24
     * @var FactoryInterface
25
     */
26
    private $addressFactory;
27
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    private $countryRepository;
32
33
    /**
34
     * @var RepositoryInterface
35
     */
36
    private $provinceRepository;
37
38
    /**
39
     * @var TokenStorageInterface
40
     */
41
    private $tokenStorage;
42
43
    /**
44
     * CreateAddressHandler constructor.
45
     *
46
     * @param RepositoryInterface $addressRepository
47
     * @param RepositoryInterface $countryRepository
48
     * @param RepositoryInterface $provinceRepository
49
     * @param FactoryInterface $addressFactory
50
     * @param TokenStorageInterface $tokenStorage
51
     */
52
    public function __construct(
53
        RepositoryInterface $addressRepository,
54
        RepositoryInterface $countryRepository,
55
        RepositoryInterface $provinceRepository,
56
        FactoryInterface $addressFactory,
57
        TokenStorageInterface $tokenStorage
58
    ) {
59
        $this->addressRepository = $addressRepository;
60
        $this->countryRepository = $countryRepository;
61
        $this->provinceRepository = $provinceRepository;
62
        $this->addressFactory = $addressFactory;
63
        $this->tokenStorage = $tokenStorage;
64
    }
65
66
    public function handle(CreateAddress $command): void
67
    {
68
        $user = $this->tokenStorage->getToken()->getUser();
69
        $customer = $user->getCustomer();
70
71
        $this->assertShopUserExists($user);
72
        $this->assertCountryExists($command->countryCode());
73
74
        /** @var AddressInterface $address */
75
        $address = $this->addressFactory->createNew();
76
        $address->setFirstName($command->firstName());
77
        $address->setLastName($command->lastName());
78
        $address->setCompany($command->company());
79
        $address->setStreet($command->street());
80
        $address->setCountryCode($command->countryCode());
81
        $address->setCity($command->city());
82
        $address->setPostcode($command->postcode());
83
        $address->setPhoneNumber($command->phoneNumber());
84
85
        if ($command->provinceCode()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $command->provinceCode() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86
            $province = $this->getProvince($command->provinceCode());
87
            $this->assertProvinceExists($province);
88
            $address->setProvinceCode($province->getCode());
89
            $address->setProvinceName($province->getName());
90
        }
91
92
        $customer->addAddress($address);
93
        $this->addressRepository->add($address);
94
    }
95
96
    /**
97
     * @param string $countryCode
98
     */
99
    private function assertCountryExists(string $countryCode): void
100
    {
101
        Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.');
102
    }
103
104
    /**
105
     * @param $province
106
     */
107
    private function assertProvinceExists($province): void
108
    {
109
        Assert::notNull($province, 'Province does not exist.');
110
    }
111
112
    /**
113
     * @param string $provinceCode
114
     *
115
     * @return ProvinceInterface|object
116
     */
117
    private function getProvince(string $provinceCode)
118
    {
119
        return $this->provinceRepository->findOneBy(['code' => $provinceCode]);
120
    }
121
122
    /**
123
     * @param $user
124
     */
125
    private function assertShopUserExists($user)
126
    {
127
        Assert::isInstanceOf($user, ShopUserInterface::class, 'Logged in user does not exist');
128
    }
129
}
130