Completed
Push — master ( 133b57...4c004c )
by Kamil
20:32
created

AddressingContext::createAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 1
eloc 15
nc 1
nop 6
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\Core\Model\AddressInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
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 RepositoryInterface
38
     */
39
    private $countryRepository;
40
41
    /**
42
     * @param FactoryInterface $addressFactory
43
     * @param CountryNameConverterInterface $countryNameConverter
44
     * @param RepositoryInterface $countryRepository
45
     */
46
    public function __construct(
47
        FactoryInterface $addressFactory,
48
        CountryNameConverterInterface $countryNameConverter,
49
        RepositoryInterface $countryRepository
50
    ) {
51
        $this->addressFactory = $addressFactory;
52
        $this->countryNameConverter = $countryNameConverter;
53
        $this->countryRepository = $countryRepository;
54
    }
55
56
    /**
57
     * @Transform /^to "([^"]+)"$/
58
     */
59
    public function createNewAddress($countryName)
60
    {
61
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
62
63
        return $this->createAddress($countryCode);
64
    }
65
66
    /**
67
     * @Transform /^country "([^"]+)"$/
68
     */
69
    public function getCountryByName($countryName)
70
    {
71
        $countryCode = $this->countryNameConverter->convertToCode($countryName);
72
        $country = $this->countryRepository->findOneBy(['code' => $countryCode]);
73
74
        if (null === $country) {
75
            throw new \InvalidArgumentException(sprintf('Country with name %s does not exist', $countryName));
76
        }
77
78
        return $country;
79
    }
80
81
    /**
82
     * @param string $countryCode
83
     * @param string $firstName
84
     * @param string $lastName
85
     * @param string $city
86
     * @param string $street
87
     *
88
     * @return AddressInterface
89
     */
90
    private function createAddress(
91
        $countryCode = 'FR',
92
        $firstName = 'John',
93
        $lastName = 'Doe',
94
        $city = 'Ankh Morpork',
95
        $street = 'Frost Alley',
96
        $postCode = '90210'
97
    ) {
98
        /** @var AddressInterface $address */
99
        $address = $this->addressFactory->createNew();
100
        $address->setCountryCode($countryCode);
101
        $address->setFirstName($firstName);
102
        $address->setLastName($lastName);
103
        $address->setCity($city);
104
        $address->setStreet($street);
105
        $address->setPostcode($postCode);
106
107
        return $address;
108
    }
109
}
110