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

CreateAddressHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Handler;
4
5
use Sylius\Component\Addressing\Model\ProvinceInterface;
6
use Sylius\Component\Core\Model\AddressInterface;
7
use Sylius\Component\Core\Model\ShopUserInterface;
8
use Sylius\Component\Resource\Factory\FactoryInterface;
9
use Sylius\Component\Resource\Repository\RepositoryInterface;
10
use Sylius\ShopApiPlugin\Command\CreateAddress;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
use Webmozart\Assert\Assert;
13
14
final class CreateAddressHandler
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 TokenStorageInterface
38
     */
39
    private $tokenStorage;
40
41
    /**
42
     * CreateAddressHandler constructor.
43
     * @param RepositoryInterface $addressRepository
44
     * @param RepositoryInterface $countryRepository
45
     * @param RepositoryInterface $provinceRepository
46
     * @param FactoryInterface $addressFactory
47
     * @param TokenStorageInterface $tokenStorage
48
     */
49
    public function __construct(
50
        RepositoryInterface $addressRepository,
51
        RepositoryInterface $countryRepository,
52
        RepositoryInterface $provinceRepository,
53
        FactoryInterface $addressFactory,
54
        TokenStorageInterface $tokenStorage
55
    )
56
    {
57
        $this->addressRepository = $addressRepository;
58
        $this->countryRepository = $countryRepository;
59
        $this->provinceRepository = $provinceRepository;
60
        $this->addressFactory = $addressFactory;
61
        $this->tokenStorage = $tokenStorage;
62
    }
63
64
    public function handle(CreateAddress $command): void
65
    {
66
        $user = $this->tokenStorage->getToken()->getUser();
67
        $customer = $user->getCustomer();
68
69
        $this->assertShopUserExists($user);
70
        $this->assertCountryExists($command->countryCode());
71
72
        /** @var AddressInterface $address */
73
        $address = $this->addressFactory->createNew();
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($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...
84
            $province = $this->getProvince($command->provinceCode());
85
            $this->assertProvinceExists($province);
86
            $address->setProvinceCode($province->getCode());
87
            $address->setProvinceName($province->getName());
88
        }
89
90
        $customer->addAddress($address);
91
        $this->addressRepository->add($address);
92
    }
93
94
    /**
95
     * @param string $countryCode
96
     */
97
    private function assertCountryExists(string $countryCode): void
98
    {
99
        Assert::notNull($this->countryRepository->findOneBy(["code" => $countryCode]), 'Country does not exist.');
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal code does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
100
    }
101
102
    /**
103
     * @param $province
104
     */
105
    private function assertProvinceExists($province): void
106
    {
107
        Assert::notNull($province, 'Province does not exist.');
108
    }
109
110
    /**
111
     * @param string $provinceCode
112
     * @return ProvinceInterface|object
113
     */
114
    private function getProvince(string $provinceCode)
115
    {
116
        return $this->provinceRepository->findOneBy(["code" => $provinceCode]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal code does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
117
    }
118
119
    /**
120
     * @param $user
121
     */
122
    private function assertShopUserExists($user)
123
    {
124
        Assert::isInstanceOf($user, ShopUserInterface::class, "Logged in user does not exist");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Logged in user does not exist does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
125
    }
126
}