Completed
Push — master ( cfa808...648607 )
by Paweł
63:01 queued 48:26
created

AddressingContext::createNewAddressWithUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 5
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\Core\Model\AddressInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
19
/**
20
 * @author Łukasz Chruściel <[email protected]>
21
 */
22
final class AddressingContext implements Context
23
{
24
    /**
25
     * @var FactoryInterface
26
     */
27
    private $addressFactory;
28
29
    /**
30
     * @var CountryNameConverterInterface
31
     */
32
    private $countryNameConverter;
33
34
    /**
35
     * @param FactoryInterface $addressFactory
36
     * @param CountryNameConverterInterface $countryNameConverter
37
     */
38
    public function __construct(
39
        FactoryInterface $addressFactory,
40
        CountryNameConverterInterface $countryNameConverter
41
    ) {
42
        $this->addressFactory = $addressFactory;
43
        $this->countryNameConverter = $countryNameConverter;
44
    }
45
46
    /**
47
     * @Transform /^to "([^"]+)"$/
48
     */
49
    public function createNewAddress($countryName)
50
    {
51
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
52
53
        return $this->createAddress($countryCode);
54
    }
55
56
    /**
57
     * @Transform /^address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
58
     */
59
    public function createNewAddressWith($cityName, $street, $postcode, $countryName, $customerName)
60
    {
61
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
62
        $customerName = explode(' ', $customerName);
63
        
64
        return $this->createAddress($countryCode, $customerName[0], $customerName[1], $cityName, $street, $postcode);
65
    }
66
67
    /**
68
     * @Transform /^do not specify any (shipping|billing) address$/
69
     */
70
    public function createEmptyAddress()
71
    {
72
        return $this->addressFactory->createNew();
73
    }
74
75
    /**
76
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"$/
77
     * @Transform /^of "([^"]+)" in the "([^"]+)", "([^"]+)" "([^"]+)", "([^"]+)"$/
78
     * @Transform /^addressed it to "([^"]+)", "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"$/
79
     */
80
    public function createNewAddressWithName($name, $street, $postcode, $city, $countryName)
81
    {
82
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
83
        $names = explode(" ", $name);
84
85
        return $this->createAddress($countryCode, $names[0], $names[1], $city, $street, $postcode);
86
    }
87
88
    /**
89
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"$/
90
     */
91
    public function createNewAddressWithUser($name, $street, $postcode, $city, $countryName)
92
    {
93
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
94
        $names = explode(" ", $name);
95
96
        return $this->createAddress($countryCode, $names[0], $names[1], $city, $street, $postcode);
97
    }
98
99
    /**
100
     * @param string $countryCode
101
     * @param string $firstName
102
     * @param string $lastName
103
     * @param string $city
104
     * @param string $street
105
     *
106
     * @return AddressInterface
107
     */
108
    private function createAddress(
109
        $countryCode = 'FR',
110
        $firstName = 'John',
111
        $lastName = 'Doe',
112
        $city = 'Ankh Morpork',
113
        $street = 'Frost Alley',
114
        $postCode = '90210'
115
    ) {
116
        /** @var AddressInterface $address */
117
        $address = $this->addressFactory->createNew();
118
        $address->setCountryCode($countryCode);
119
        $address->setFirstName($firstName);
120
        $address->setLastName($lastName);
121
        $address->setCity($city);
122
        $address->setStreet($street);
123
        $address->setPostcode($postCode);
124
125
        return $address;
126
    }
127
}
128