|
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\Addressing\Factory\ZoneFactoryInterface; |
|
15
|
|
|
use Sylius\Component\Addressing\Model\CountryInterface; |
|
16
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
|
17
|
|
|
use Sylius\Component\Channel\Factory\ChannelFactoryInterface; |
|
18
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
19
|
|
|
use Sylius\Component\Currency\Model\CurrencyInterface; |
|
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 DefaultUnitedStatesChannelFactory implements DefaultChannelFactoryInterface |
|
28
|
|
|
{ |
|
29
|
|
|
const DEFAULT_CHANNEL_CODE = 'WEB-US'; |
|
30
|
|
|
const DEFAULT_COUNTRY_CODE = 'US'; |
|
31
|
|
|
const DEFAULT_ZONE_CODE = 'US'; |
|
32
|
|
|
const DEFAULT_CURRENCY_CODE = 'USD'; |
|
33
|
|
|
const DEFAULT_ZONE_NAME = 'United States'; |
|
34
|
|
|
const DEFAULT_CHANNEL_NAME = 'United States'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RepositoryInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $channelRepository; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var RepositoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $countryRepository; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var RepositoryInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $currencyRepository; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var RepositoryInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $localeRepository; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var RepositoryInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $zoneRepository; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var ChannelFactoryInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
private $channelFactory; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var FactoryInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
private $countryFactory; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var FactoryInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
private $currencyFactory; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var FactoryInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
private $localeFactory; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var ZoneFactoryInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
private $zoneFactory; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var string |
|
88
|
|
|
*/ |
|
89
|
|
|
private $defaultLocaleCode; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param RepositoryInterface $channelRepository |
|
93
|
|
|
* @param RepositoryInterface $countryRepository |
|
94
|
|
|
* @param RepositoryInterface $currencyRepository |
|
95
|
|
|
* @param RepositoryInterface $localeRepository |
|
96
|
|
|
* @param RepositoryInterface $zoneRepository |
|
97
|
|
|
* @param ChannelFactoryInterface $channelFactory |
|
98
|
|
|
* @param FactoryInterface $countryFactory |
|
99
|
|
|
* @param FactoryInterface $currencyFactory |
|
100
|
|
|
* @param FactoryInterface $localeFactory |
|
101
|
|
|
* @param ZoneFactoryInterface $zoneFactory |
|
102
|
|
|
* @param string $defaultLocaleCode |
|
103
|
|
|
*/ |
|
104
|
|
|
public function __construct( |
|
105
|
|
|
RepositoryInterface $channelRepository, |
|
106
|
|
|
RepositoryInterface $countryRepository, |
|
107
|
|
|
RepositoryInterface $currencyRepository, |
|
108
|
|
|
RepositoryInterface $localeRepository, |
|
109
|
|
|
RepositoryInterface $zoneRepository, |
|
110
|
|
|
ChannelFactoryInterface $channelFactory, |
|
111
|
|
|
FactoryInterface $countryFactory, |
|
112
|
|
|
FactoryInterface $currencyFactory, |
|
113
|
|
|
FactoryInterface $localeFactory, |
|
114
|
|
|
ZoneFactoryInterface $zoneFactory, |
|
115
|
|
|
$defaultLocaleCode |
|
116
|
|
|
) { |
|
117
|
|
|
$this->channelRepository = $channelRepository; |
|
118
|
|
|
$this->countryRepository = $countryRepository; |
|
119
|
|
|
$this->currencyRepository = $currencyRepository; |
|
120
|
|
|
$this->localeRepository = $localeRepository; |
|
121
|
|
|
$this->zoneRepository = $zoneRepository; |
|
122
|
|
|
$this->channelFactory = $channelFactory; |
|
123
|
|
|
$this->countryFactory = $countryFactory; |
|
124
|
|
|
$this->currencyFactory = $currencyFactory; |
|
125
|
|
|
$this->localeFactory = $localeFactory; |
|
126
|
|
|
$this->zoneFactory = $zoneFactory; |
|
127
|
|
|
$this->defaultLocaleCode = $defaultLocaleCode; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function create($code = null, $name = null, $currencyCode = null) |
|
134
|
|
|
{ |
|
135
|
|
|
$currency = $this->provideCurrency($currencyCode); |
|
136
|
|
|
$locale = $this->provideLocale(); |
|
137
|
|
|
|
|
138
|
|
|
$channel = $this->createChannel($code ?: self::DEFAULT_CHANNEL_CODE, $name ?: self::DEFAULT_CHANNEL_NAME); |
|
139
|
|
|
$channel->addCurrency($currency); |
|
140
|
|
|
$channel->setBaseCurrency($currency); |
|
141
|
|
|
$channel->addLocale($locale); |
|
142
|
|
|
$channel->setDefaultLocale($locale); |
|
143
|
|
|
$channel->setTaxCalculationStrategy('order_items_based'); |
|
144
|
|
|
|
|
145
|
|
|
$defaultData['channel'] = $channel; |
|
|
|
|
|
|
146
|
|
|
$defaultData['country'] = $this->createCountry(); |
|
147
|
|
|
$defaultData['currency'] = $currency; |
|
148
|
|
|
$defaultData['locale'] = $locale; |
|
149
|
|
|
$defaultData['zone'] = $this->createZone(); |
|
150
|
|
|
|
|
151
|
|
|
$this->channelRepository->add($channel); |
|
152
|
|
|
$this->countryRepository->add($defaultData['country']); |
|
153
|
|
|
$this->zoneRepository->add($defaultData['zone']); |
|
154
|
|
|
|
|
155
|
|
|
return $defaultData; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return ChannelInterface |
|
160
|
|
|
*/ |
|
161
|
|
|
private function createChannel($code, $name) |
|
162
|
|
|
{ |
|
163
|
|
|
$channel = $this->channelFactory->createNamed($name); |
|
164
|
|
|
$channel->setCode($code); |
|
165
|
|
|
|
|
166
|
|
|
return $channel; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return CountryInterface |
|
171
|
|
|
*/ |
|
172
|
|
|
private function createCountry() |
|
173
|
|
|
{ |
|
174
|
|
|
/** @var CountryInterface $country */ |
|
175
|
|
|
$country = $this->countryFactory->createNew(); |
|
176
|
|
|
$country->setCode(self::DEFAULT_COUNTRY_CODE); |
|
177
|
|
|
|
|
178
|
|
|
return $country; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return CurrencyInterface |
|
183
|
|
|
*/ |
|
184
|
|
|
private function provideCurrency($currencyCode = null) |
|
185
|
|
|
{ |
|
186
|
|
|
$currencyCode = $currencyCode ?: self::DEFAULT_CURRENCY_CODE; |
|
187
|
|
|
|
|
188
|
|
|
/** @var CurrencyInterface $currency */ |
|
189
|
|
|
$currency = $this->currencyRepository->findOneBy(['code' => $currencyCode]); |
|
190
|
|
|
|
|
191
|
|
|
if (null === $currency) { |
|
192
|
|
|
$currency = $this->currencyFactory->createNew(); |
|
193
|
|
|
$currency->setCode($currencyCode); |
|
194
|
|
|
|
|
195
|
|
|
$this->currencyRepository->add($currency); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $currency; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return LocaleInterface |
|
203
|
|
|
*/ |
|
204
|
|
|
private function provideLocale() |
|
205
|
|
|
{ |
|
206
|
|
|
/** @var LocaleInterface $locale */ |
|
207
|
|
|
$locale = $this->localeRepository->findOneBy(['code' => $this->defaultLocaleCode]); |
|
208
|
|
|
|
|
209
|
|
|
if (null === $locale) { |
|
210
|
|
|
$locale = $this->localeFactory->createNew(); |
|
211
|
|
|
$locale->setCode($this->defaultLocaleCode); |
|
212
|
|
|
|
|
213
|
|
|
$this->localeRepository->add($locale); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $locale; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return ZoneInterface |
|
221
|
|
|
*/ |
|
222
|
|
|
private function createZone() |
|
223
|
|
|
{ |
|
224
|
|
|
/** @var ZoneInterface $zone */ |
|
225
|
|
|
$zone = $this->zoneFactory->createWithMembers([self::DEFAULT_ZONE_CODE]); |
|
226
|
|
|
$zone->setCode(self::DEFAULT_ZONE_CODE); |
|
227
|
|
|
$zone->setName(self::DEFAULT_ZONE_NAME); |
|
228
|
|
|
$zone->setType(ZoneInterface::TYPE_COUNTRY); |
|
229
|
|
|
|
|
230
|
|
|
return $zone; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.