Completed
Push — 1.0-symfony-3.3.13 ( 577127...a3487e )
by Kamil
50:10 queued 26:38
created

ZoneContext::setUpZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Setup;
15
16
use Behat\Behat\Context\Context;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Component\Addressing\Factory\ZoneFactoryInterface;
20
use Sylius\Component\Addressing\Model\CountryInterface;
21
use Sylius\Component\Addressing\Model\ProvinceInterface;
22
use Sylius\Component\Addressing\Model\Scope;
23
use Sylius\Component\Addressing\Model\ZoneInterface;
24
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
25
use Sylius\Component\Core\Model\ChannelInterface;
26
use Sylius\Component\Resource\Factory\FactoryInterface;
27
use Sylius\Component\Resource\Model\CodeAwareInterface;
28
use Sylius\Component\Resource\Repository\RepositoryInterface;
29
use Symfony\Component\Intl\Intl;
30
31
final class ZoneContext implements Context
32
{
33
    /**
34
     * @var SharedStorageInterface
35
     */
36
    private $sharedStorage;
37
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    private $zoneRepository;
42
43
    /**
44
     * @var ObjectManager
45
     */
46
    private $objectManager;
47
48
    /**
49
     * @var ZoneFactoryInterface
50
     */
51
    private $zoneFactory;
52
53
    /**
54
     * @var FactoryInterface
55
     */
56
    private $zoneMemberFactory;
57
58
    /**
59
     * @param SharedStorageInterface $sharedStorage
60
     * @param RepositoryInterface $zoneRepository
61
     * @param ObjectManager $objectManager
62
     * @param ZoneFactoryInterface $zoneFactory
63
     * @param FactoryInterface $zoneMemberFactory
64
     */
65
    public function __construct(
66
        SharedStorageInterface $sharedStorage,
67
        RepositoryInterface $zoneRepository,
68
        ObjectManager $objectManager,
69
        ZoneFactoryInterface $zoneFactory,
70
        FactoryInterface $zoneMemberFactory
71
    ) {
72
        $this->sharedStorage = $sharedStorage;
73
        $this->zoneRepository = $zoneRepository;
74
        $this->objectManager = $objectManager;
75
        $this->zoneFactory = $zoneFactory;
76
        $this->zoneMemberFactory = $zoneMemberFactory;
77
    }
78
79
    /**
80
     * @Given /^there is a zone "The Rest of the World" containing all other countries$/
81
     */
82
    public function thereIsAZoneTheRestOfTheWorldContainingAllOtherCountries()
83
    {
84
        $restOfWorldCountries = Intl::getRegionBundle()->getCountryNames('en');
85
        unset($restOfWorldCountries['US']);
86
87
        $zone = $this->zoneFactory->createWithMembers(array_keys($restOfWorldCountries));
88
        $zone->setType(ZoneInterface::TYPE_COUNTRY);
89
        $zone->setCode('RoW');
90
        $zone->setName('The Rest of the World');
91
92
        $this->zoneRepository->add($zone);
93
    }
94
95
    /**
96
     * @Given default tax zone is :zone
97
     */
98
    public function defaultTaxZoneIs(ZoneInterface $zone)
99
    {
100
        /** @var ChannelInterface $channel */
101
        $channel = $this->sharedStorage->get('channel');
102
        $channel->setDefaultTaxZone($zone);
103
104
        $this->objectManager->flush();
105
    }
106
107
    /**
108
     * @Given the store does not have any zones defined
109
     */
110
    public function theStoreDoesNotHaveAnyZonesDefined()
111
    {
112
        $zones = $this->zoneRepository->findAll();
113
114
        foreach ($zones as $zone) {
115
            $this->zoneRepository->remove($zone);
116
        }
117
    }
118
119
    /**
120
     * @Given the store has a zone :zoneName with code :code
121
     * @Given the store also has a zone :zoneName with code :code
122
     */
123
    public function theStoreHasAZoneWithCode($zoneName, $code)
124
    {
125
        $this->saveZone($this->setUpZone($zoneName, $code, Scope::ALL), 'zone');
126
    }
127
128
    /**
129
     * @Given the store has a :scope zone :zoneName with code :code
130
     */
131
    public function theStoreHasAScopedZoneWithCode($scope, $zoneName, $code)
132
    {
133
        $this->saveZone($this->setUpZone($zoneName, $code, $scope), $scope . '_zone');
134
    }
135
136
    /**
137
     * @Given /^(it)(?:| also) has the ("([^"]+)" country) member$/
138
     * @Given /^(this zone)(?:| also) has the ("([^"]+)" country) member$/
139
     */
140
    public function itHasTheCountryMemberAndTheCountryMember(
141
        ZoneInterface $zone,
142
        CountryInterface $country
143
    ) {
144
        $zone->setType(ZoneInterface::TYPE_COUNTRY);
145
        $zone->addMember($this->createZoneMember($country));
146
147
        $this->objectManager->flush();
148
    }
149
150
    /**
151
     * @Given /^(it) has the ("[^"]+" province) member$/
152
     * @Given /^(it) also has the ("[^"]+" province) member$/
153
     */
154
    public function itHasTheProvinceMemberAndTheProvinceMember(
155
        ZoneInterface $zone,
156
        ProvinceInterface $province
157
    ) {
158
        $zone->setType(ZoneInterface::TYPE_PROVINCE);
159
        $zone->addMember($this->createZoneMember($province));
160
161
        $this->objectManager->flush();
162
    }
163
164
    /**
165
     * @Given /^(it) has the (zone named "([^"]+)")$/
166
     * @Given /^(it) also has the (zone named "([^"]+)")$/
167
     */
168
    public function itHasTheZoneMemberAndTheZoneMember(
169
        ZoneInterface $parentZone,
170
        ZoneInterface $childZone
171
    ) {
172
        $parentZone->setType(ZoneInterface::TYPE_ZONE);
173
        $parentZone->addMember($this->createZoneMember($childZone));
174
175
        $this->objectManager->flush();
176
    }
177
178
    /**
179
     * @param CodeAwareInterface $zoneMember
180
     *
181
     * @return ZoneMemberInterface
182
     */
183
    private function createZoneMember(CodeAwareInterface $zoneMember)
184
    {
185
        $code = $zoneMember->getCode();
186
        /** @var ZoneMemberInterface $zoneMember */
187
        $zoneMember = $this->zoneMemberFactory->createNew();
188
        $zoneMember->setCode($code);
189
190
        return $zoneMember;
191
    }
192
193
    /**
194
     * @param string $zoneName
195
     * @param string $code
196
     * @param string $scope
197
     *
198
     * @return ZoneInterface
199
     */
200
    private function setUpZone($zoneName, $code, $scope)
201
    {
202
        $zone = $this->zoneFactory->createTyped(ZoneInterface::TYPE_ZONE);
203
        $zone->setCode($code);
204
        $zone->setName($zoneName);
205
        $zone->setScope($scope);
206
207
        return $zone;
208
    }
209
210
    /**
211
     * @param ZoneInterface $zone
212
     * @param string $key
213
     */
214
    private function saveZone($zone, $key)
215
    {
216
        $this->sharedStorage->set($key, $zone);
217
        $this->zoneRepository->add($zone);
218
    }
219
}
220