Completed
Push — master ( 6999e1...b703c9 )
by Kamil
23:47
created

LocaleContext::itUsesTheLocaleByDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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