Completed
Push — a-bulwa-travis-is-2 ( 7c64aa )
by Kamil
41:24
created

AddressingContext::getByStreet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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\Component\Addressing\Converter\CountryNameConverterInterface;
16
use Sylius\Component\Core\Model\AddressInterface;
17
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Łukasz Chruściel <[email protected]>
23
 */
24
final class AddressingContext implements Context
25
{
26
    /**
27
     * @var FactoryInterface
28
     */
29
    private $addressFactory;
30
31
    /**
32
     * @var CountryNameConverterInterface
33
     */
34
    private $countryNameConverter;
35
36
    /**
37
     * @var AddressRepositoryInterface
38
     */
39
    private $addressRepository;
40
41
    /**
42
     * @param FactoryInterface $addressFactory
43
     * @param CountryNameConverterInterface $countryNameConverter
44
     * @param AddressRepositoryInterface $addressRepository
45
     */
46
    public function __construct(
47
        FactoryInterface $addressFactory,
48
        CountryNameConverterInterface $countryNameConverter,
49
        AddressRepositoryInterface $addressRepository
50
    ) {
51
        $this->addressFactory = $addressFactory;
52
        $this->countryNameConverter = $countryNameConverter;
53
        $this->addressRepository = $addressRepository;
54
    }
55
56
    /**
57
     * @Transform /^to "([^"]+)"$/
58
     * @Transform /^"([^"]+)" based \w+ address$/
59
     */
60
    public function createNewAddress($countryName)
61
    {
62
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
63
64
        return $this->createAddress($countryCode);
65
    }
66
67
    /**
68
     * @Transform /^address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
69
     * @Transform /^address is "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
70
     * @Transform /^address to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)"$/
71
     */
72
    public function createNewAddressWith($cityName, $street, $postcode, $countryName, $customerName,  $provinceName = null)
73
    {
74
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
75
        $customerName = explode(' ', $customerName);
76
77
        return $this->createAddress($countryCode, $customerName[0], $customerName[1], $cityName, $street, $postcode, $provinceName);
78
    }
79
80
    /**
81
     * @Transform /^do not specify any (shipping|billing) address$/
82
     */
83
    public function createEmptyAddress()
84
    {
85
        return $this->addressFactory->createNew();
86
    }
87
88
    /**
89
     * @Transform /^address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"$/
90
     * @Transform /^"([^"]+)" addressed it to "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"(?:|, "([^"]+)")$/
91
     * @Transform /^of "([^"]+)" in the "([^"]+)", "([^"]+)" "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
92
     * @Transform /^addressed it to "([^"]+)", "([^"]+)", "([^"]+)" "([^"]+)" in the "([^"]+)"(?:|, "([^"]+)")$/
93
     * @Transform /^address (?:|is )"([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
94
     * @Transform /^address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)"(?:|, "([^"]+)")$/
95
     */
96
    public function createNewAddressWithName($name, $street, $postcode, $city, $countryName, $provinceName = null)
97
    {
98
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
99
        $names = explode(" ", $name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 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
        return $this->createAddress($countryCode, $names[0], $names[1], $city, $street, $postcode, $provinceName);
102
    }
103
104
    /**
105
     * @Transform /^"([^"]+)" street$/
106
     */
107
    public function getByStreet($street)
108
    {
109
        $address = $this->addressRepository->findOneBy(['street' => $street]);
110
        Assert::notNull($address, sprintf('Cannot find address by %s street' , $street));
111
112
        return $address;
113
    }
114
115
    /**
116
     * @param string $countryCode
117
     * @param string $firstName
118
     * @param string $lastName
119
     * @param string $city
120
     * @param string $street
121
     * @param string $postCode
122
     * @param string $provinceName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $provinceName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
123
     *
124
     * @return AddressInterface
125
     */
126
    private function createAddress(
127
        $countryCode = 'US',
128
        $firstName = 'John',
129
        $lastName = 'Doe',
130
        $city = 'Ankh Morpork',
131
        $street = 'Frost Alley',
132
        $postCode = '90210',
133
        $provinceName = null
134
    ) {
135
        /** @var AddressInterface $address */
136
        $address = $this->addressFactory->createNew();
137
        $address->setCountryCode($countryCode);
138
        $address->setFirstName($firstName);
139
        $address->setLastName($lastName);
140
        $address->setCity($city);
141
        $address->setStreet($street);
142
        $address->setPostcode($postCode);
143
        $address->setProvinceName($provinceName);
144
145
        return $address;
146
    }
147
}
148