Completed
Pull Request — master (#229)
by
unknown
02:44
created

UpdateAddressBookAddressHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 13 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 13
loc 100
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A handle() 0 30 3
A assertCountryExists() 0 4 1
A assertProvinceExists() 0 4 1
A assertCurrentUserIsOwner() 0 5 1
A assertAddressExists() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Webmozart\Assert\Assert;
13
14
final class UpdateAddressBookAddressHandler
15
{
16
    /**
17
     * @var RepositoryInterface
18
     */
19
    private $addressRepository;
20
21
    /**
22
     * @var FactoryInterface
23
     */
24
    private $addressFactory;
25
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    private $countryRepository;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $provinceRepository;
35
36
    /**
37
     * @var RepositoryInterface
38
     */
39
    private $shopUserRepository;
40
41
    /**
42
     * @param RepositoryInterface $addressRepository
43
     * @param RepositoryInterface $countryRepository
44
     * @param RepositoryInterface $provinceRepository
45
     * @param RepositoryInterface $shopUserRepository
46
     * @param FactoryInterface $addressFactory
47
     */
48 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...
49
        RepositoryInterface $addressRepository,
50
        RepositoryInterface $countryRepository,
51
        RepositoryInterface $provinceRepository,
52
        RepositoryInterface $shopUserRepository,
53
        FactoryInterface $addressFactory
54
    ) {
55
        $this->addressRepository = $addressRepository;
56
        $this->countryRepository = $countryRepository;
57
        $this->provinceRepository = $provinceRepository;
58
        $this->addressFactory = $addressFactory;
59
        $this->shopUserRepository = $shopUserRepository;
60
    }
61
62
    public function handle(UpdateAddress $command): void
63
    {
64
        /** @var AddressInterface $address */
65
        $address = $this->addressRepository->findOneBy(['id' => $command->id()]);
66
        /** @var ShopUserInterface $shopUser */
67
        $shopUser = $this->shopUserRepository->findOneBy(['username' => $command->userEmail()]);
68
69
        $this->assertAddressExists($address);
70
        $this->assertCurrentUserIsOwner($address, $shopUser);
71
        $this->assertCountryExists($command->countryCode());
72
73
        /** @var AddressInterface $address */
74
        $address->setFirstName($command->firstName());
75
        $address->setLastName($command->lastName());
76
        $address->setCompany($command->company());
77
        $address->setStreet($command->street());
78
        $address->setCountryCode($command->countryCode());
79
        $address->setCity($command->city());
80
        $address->setPostcode($command->postcode());
81
        $address->setPhoneNumber($command->phoneNumber());
82
83
        if (null !== $command->provinceCode() && $command->provinceCode() !== $address->getProvinceCode()) {
84
            $province = $this->provinceRepository->findOneBy(['code' => $command->provinceCode()]);
85
            $this->assertProvinceExists($province);
86
            $address->setProvinceCode($province->getCode());
87
            $address->setProvinceName($province->getName());
88
        }
89
90
        $this->addressRepository->add($address);
91
    }
92
93
    private function assertCountryExists(string $countryCode): void
94
    {
95
        Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.');
96
    }
97
98
    private function assertProvinceExists($province): void
99
    {
100
        Assert::notNull($province, 'Province does not exist.');
101
    }
102
103
    private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user)
104
    {
105
        Assert::notNull($address->getCustomer(), 'Address is not associated with any user');
106
        Assert::eq($address->getCustomer(), $user->getCustomer(), 'Current user is not owner of this address');
107
    }
108
109
    private function assertAddressExists($address)
110
    {
111
        Assert::notNull($address, 'Address does not exist.');
112
    }
113
}
114