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

CreateAddressHandler::checkProvinceExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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\CustomerInterface;
10
use Sylius\Component\Resource\Factory\FactoryInterface;
11
use Sylius\Component\Resource\Repository\RepositoryInterface;
12
use Sylius\ShopApiPlugin\Command\CreateAddress;
13
use Webmozart\Assert\Assert;
14
15
final class CreateAddressHandler
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 RepositoryInterface
39
     */
40
    private $customerRepository;
41
42
    /**
43
     * @param RepositoryInterface $addressRepository
44
     * @param RepositoryInterface $countryRepository
45
     * @param RepositoryInterface $provinceRepository
46
     * @param RepositoryInterface $customerRepository
47
     * @param FactoryInterface $addressFactory
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
        RepositoryInterface $customerRepository,
54
        FactoryInterface $addressFactory
55
    ) {
56
        $this->addressRepository = $addressRepository;
57
        $this->countryRepository = $countryRepository;
58
        $this->provinceRepository = $provinceRepository;
59
        $this->addressFactory = $addressFactory;
60
        $this->customerRepository = $customerRepository;
61
    }
62
63
    public function handle(CreateAddress $command): void
64
    {
65
        /** @var CustomerInterface $shopUser */
66
        $customer = $this->customerRepository->findOneBy(['email' => $command->userEmail()]);
67
68
        $this->assertCustomerExists($customer);
69
        $this->assertCountryExists($command->countryCode());
70
71
        /** @var AddressInterface $address */
72
        $address = $this->addressFactory->createNew();
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()) {
83
            $province = $this->checkProvinceExists($command->provinceCode());
84
            $address->setProvinceCode($province->getCode());
85
            $address->setProvinceName($province->getName());
86
        }
87
88
        $customer->addAddress($address);
89
90
        $this->addressRepository->add($address);
91
    }
92
93
    /**
94
     * @param $customer
95
     */
96
    private function assertCustomerExists($customer)
97
    {
98
        Assert::notNull($customer, 'Customer does not exists!');
99
    }
100
101
    /**
102
     * @param string $countryCode
103
     */
104
    private function assertCountryExists(string $countryCode): void
105
    {
106
        Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.');
107
    }
108
109
    /**
110
     * @param string $provinceCode
111
     *
112
     * @return ProvinceInterface
113
     */
114
    private function checkProvinceExists(string $provinceCode): ProvinceInterface
115
    {
116
        /** @var ProvinceInterface $province */
117
        $province = $this->provinceRepository->findOneBy(['code' => $provinceCode]);
118
119
        Assert::notNull($province, 'Province does not exist.');
120
121
        return $province;
122
    }
123
}
124