|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Odiseo\SyliusVendorPlugin\Fixture; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
use Faker\Factory; |
|
7
|
|
|
use Odiseo\SyliusVendorPlugin\Model\ChannelInterface; |
|
8
|
|
|
use Odiseo\SyliusVendorPlugin\Model\VendorInterface; |
|
9
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
|
10
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
|
12
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
13
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
15
|
|
|
use Symfony\Component\Finder\Finder; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
18
|
|
|
|
|
19
|
|
|
class VendorFixture extends AbstractFixture |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ObjectManager |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $objectManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var FactoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $vendorFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $vendorRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ChannelRepositoryInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $channelRepository; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var RepositoryInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $localeRepository; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Faker\Generator |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $faker; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var OptionsResolver |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $optionsResolver; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param ObjectManager $objectManager |
|
58
|
|
|
* @param FactoryInterface $vendorFactory |
|
59
|
|
|
* @param RepositoryInterface $vendorRepository |
|
60
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
|
61
|
|
|
* @param RepositoryInterface $localeRepository |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct( |
|
64
|
|
|
ObjectManager $objectManager, |
|
65
|
|
|
FactoryInterface $vendorFactory, |
|
66
|
|
|
RepositoryInterface $vendorRepository, |
|
67
|
|
|
ChannelRepositoryInterface $channelRepository, |
|
68
|
|
|
RepositoryInterface $localeRepository |
|
69
|
|
|
) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->objectManager = $objectManager; |
|
72
|
|
|
$this->vendorFactory = $vendorFactory; |
|
73
|
|
|
$this->vendorRepository = $vendorRepository; |
|
74
|
|
|
$this->channelRepository = $channelRepository; |
|
75
|
|
|
$this->localeRepository = $localeRepository; |
|
76
|
|
|
|
|
77
|
|
|
$this->faker = Factory::create(); |
|
78
|
|
|
$this->optionsResolver = |
|
79
|
|
|
(new OptionsResolver()) |
|
80
|
|
|
->setRequired('vendors_per_channel') |
|
81
|
|
|
->setAllowedTypes('vendors_per_channel', 'int') |
|
82
|
|
|
; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function load(array $options): void |
|
89
|
|
|
{ |
|
90
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
91
|
|
|
|
|
92
|
|
|
$channels = $this->channelRepository->findAll(); |
|
93
|
|
|
|
|
94
|
|
|
/** @var ChannelInterface $channel */ |
|
95
|
|
|
foreach($channels as $channel) |
|
96
|
|
|
{ |
|
97
|
|
|
$imageIndex = 1; |
|
98
|
|
|
for($i=1; $i <= $options['vendors_per_channel']; $i++) |
|
99
|
|
|
{ |
|
100
|
|
|
/** @var VendorInterface $vendor */ |
|
101
|
|
|
$vendor = $this->vendorFactory->createNew(); |
|
102
|
|
|
|
|
103
|
|
|
$vendor->setName($this->faker->company); |
|
104
|
|
|
$vendor->setEmail($this->faker->companyEmail); |
|
105
|
|
|
$vendor->addChannel($channel); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($this->getLocales() as $localeCode) { |
|
108
|
|
|
$vendor->setCurrentLocale($localeCode); |
|
109
|
|
|
$vendor->setFallbackLocale($localeCode); |
|
110
|
|
|
|
|
111
|
|
|
$vendor->setDescription($this->faker->text); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$imageFinder = new Finder(); |
|
115
|
|
|
$imagesPath = __DIR__ . '/../Resources/fixtures/vendor'; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($imageFinder->files()->in($imagesPath)->name('0'.$imageIndex.'.png') as $img) |
|
118
|
|
|
{ |
|
119
|
|
|
$file = new UploadedFile($img->getRealPath(), $img->getFilename()); |
|
120
|
|
|
$vendor->setLogoFile($file); |
|
121
|
|
|
} |
|
122
|
|
|
$imageIndex = $imageIndex>=4?1:$imageIndex+1; |
|
123
|
|
|
|
|
124
|
|
|
$this->objectManager->persist($vendor); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->objectManager->flush(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getLocales() |
|
135
|
|
|
{ |
|
136
|
|
|
/** @var LocaleInterface[] $locales */ |
|
137
|
|
|
$locales = $this->localeRepository->findAll(); |
|
138
|
|
|
foreach ($locales as $locale) { |
|
139
|
|
|
yield $locale->getCode(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void |
|
147
|
|
|
{ |
|
148
|
|
|
$optionsNode |
|
149
|
|
|
->children() |
|
150
|
|
|
->integerNode('vendors_per_channel')->isRequired()->min(1)->end() |
|
151
|
|
|
; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getName(): string |
|
158
|
|
|
{ |
|
159
|
|
|
return 'vendor'; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|