|
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\Bundle\CoreBundle\Fixture\Factory; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
|
15
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
|
16
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
17
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
|
18
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
19
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
|
20
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
|
21
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
22
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
23
|
|
|
use Sylius\Component\Shipping\Calculator\DefaultCalculators; |
|
24
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
|
25
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
26
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Kamil Kokot <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class ShippingMethodExampleFactory implements ExampleFactoryInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var FactoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $shippingMethodFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var RepositoryInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $localeRepository; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ChannelRepositoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $channelRepository; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Faker\Generator |
|
50
|
|
|
*/ |
|
51
|
|
|
private $faker; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var OptionsResolver |
|
55
|
|
|
*/ |
|
56
|
|
|
private $optionsResolver; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param FactoryInterface $shippingMethodFactory |
|
60
|
|
|
* @param RepositoryInterface $zoneRepository |
|
61
|
|
|
* @param RepositoryInterface $shippingCategoryRepository |
|
62
|
|
|
* @param RepositoryInterface $localeRepository |
|
63
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
FactoryInterface $shippingMethodFactory, |
|
|
|
|
|
|
67
|
|
|
RepositoryInterface $zoneRepository, |
|
68
|
|
|
RepositoryInterface $shippingCategoryRepository, |
|
|
|
|
|
|
69
|
|
|
RepositoryInterface $localeRepository, |
|
70
|
|
|
ChannelRepositoryInterface $channelRepository |
|
71
|
|
|
) { |
|
72
|
|
|
$this->shippingMethodFactory = $shippingMethodFactory; |
|
73
|
|
|
$this->localeRepository = $localeRepository; |
|
74
|
|
|
$this->channelRepository = $channelRepository; |
|
75
|
|
|
|
|
76
|
|
|
$this->faker = \Faker\Factory::create(); |
|
77
|
|
|
$this->optionsResolver = |
|
78
|
|
|
(new OptionsResolver()) |
|
79
|
|
|
->setDefault('code', function (Options $options) { |
|
80
|
|
|
return StringInflector::nameToCode($options['name']); |
|
81
|
|
|
}) |
|
82
|
|
|
->setDefault('name', function (Options $options) { |
|
|
|
|
|
|
83
|
|
|
return $this->faker->words(3, true); |
|
84
|
|
|
}) |
|
85
|
|
|
->setDefault('description', function (Options $options) { |
|
|
|
|
|
|
86
|
|
|
return $this->faker->sentence(); |
|
87
|
|
|
}) |
|
88
|
|
|
->setDefault('enabled', function (Options $options) { |
|
|
|
|
|
|
89
|
|
|
return $this->faker->boolean(90); |
|
90
|
|
|
}) |
|
91
|
|
|
->setAllowedTypes('enabled', 'bool') |
|
92
|
|
|
->setDefault('zone', LazyOption::randomOne($zoneRepository)) |
|
93
|
|
|
->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class]) |
|
94
|
|
|
->setNormalizer('zone', LazyOption::findOneBy($zoneRepository, 'code')) |
|
95
|
|
|
->setDefined('shipping_category') |
|
96
|
|
|
->setAllowedTypes('shipping_category', ['null', 'string', ShippingCategoryInterface::class]) |
|
97
|
|
|
->setNormalizer('shipping_category', LazyOption::findOneBy($shippingCategoryRepository, 'code')) |
|
98
|
|
|
->setDefault('calculator', function (Options $options) { |
|
99
|
|
|
$configuration = []; |
|
100
|
|
|
/** @var ChannelInterface $channel */ |
|
101
|
|
|
foreach ($options['channels'] as $channel) { |
|
102
|
|
|
$configuration[$channel->getCode()] = ['amount' => $this->faker->randomNumber(4)]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return [ |
|
106
|
|
|
'type' => DefaultCalculators::FLAT_RATE, |
|
107
|
|
|
'configuration' => $configuration, |
|
108
|
|
|
]; |
|
109
|
|
|
}) |
|
110
|
|
|
->setDefault('channels', LazyOption::all($channelRepository)) |
|
111
|
|
|
->setAllowedTypes('channels', 'array') |
|
112
|
|
|
->setNormalizer('channels', LazyOption::findBy($channelRepository, 'code')) |
|
113
|
|
|
; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function create(array $options = []) |
|
120
|
|
|
{ |
|
121
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
122
|
|
|
|
|
123
|
|
|
/** @var ShippingMethodInterface $shippingMethod */ |
|
124
|
|
|
$shippingMethod = $this->shippingMethodFactory->createNew(); |
|
125
|
|
|
$shippingMethod->setCode($options['code']); |
|
126
|
|
|
$shippingMethod->setEnabled($options['enabled']); |
|
127
|
|
|
$shippingMethod->setZone($options['zone']); |
|
128
|
|
|
$shippingMethod->setCalculator($options['calculator']['type']); |
|
129
|
|
|
$shippingMethod->setConfiguration($options['calculator']['configuration']); |
|
130
|
|
|
|
|
131
|
|
|
if (array_key_exists('shipping_category', $options)) { |
|
132
|
|
|
$shippingMethod->setCategory($options['shipping_category']); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
foreach ($this->getLocales() as $localeCode) { |
|
136
|
|
|
$shippingMethod->setCurrentLocale($localeCode); |
|
137
|
|
|
$shippingMethod->setFallbackLocale($localeCode); |
|
138
|
|
|
|
|
139
|
|
|
$shippingMethod->setName($options['name']); |
|
140
|
|
|
$shippingMethod->setDescription($options['description']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
foreach ($options['channels'] as $channel) { |
|
144
|
|
|
$shippingMethod->addChannel($channel); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $shippingMethod; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return array |
|
|
|
|
|
|
152
|
|
|
*/ |
|
153
|
|
|
private function getLocales() |
|
154
|
|
|
{ |
|
155
|
|
|
/** @var LocaleInterface[] $locales */ |
|
156
|
|
|
$locales = $this->localeRepository->findAll(); |
|
157
|
|
|
foreach ($locales as $locale) { |
|
158
|
|
|
yield $locale->getCode(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.