1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusBannerPlugin\Fixture; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
8
|
|
|
use Faker\Factory; |
9
|
|
|
use Odiseo\SyliusBannerPlugin\Entity\BannerInterface; |
10
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
11
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface; |
12
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
13
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
14
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
16
|
|
|
use Sylius\Component\Taxonomy\Model\TaxonInterface; |
17
|
|
|
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
18
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
19
|
|
|
use Symfony\Component\Finder\Finder; |
20
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
|
23
|
|
|
final class BannerFixture extends AbstractFixture |
24
|
|
|
{ |
25
|
|
|
/** @var ObjectManager */ |
26
|
|
|
private $objectManager; |
27
|
|
|
|
28
|
|
|
/** @var FactoryInterface */ |
29
|
|
|
private $bannerFactory; |
30
|
|
|
|
31
|
|
|
/** @var RepositoryInterface */ |
32
|
|
|
private $bannerRepository; |
33
|
|
|
|
34
|
|
|
/** @var ChannelRepositoryInterface */ |
35
|
|
|
private $channelRepository; |
36
|
|
|
|
37
|
|
|
/** @var RepositoryInterface */ |
38
|
|
|
private $localeRepository; |
39
|
|
|
|
40
|
|
|
/** @var FactoryInterface */ |
41
|
|
|
private $taxonFactory; |
42
|
|
|
|
43
|
|
|
/** @var TaxonRepositoryInterface */ |
44
|
|
|
private $taxonRepository; |
45
|
|
|
|
46
|
|
|
/** @var \Faker\Generator */ |
47
|
|
|
private $faker; |
48
|
|
|
|
49
|
|
|
/** @var OptionsResolver */ |
50
|
|
|
private $optionsResolver; |
51
|
|
|
|
52
|
|
|
public function __construct( |
53
|
|
|
ObjectManager $objectManager, |
54
|
|
|
FactoryInterface $bannerFactory, |
55
|
|
|
RepositoryInterface $bannerRepository, |
56
|
|
|
ChannelRepositoryInterface $channelRepository, |
57
|
|
|
RepositoryInterface $localeRepository, |
58
|
|
|
FactoryInterface $taxonFactory, |
59
|
|
|
TaxonRepositoryInterface $taxonRepository |
60
|
|
|
) { |
61
|
|
|
$this->objectManager = $objectManager; |
62
|
|
|
$this->bannerFactory = $bannerFactory; |
63
|
|
|
$this->bannerRepository = $bannerRepository; |
64
|
|
|
$this->channelRepository = $channelRepository; |
65
|
|
|
$this->localeRepository = $localeRepository; |
66
|
|
|
$this->taxonFactory = $taxonFactory; |
67
|
|
|
$this->taxonRepository = $taxonRepository; |
68
|
|
|
|
69
|
|
|
$this->faker = Factory::create(); |
70
|
|
|
$this->optionsResolver = |
71
|
|
|
(new OptionsResolver()) |
72
|
|
|
->setRequired('banners_per_channel') |
73
|
|
|
->setAllowedTypes('banners_per_channel', 'int') |
74
|
|
|
; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function load(array $options): void |
81
|
|
|
{ |
82
|
|
|
$options = $this->optionsResolver->resolve($options); |
83
|
|
|
|
84
|
|
|
/** @var TaxonInterface $taxonBanners */ |
85
|
|
|
$taxonBanners = $this->taxonFactory->createNew(); |
86
|
|
|
$taxonBanners->setCode('banners'); |
87
|
|
|
foreach ($this->getLocales() as $localeCode) { |
88
|
|
|
$taxonBanners->setCurrentLocale($localeCode); |
89
|
|
|
$taxonBanners->setFallbackLocale($localeCode); |
90
|
|
|
|
91
|
|
|
$taxonBanners->setName('Banners'); |
92
|
|
|
$taxonBanners->setSlug('banners'); |
93
|
|
|
$taxonBanners->setDescription($this->faker->text); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var TaxonInterface $taxonHome */ |
97
|
|
|
$taxonHome = $this->taxonFactory->createNew(); |
98
|
|
|
$taxonHome->setCode('home'); |
99
|
|
|
$taxonHome->setParent($taxonBanners); |
100
|
|
|
foreach ($this->getLocales() as $localeCode) { |
101
|
|
|
$taxonHome->setCurrentLocale($localeCode); |
102
|
|
|
$taxonHome->setFallbackLocale($localeCode); |
103
|
|
|
|
104
|
|
|
$taxonHome->setName('Home'); |
105
|
|
|
$taxonHome->setSlug('banners/home'); |
106
|
|
|
$taxonHome->setDescription($this->faker->text); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->objectManager->persist($taxonBanners); |
110
|
|
|
$this->objectManager->persist($taxonHome); |
111
|
|
|
|
112
|
|
|
$channels = $this->channelRepository->findAll(); |
113
|
|
|
|
114
|
|
|
/** @var ChannelInterface $channel */ |
115
|
|
|
foreach ($channels as $channel) { |
116
|
|
|
/** @var BannerInterface $banner */ |
117
|
|
|
$banner = $this->bannerFactory->createNew(); |
118
|
|
|
|
119
|
|
|
$banner->setCode($this->faker->slug); |
120
|
|
|
$banner->addChannel($channel); |
121
|
|
|
$banner->addTaxon($taxonHome); |
122
|
|
|
|
123
|
|
|
foreach ($this->getLocales() as $localeCode) { |
124
|
|
|
$banner->setCurrentLocale($localeCode); |
125
|
|
|
$banner->setFallbackLocale($localeCode); |
126
|
|
|
|
127
|
|
|
$banner->setUrl($this->faker->url); |
128
|
|
|
|
129
|
|
|
$imageFinder = new Finder(); |
130
|
|
|
|
131
|
|
|
$imagesPath = __DIR__ . '/../Resources/fixtures/banner/images'; |
132
|
|
|
$mobileImagesPath = __DIR__ . '/../Resources/fixtures/banner/mobile-images'; |
133
|
|
|
|
134
|
|
|
foreach ($imageFinder->files()->in($imagesPath)->name('01.png') as $img) { |
135
|
|
|
/** @var string $path */ |
136
|
|
|
$path = $img->getRealPath(); |
137
|
|
|
/** @var string $filename */ |
138
|
|
|
$filename = $img->getFilename(); |
139
|
|
|
$file = new UploadedFile($path, $filename); |
140
|
|
|
$banner->setImageFile($file); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
foreach ($imageFinder->files()->in($mobileImagesPath)->name('01.png') as $img) { |
144
|
|
|
/** @var string $path */ |
145
|
|
|
$path = $img->getRealPath(); |
146
|
|
|
/** @var string $filename */ |
147
|
|
|
$filename = $img->getFilename(); |
148
|
|
|
$file = new UploadedFile($path, $filename); |
149
|
|
|
$banner->setMobileImageFile($file); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->objectManager->persist($banner); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @var ChannelInterface $channel */ |
157
|
|
|
foreach ($channels as $channel) { |
158
|
|
|
$imageIndex = 1; |
159
|
|
|
$mobileImageIndex = 1; |
160
|
|
|
for ($i=1; $i <= $options['banners_per_channel']; $i++) { |
161
|
|
|
/** @var BannerInterface $banner */ |
162
|
|
|
$banner = $this->bannerFactory->createNew(); |
163
|
|
|
|
164
|
|
|
$banner->setCode($this->faker->slug); |
165
|
|
|
$banner->addChannel($channel); |
166
|
|
|
|
167
|
|
|
foreach ($this->getLocales() as $localeCode) { |
168
|
|
|
$banner->setCurrentLocale($localeCode); |
169
|
|
|
$banner->setFallbackLocale($localeCode); |
170
|
|
|
|
171
|
|
|
$banner->setUrl($this->faker->url); |
172
|
|
|
|
173
|
|
|
$imageFinder = new Finder(); |
174
|
|
|
|
175
|
|
|
$imagesPath = __DIR__ . '/../Resources/fixtures/banner/images'; |
176
|
|
|
$mobileImagesPath = __DIR__ . '/../Resources/fixtures/banner/mobile-images'; |
177
|
|
|
|
178
|
|
|
foreach ($imageFinder->files()->in($imagesPath)->name('0'.$imageIndex.'.png') as $img) { |
179
|
|
|
/** @var string $path */ |
180
|
|
|
$path = $img->getRealPath(); |
181
|
|
|
/** @var string $filename */ |
182
|
|
|
$filename = $img->getFilename(); |
183
|
|
|
$file = new UploadedFile($path, $filename); |
184
|
|
|
$banner->setImageFile($file); |
185
|
|
|
} |
186
|
|
|
$imageIndex = $imageIndex>=4?1:$imageIndex+1; |
187
|
|
|
|
188
|
|
|
foreach ($imageFinder->files()->in($mobileImagesPath)->name('0'.$mobileImageIndex.'.png') as $img) { |
189
|
|
|
/** @var string $path */ |
190
|
|
|
$path = $img->getRealPath(); |
191
|
|
|
/** @var string $filename */ |
192
|
|
|
$filename = $img->getFilename(); |
193
|
|
|
$file = new UploadedFile($path, $filename); |
194
|
|
|
$banner->setMobileImageFile($file); |
195
|
|
|
} |
196
|
|
|
$mobileImageIndex = $mobileImageIndex>=4?1:$mobileImageIndex+1; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->objectManager->persist($banner); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->objectManager->flush(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return \Generator |
208
|
|
|
*/ |
209
|
|
|
private function getLocales(): \Generator |
210
|
|
|
{ |
211
|
|
|
/** @var LocaleInterface[] $locales */ |
212
|
|
|
$locales = $this->localeRepository->findAll(); |
213
|
|
|
foreach ($locales as $locale) { |
214
|
|
|
yield $locale->getCode(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void |
222
|
|
|
{ |
223
|
|
|
$optionsNode |
224
|
|
|
->children() |
225
|
|
|
->integerNode('banners_per_channel')->isRequired()->min(1)->end() |
226
|
|
|
; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function getName(): string |
233
|
|
|
{ |
234
|
|
|
return 'banner'; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|