Failed Conditions
Pull Request — master (#229)
by
unknown
04:40 queued 02:07
created

assertProvinceExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Handler;
6
7
use Sylius\Component\Core\Model\AddressInterface;
8
use Sylius\Component\Core\Model\ShopUserInterface;
9
use Sylius\Component\Resource\Factory\FactoryInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
use Sylius\ShopApiPlugin\Command\UpdateAddress;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13
use Webmozart\Assert\Assert;
14
15
final class UpdateAddressBookAddressHandler
16
{
17
    /**
18
     * @var RepositoryInterface
19
     */
20
    private $addressRepository;
21
22
    /**
23
     * @var FactoryInterface
24
     */
25
    private $addressFactory;
26
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    private $countryRepository;
31
32
    /**
33
     * @var RepositoryInterface
34
     */
35
    private $provinceRepository;
36
37
    /**
38
     * @var TokenStorageInterface
39
     */
40
    private $tokenStorage;
41
42
    /**
43
     * @param RepositoryInterface $addressRepository
44
     * @param RepositoryInterface $countryRepository
45
     * @param RepositoryInterface $provinceRepository
46
     * @param FactoryInterface $addressFactory
47
     * @param TokenStorageInterface $tokenStorage
48
     */
49 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
        RepositoryInterface $addressRepository,
51
        RepositoryInterface $countryRepository,
52
        RepositoryInterface $provinceRepository,
53
        FactoryInterface $addressFactory,
54
        TokenStorageInterface $tokenStorage
55
    ) {
56
        $this->addressRepository = $addressRepository;
57
        $this->countryRepository = $countryRepository;
58
        $this->provinceRepository = $provinceRepository;
59
        $this->addressFactory = $addressFactory;
60
        $this->tokenStorage = $tokenStorage;
61
    }
62
63
    public function handle(UpdateAddress $command): void
64
    {
65
        /** @var AddressInterface $address */
66
        $address = $this->addressRepository->findOneBy(['id' => $command->id()]);
67
        $user = $this->tokenStorage->getToken()->getUser();
68
69
        $this->assertCurrentUserIsOwner($address, $user);
70
        $this->assertCountryExists($command->countryCode());
71
72
        /** @var AddressInterface $address */
73
        $address->setFirstName($command->firstName());
74
        $address->setLastName($command->lastName());
75
        $address->setCompany($command->company());
76
        $address->setStreet($command->street());
77
        $address->setCountryCode($command->countryCode());
78
        $address->setCity($command->city());
79
        $address->setPostcode($command->postcode());
80
        $address->setPhoneNumber($command->phoneNumber());
81
82
        if (null !== $command->provinceCode() && $command->provinceCode() !== $address->getProvinceCode()) {
83
            $province = $this->provinceRepository->findOneBy(['code' => $command->provinceCode()]);
84
            $this->assertProvinceExists($province);
85
            $address->setProvinceCode($province->getCode());
86
            $address->setProvinceName($province->getName());
87
        }
88
89
        $this->addressRepository->add($address);
90
    }
91
92
    private function assertCountryExists(string $countryCode): void
93
    {
94
        Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.');
95
    }
96
97
    private function assertProvinceExists($province): void
98
    {
99
        Assert::notNull($province, 'Province does not exist.');
100
    }
101
102
    private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user)
103
    {
104
        Assert::notNull($address->getCustomer(), 'Address is not associated with any user');
105
        Assert::eq($address->getCustomer(), $user->getCustomer(), 'Current user is not owner of this address');
106
    }
107
}
108