Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

LocaleContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 168
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A theStoreHasLocale() 0 6 1
A theStoreHasDisabledLocale() 0 7 1
A theStoreDoesNotHaveLocale() 0 8 2
A thatChannelAllowsToShopUsingAndLocales() 0 12 3
A itUsesTheLocaleByDefault() 0 11 1
A createLocale() 0 8 1
A provideLocale() 0 12 2
A saveLocale() 0 5 1
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\Collections\ArrayCollection;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Behat\Service\SharedStorageInterface;
18
use Sylius\Component\Core\Model\ChannelInterface;
19
use Sylius\Component\Locale\Converter\LocaleConverterInterface;
20
use Sylius\Component\Locale\Model\LocaleInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 */
27
final class LocaleContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $localeFactory;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $localeRepository;
43
44
    /**
45
     * @var ObjectManager
46
     */
47
    private $localeManager;
48
49
    /**
50
     * @var ObjectManager
51
     */
52
    private $channelManager;
53
54
    /**
55
     * @var LocaleConverterInterface
56
     */
57
    private $localeConverter;
58
59
    /**
60
     * @param SharedStorageInterface $sharedStorage
61
     * @param LocaleConverterInterface $localeConverter
62
     * @param FactoryInterface $localeFactory
63
     * @param RepositoryInterface $localeRepository
64
     * @param ObjectManager $localeManager
65
     * @param ObjectManager $channelManager
66
     */
67
    public function __construct(
68
        SharedStorageInterface $sharedStorage,
69
        LocaleConverterInterface $localeConverter,
70
        FactoryInterface $localeFactory,
71
        RepositoryInterface $localeRepository,
72
        ObjectManager $localeManager,
73
        ObjectManager $channelManager
74
    ) {
75
        $this->sharedStorage = $sharedStorage;
76
        $this->localeConverter = $localeConverter;
77
        $this->localeFactory = $localeFactory;
78
        $this->localeRepository = $localeRepository;
79
        $this->localeManager = $localeManager;
80
        $this->channelManager = $channelManager;
81
    }
82
83
    /**
84
     * @Given the store has locale :localeCode
85
     * @Given the store is( also) available in :localeCode
86
     * @Given the locale :localeCode is enabled
87
     */
88
    public function theStoreHasLocale($localeCode)
89
    {
90
        $locale = $this->provideLocale($localeCode);
91
92
        $this->saveLocale($locale);
93
    }
94
95
    /**
96
     * @Given the store has disabled locale :localeCode
97
     * @Given the locale :localeCode is disabled (as well)
98
     * @Given the locale :localeCode gets disabled
99
     * @Given language :localeCode is disabled
100
     */
101
    public function theStoreHasDisabledLocale($localeCode)
102
    {
103
        $locale = $this->provideLocale($localeCode);
104
        $locale->disable();
105
106
        $this->saveLocale($locale);
107
    }
108
109
    /**
110
     * @Given the locale :localeCode does not exist in the store
111
     */
112
    public function theStoreDoesNotHaveLocale($localeCode)
113
    {
114
        /** @var LocaleInterface $locale */
115
        $locale = $this->localeRepository->findOneBy(['code' => $localeCode]);
116
        if (null !== $locale) {
117
            $this->localeRepository->remove($locale);
118
        }
119
    }
120
121
    /**
122
     * @Given /^(that channel) allows to shop using the "([^"]+)" locale$/
123
     * @Given /^(that channel) allows to shop using "([^"]+)" and "([^"]+)" locales$/
124
     * @Given /^(that channel) allows to shop using "([^"]+)", "([^"]+)" and "([^"]+)" locales$/
125
     */
126
    public function thatChannelAllowsToShopUsingAndLocales(ChannelInterface $channel, ...$localesNames)
127
    {
128
        foreach ($channel->getLocales() as $locale) {
129
            $channel->removeLocale($locale);
130
        }
131
132
        foreach ($localesNames as $localeName) {
133
            $channel->addLocale($this->provideLocale($this->localeConverter->convertNameToCode($localeName)));
134
        }
135
136
        $this->channelManager->flush();
137
    }
138
139
    /**
140
     * @Given /^(it) uses the "([^"]+)" locale by default$/
141
     */
142
    public function itUsesTheLocaleByDefault(ChannelInterface $channel, $localeName)
143
    {
144
        $locale = $this->provideLocale($this->localeConverter->convertNameToCode($localeName));
145
146
        $this->localeManager->flush();
147
148
        $channel->addLocale($locale);
149
        $channel->setDefaultLocale($locale);
150
151
        $this->channelManager->flush();
152
    }
153
154
    /**
155
     * @param string $localeCode
156
     *
157
     * @return LocaleInterface
158
     */
159
    private function createLocale($localeCode)
160
    {
161
        /** @var LocaleInterface $locale */
162
        $locale = $this->localeFactory->createNew();
163
        $locale->setCode($localeCode);
164
165
        return $locale;
166
    }
167
168
    /**
169
     * @param string $localeCode
170
     *
171
     * @return LocaleInterface
172
     */
173
    private function provideLocale($localeCode)
174
    {
175
        $locale = $this->localeRepository->findOneBy(['code' => $localeCode]);
176
        if (null === $locale) {
177
            /** @var LocaleInterface $locale */
178
            $locale = $this->createLocale($localeCode);
179
180
            $this->localeRepository->add($locale);
181
        }
182
183
        return $locale;
184
    }
185
186
    /**
187
     * @param LocaleInterface $locale
188
     */
189
    private function saveLocale(LocaleInterface $locale)
190
    {
191
        $this->sharedStorage->set('locale', $locale);
192
        $this->localeRepository->add($locale);
193
    }
194
}
195