Completed
Push — master ( 7d844f...319af7 )
by Kamil
17s
created

AddressContext   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 3
cbo 5
dl 0
loc 140
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A createNewAddress() 0 7 1
A createNewAddressWith() 0 16 1
A createEmptyAddress() 0 4 1
A createNewAddressWithNameAndProvince() 0 17 1
A createNewAddressWithName() 0 16 1
A getByStreet() 0 7 1
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\Bundle\CoreBundle\Fixture\Factory\AddressExampleFactory;
16
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
17
use Sylius\Component\Addressing\Converter\CountryNameConverterInterface;
18
use Sylius\Component\Addressing\Model\CountryInterface;
19
use Sylius\Component\Addressing\Model\ProvinceInterface;
20
use Sylius\Component\Core\Model\AddressInterface;
21
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
use Webmozart\Assert\Assert;
25
26
/**
27
 * @author Łukasz Chruściel <[email protected]>
28
 */
29
final class AddressContext implements Context
30
{
31
    /**
32
     * @var FactoryInterface
33
     */
34
    private $addressFactory;
35
36
    /**
37
     * @var CountryNameConverterInterface
38
     */
39
    private $countryNameConverter;
40
41
    /**
42
     * @var AddressRepositoryInterface
43
     */
44
    private $addressRepository;
45
46
    /**
47
     * @var ExampleFactoryInterface
48
     */
49
    private $exampleAddressFactory;
50
51
    /**
52
     * @param FactoryInterface $addressFactory
53
     * @param CountryNameConverterInterface $countryNameConverter
54
     * @param AddressRepositoryInterface $addressRepository
55
     * @param ExampleFactoryInterface $exampleAddressFactory
56
     */
57
    public function __construct(
58
        FactoryInterface $addressFactory,
59
        CountryNameConverterInterface $countryNameConverter,
60
        AddressRepositoryInterface $addressRepository,
61
        ExampleFactoryInterface $exampleAddressFactory
62
    ) {
63
        $this->addressFactory = $addressFactory;
64
        $this->countryNameConverter = $countryNameConverter;
65
        $this->addressRepository = $addressRepository;
66
        $this->exampleAddressFactory = $exampleAddressFactory;
67
    }
68
69
    /**
70
     * @Transform /^to "([^"]+)"$/
71
     * @Transform /^"([^"]+)" based \w+ address$/
72
     */
73
    public function createNewAddress($countryName)
74
    {
75
        return $this->exampleAddressFactory->create([
76
            'country_code' => $this->countryNameConverter->convertToCode($countryName),
77
            'customer' => null,
78
        ]);
79
    }
80
81
    /**
82
     * @Transform /^address (?:as |is |to )"([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
83
     */
84
    public function createNewAddressWith($city, $street, $postcode, $countryName, $customerName)
85
    {
86
        list($firstName, $lastName) = explode(' ', $customerName);
87
88
        return $this->exampleAddressFactory->create([
89
            'country_code' => $this->countryNameConverter->convertToCode($countryName),
90
            'first_name' => $firstName,
91
            'last_name' => $lastName,
92
            'company' => null,
93
            'customer' => null,
94
            'phone_number' => null,
95
            'city' => $city,
96
            'street' => $street,
97
            'postcode' => $postcode,
98
        ]);
99
    }
100
101
    /**
102
     * @Transform /^clear old (shipping|billing) address$/
103
     * @Transform /^do not specify any (shipping|billing) address$/
104
     */
105
    public function createEmptyAddress()
106
    {
107
        return $this->addressFactory->createNew();
108
    }
109
110
    /**
111
     * @Transform /^address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/
112
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)", "([^"]+)"$/
113
     * @Transform /^of "([^"]+)" in the "([^"]+)", "([^"]+)" "([^"]+)", "([^"]+)", "([^"]+)"$/
114
     * @Transform /^addressed it to "([^"]+)", "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)", "([^"]+)"$/
115
     * @Transform /^address (?:|is |as )"([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/
116
     */
117
    public function createNewAddressWithNameAndProvince($name, $street, $postcode, $city, $countryName, $provinceName)
118
    {
119
        list($firstName, $lastName) = explode(' ', $name);
120
121
        return $this->exampleAddressFactory->create([
122
            'country_code' => $this->countryNameConverter->convertToCode($countryName),
123
            'first_name' => $firstName,
124
            'last_name' => $lastName,
125
            'company' => null,
126
            'customer' => null,
127
            'phone_number' => null,
128
            'city' => $city,
129
            'street' => $street,
130
            'postcode' => $postcode,
131
            'province_name' => $provinceName,
132
        ]);
133
    }
134
135
    /**
136
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"$/
137
     * @Transform /^of "([^"]+)" in the "([^"]+)", "([^"]+)" "([^"]+)", "([^"]+)"$/
138
     * @Transform /^addressed it to "([^"]+)", "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"$/
139
     * @Transform /^address (?:|is |as )"([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/
140
     */
141
    public function createNewAddressWithName($name, $street, $postcode, $city, $countryName)
142
    {
143
        list($firstName, $lastName) = explode(' ', $name);
144
145
        return $this->exampleAddressFactory->create([
146
            'country_code' => $this->countryNameConverter->convertToCode($countryName),
147
            'first_name' => $firstName,
148
            'last_name' => $lastName,
149
            'company' => null,
150
            'customer' => null,
151
            'phone_number' => null,
152
            'city' => $city,
153
            'street' => $street,
154
            'postcode' => $postcode,
155
        ]);
156
    }
157
158
    /**
159
     * @Transform /^"([^"]+)" street$/
160
     */
161
    public function getByStreet($street)
162
    {
163
        $address = $this->addressRepository->findOneBy(['street' => $street]);
164
        Assert::notNull($address, sprintf('Cannot find address by %s street.', $street));
165
166
        return $address;
167
    }
168
}
169