Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ZoneContext::theStoreHasAZoneWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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