Completed
Push — master ( 2f9340...094334 )
by Kamil
17:24
created

ChannelContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
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\Core\Test\Services\DefaultStoreDataInterface;
16
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Symfony\Component\Intl\Intl;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 * @author Mateusz Zalewski <[email protected]>
24
 */
25
final class ChannelContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var DefaultStoreDataInterface
34
     */
35
    private $defaultFranceChannelFactory;
36
37
    /**
38
     * @var FactoryInterface
39
     */
40
    private $countryFactory;
41
42
    /**
43
     * @var RepositoryInterface
44
     */
45
    private $countryRepository;
46
47
    /**
48
     * @param SharedStorageInterface $sharedStorage
49
     * @param DefaultStoreDataInterface $defaultFranceChannelFactory
50
     * @param FactoryInterface $countryFactory
51
     * @param RepositoryInterface $countryRepository
52
     */
53
    public function __construct(
54
        SharedStorageInterface $sharedStorage,
55
        DefaultStoreDataInterface $defaultFranceChannelFactory,
56
        FactoryInterface $countryFactory,
57
        RepositoryInterface $countryRepository
58
    ) {
59
        $this->sharedStorage = $sharedStorage;
60
        $this->defaultFranceChannelFactory = $defaultFranceChannelFactory;
61
        $this->countryFactory = $countryFactory;
62
        $this->countryRepository = $countryRepository;
63
    }
64
65
    /**
66
     * @Given the store is operating on a single channel
67
     */
68
    public function thatStoreIsOperatingOnASingleChannel()
69
    {
70
        $defaultData = $this->defaultFranceChannelFactory->create();
71
        $this->sharedStorage->setClipboard($defaultData);
72
    }
73
74
    /**
75
     * @Given /^store ships to "([^"]*)"$/
76
     * @Given /^store ships to "([^"]*)" and "([^"]*)"$/
77
     * @Given /^store ships to "([^"]*)", "([^"]*)" and "([^"]*)"$/
78
     */
79
    public function storeShipsTo($country1, $country2 = null, $country3 = null)
80
    {
81
        $countries = [$this->getCountryCodeByEnglishCountryName($country1)];
82
83
        if (null !== $country2) {
84
            $countries[] = $this->getCountryCodeByEnglishCountryName($country2);
85
        }
86
87
        if (null !== $country3) {
88
            $countries[] = $this->getCountryCodeByEnglishCountryName($country3);
89
        }
90
91
        foreach ($countries as $country) {
92
            $this->createCountry($country);
93
        }
94
    }
95
96
    /**
97
     * @param string $code
98
     */
99
    private function createCountry($code)
100
    {
101
        $country = $this->countryFactory->createNew();
102
        $country->setCode($code);
103
104
        $this->countryRepository->add($country);
105
    }
106
107
    /**
108
     * @param string $name
109
     *
110
     * @return string
111
     *
112
     * @throws \InvalidArgumentException If name is not found in country code registry.
113
     */
114
    private function getCountryCodeByEnglishCountryName($name)
115
    {
116
        $names = Intl::getRegionBundle()->getCountryNames('en');
117
        $countryCode = array_search(trim($name), $names);
118
119
        if (null === $countryCode) {
120
            throw new \InvalidArgumentException(sprintf(
121
                'Country "%s" not found! Available names: %s.', $name, implode(', ', $names)
122
            ));
123
        }
124
125
        return $countryCode;
126
    }
127
}
128