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
|
|
|
|