Completed
Push — master ( 34008a...71b70e )
by Paweł
224:17 queued 208:23
created

GeographicalContext::storeShipsTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 3
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\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Addressing\Model\CountryInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Symfony\Component\Intl\Intl;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
final class GeographicalContext implements Context
24
{
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $countryFactory;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $countryRepository;
34
35
    /**
36
     * @param FactoryInterface $countryFactory
37
     * @param RepositoryInterface $countryRepository
38
     */
39
    public function __construct(
40
        FactoryInterface $countryFactory,
41
        RepositoryInterface $countryRepository
42
    ) {
43
        $this->countryFactory = $countryFactory;
44
        $this->countryRepository = $countryRepository;
45
    }
46
47
    /**
48
     * @Given /^store ships to "([^"]+)"$/
49
     * @Given /^store ships to "([^"]+)" and "([^"]+)"$/
50
     * @Given /^store ships to "([^"]+)", "([^"]+)" and "([^"]+)"$/
51
     */
52
    public function storeShipsTo($country1, $country2 = null, $country3 = null)
53
    {
54
        foreach ([$country1, $country2, $country3] as $countryName) {
55
            if (null === $countryName) {
56
                continue;
57
            }
58
59
            $this->createCountryNamed(trim($countryName));
60
        }
61
    }
62
63
    /**
64
     * @param string $name
65
     */
66
    private function createCountryNamed($name)
67
    {
68
        /** @var CountryInterface $country */
69
        $country = $this->countryFactory->createNew();
70
        $country->setCode($this->getCountryCodeByEnglishCountryName($name));
71
72
        $this->countryRepository->add($country);
73
    }
74
75
    /**
76
     * @param string $name
77
     *
78
     * @return string
79
     *
80
     * @throws \InvalidArgumentException If name is not found in country code registry.
81
     */
82
    private function getCountryCodeByEnglishCountryName($name)
83
    {
84
        $names = Intl::getRegionBundle()->getCountryNames('en');
85
        $countryCode = array_search($name, $names, true);
86
87
        if (null === $countryCode) {
88
            throw new \InvalidArgumentException(sprintf(
89
                'Country "%s" not found! Available names: %s.', $name, implode(', ', $names)
90
            ));
91
        }
92
93
        return $countryCode;
94
    }
95
}
96