Completed
Push — core-test-reorganisation ( 880d88 )
by Kamil
44:19 queued 07:25
created

DefaultChannelFactory::provideLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
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\Service;
13
14
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
15
use Sylius\Component\Core\Model\ChannelInterface;
16
use Sylius\Component\Currency\Model\CurrencyInterface;
17
use Sylius\Component\Locale\Model\LocaleInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class DefaultChannelFactory implements DefaultChannelFactoryInterface
25
{
26
    const DEFAULT_CHANNEL_NAME = 'Default';
27
    const DEFAULT_CHANNEL_CODE = 'DEFAULT';
28
    const DEFAULT_CHANNEL_CURRENCY = 'USD';
29
30
    /**
31
     * @var ChannelFactoryInterface
32
     */
33
    private $channelFactory;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $currencyFactory;
39
40
    /**
41
     * @var FactoryInterface
42
     */
43
    private $localeFactory;
44
45
    /**
46
     * @var RepositoryInterface
47
     */
48
    private $channelRepository;
49
50
    /**
51
     * @var RepositoryInterface
52
     */
53
    private $currencyRepository;
54
55
    /**
56
     * @var RepositoryInterface
57
     */
58
    private $localeRepository;
59
60
    /**
61
     * @var string
62
     */
63
    private $defaultLocaleCode;
64
65
    /**
66
     * @param ChannelFactoryInterface $channelFactory
67
     * @param FactoryInterface $currencyFactory
68
     * @param FactoryInterface $localeFactory
69
     * @param RepositoryInterface $channelRepository
70
     * @param RepositoryInterface $currencyRepository
71
     * @param RepositoryInterface $localeRepository
72
     * @param string $defaultLocaleCode
73
     */
74
    public function __construct(
75
        ChannelFactoryInterface $channelFactory,
76
        FactoryInterface $currencyFactory,
77
        FactoryInterface $localeFactory,
78
        RepositoryInterface $channelRepository,
79
        RepositoryInterface $currencyRepository,
80
        RepositoryInterface $localeRepository,
81
        $defaultLocaleCode
82
    ) {
83
        $this->channelFactory = $channelFactory;
84
        $this->currencyFactory = $currencyFactory;
85
        $this->localeFactory = $localeFactory;
86
        $this->channelRepository = $channelRepository;
87
        $this->currencyRepository = $currencyRepository;
88
        $this->localeRepository = $localeRepository;
89
        $this->defaultLocaleCode = $defaultLocaleCode;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function create($code = null, $name = null, $currencyCode = null)
96
    {
97
        $currency = $this->provideCurrency($currencyCode);
98
        $locale = $this->provideLocale();
99
100
        /** @var ChannelInterface $channel */
101
        $channel = $this->channelFactory->createNamed($name ?: self::DEFAULT_CHANNEL_NAME);
102
        $channel->setCode($code ?: self::DEFAULT_CHANNEL_CODE);
103
        $channel->setTaxCalculationStrategy('order_items_based');
104
105
        $channel->addCurrency($currency);
106
        $channel->setBaseCurrency($currency);
107
108
        $channel->addLocale($locale);
109
        $channel->setDefaultLocale($locale);
110
111
        $this->channelRepository->add($channel);
112
113
        return [
114
            'channel' => $channel,
115
            'currency' => $currency,
116
            'locale' => $locale,
117
        ];
118
    }
119
120
    /**
121
     * @param string|null $currencyCode
122
     *
123
     * @return CurrencyInterface
124
     */
125
    private function provideCurrency($currencyCode = null)
126
    {
127
        $currencyCode = (null === $currencyCode) ? self::DEFAULT_CHANNEL_CURRENCY : $currencyCode;
128
129
        /** @var CurrencyInterface $currency */
130
        $currency = $this->currencyRepository->findOneBy(['code' => $currencyCode]);
131
132
        if (null === $currency) {
133
            $currency = $this->currencyFactory->createNew();
134
            $currency->setCode($currencyCode);
135
136
            $this->currencyRepository->add($currency);
137
        }
138
139
        return $currency;
140
    }
141
142
    /**
143
     * @return LocaleInterface
144
     */
145
    private function provideLocale()
146
    {
147
        /** @var LocaleInterface $locale */
148
        $locale = $this->localeRepository->findOneBy(['code' => $this->defaultLocaleCode]);
149
150
        if (null === $locale) {
151
            $locale = $this->localeFactory->createNew();
152
            $locale->setCode($this->defaultLocaleCode);
153
154
            $this->localeRepository->add($locale);
155
        }
156
157
        return $locale;
158
    }
159
}
160