Completed
Push — master ( 0a1951...1a638f )
by Paweł
22:32 queued 13:24
created

AddressContext::createAddress()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 4
nop 7
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Transform;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Addressing\Converter\CountryNameConverterInterface;
16
use Sylius\Component\Addressing\Model\CountryInterface;
17
use Sylius\Component\Addressing\Model\ProvinceInterface;
18
use Sylius\Component\Core\Model\AddressInterface;
19
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Łukasz Chruściel <[email protected]>
26
 */
27
final class AddressContext implements Context
28
{
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $addressFactory;
33
34
    /**
35
     * @var CountryNameConverterInterface
36
     */
37
    private $countryNameConverter;
38
39
    /**
40
     * @var AddressRepositoryInterface
41
     */
42
    private $addressRepository;
43
44
    /**
45
     * @var RepositoryInterface
46
     */
47
    private $countryRepository;
48
49
    /**
50
     * @param FactoryInterface $addressFactory
51
     * @param CountryNameConverterInterface $countryNameConverter
52
     * @param AddressRepositoryInterface $addressRepository
53
     * @param RepositoryInterface $countryRepository
54
     */
55
    public function __construct(
56
        FactoryInterface $addressFactory,
57
        CountryNameConverterInterface $countryNameConverter,
58
        AddressRepositoryInterface $addressRepository,
59
        RepositoryInterface $countryRepository
60
    ) {
61
        $this->addressFactory = $addressFactory;
62
        $this->countryNameConverter = $countryNameConverter;
63
        $this->addressRepository = $addressRepository;
64
        $this->countryRepository = $countryRepository;
65
    }
66
67
    /**
68
     * @Transform /^to "([^"]+)"$/
69
     * @Transform /^"([^"]+)" based \w+ address$/
70
     */
71
    public function createNewAddress($countryName)
72
    {
73
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
74
75
        return $this->createAddress($countryCode);
76
    }
77
78
    /**
79
     * @Transform /^address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
80
     * @Transform /^address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
81
     * @Transform /^address to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
82
     */
83
    public function createNewAddressWith($city, $street, $postcode, $countryName, $customerName)
84
    {
85
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
86
        list($firstName, $lastName) = explode(' ', $customerName);
87
88
        return $this->createAddress($countryCode, $firstName, $lastName, $city, $street, $postcode);
89
    }
90
91
    /**
92
     * @Transform /^clear old (shipping|billing) address$/
93
     * @Transform /^do not specify any (shipping|billing) address$/
94
     */
95
    public function createEmptyAddress()
96
    {
97
        return $this->addressFactory->createNew();
98
    }
99
100
    /**
101
     * @Transform /^address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/
102
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"(?:|, "([^"]+)")$/
103
     * @Transform /^of "([^"]+)" in the "([^"]+)", "([^"]+)" "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
104
     * @Transform /^addressed it to "([^"]+)", "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"(?:|, "([^"]+)")$/
105
     * @Transform /^address (?:|is )"([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
106
     * @Transform /^address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
107
     */
108
    public function createNewAddressWithName($name, $street, $postcode, $city, $countryName, $provinceName = null)
109
    {
110
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
111
        list($firstName, $lastName) = explode(' ', $name);
112
113
        return $this->createAddress($countryCode, $firstName, $lastName, $city, $street, $postcode, $provinceName);
114
    }
115
116
    /**
117
     * @Transform /^"([^"]+)" street$/
118
     */
119
    public function getByStreet($street)
120
    {
121
        $address = $this->addressRepository->findOneBy(['street' => $street]);
122
        Assert::notNull($address, sprintf('Cannot find address by %s street.', $street));
123
124
        return $address;
125
    }
126
127
    /**
128
     * @param string $countryCode
129
     * @param string $firstName
130
     * @param string $lastName
131
     * @param string $city
132
     * @param string $street
133
     * @param string $postCode
134
     * @param string $provinceName
135
     *
136
     * @return AddressInterface
137
     */
138
    private function createAddress(
139
        $countryCode = 'US',
140
        $firstName = 'John',
141
        $lastName = 'Doe',
142
        $city = 'Ankh Morpork',
143
        $street = 'Frost Alley',
144
        $postCode = '90210',
145
        $provinceName = null
146
    ) {
147
        /** @var AddressInterface $address */
148
        $address = $this->addressFactory->createNew();
149
        $address->setCountryCode($countryCode);
150
        $address->setFirstName($firstName);
151
        $address->setLastName($lastName);
152
        $address->setCity($city);
153
        $address->setStreet($street);
154
        $address->setPostcode($postCode);
155
156
        if (null === $provinceName) {
157
            return $address;
158
        }
159
160
        /** @var CountryInterface $country */
161
        $country = $this->countryRepository->findOneBy(['code' => $countryCode]);
162
163
        if (null !== $country) {
164
            /** @var ProvinceInterface $province */
165
            foreach ($country->getProvinces() as $province) {
166
                if ($province->getName() === $provinceName) {
167
                    $address->setProvinceCode($province->getCode());
168
169
                    return $address;
170
                }
171
            }
172
        }
173
174
        $address->setProvinceName($provinceName);
175
176
        return $address;
177
    }
178
}
179