1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusBlogPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
8
|
|
|
use Faker\Factory; |
9
|
|
|
use Generator; |
10
|
|
|
use Odiseo\BlogBundle\EventListener\ArticleImageUploadListener; |
11
|
|
|
use Odiseo\BlogBundle\Model\ArticleCategoryInterface; |
12
|
|
|
use Odiseo\BlogBundle\Model\ArticleImageInterface; |
13
|
|
|
use Odiseo\SyliusBlogPlugin\Entity\ArticleCommentInterface; |
14
|
|
|
use Odiseo\SyliusBlogPlugin\Entity\ArticleInterface; |
15
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
16
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
17
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
18
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
19
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
20
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
21
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
22
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
24
|
|
|
use Symfony\Component\OptionsResolver\Options; |
25
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
26
|
|
|
|
27
|
|
|
final class BlogExampleFactory extends AbstractExampleFactory |
28
|
|
|
{ |
29
|
|
|
/** @var ObjectManager */ |
30
|
|
|
private $objectManager; |
31
|
|
|
|
32
|
|
|
/** @var FactoryInterface */ |
33
|
|
|
private $articleFactory; |
34
|
|
|
|
35
|
|
|
/** @var FactoryInterface */ |
36
|
|
|
private $articleCategoryFactory; |
37
|
|
|
|
38
|
|
|
/** @var FactoryInterface */ |
39
|
|
|
private $articleImageFactory; |
40
|
|
|
|
41
|
|
|
/** @var FactoryInterface */ |
42
|
|
|
private $articleCommentFactory; |
43
|
|
|
|
44
|
|
|
/** @var ChannelRepositoryInterface */ |
45
|
|
|
private $channelRepository; |
46
|
|
|
|
47
|
|
|
/** @var RepositoryInterface */ |
48
|
|
|
private $localeRepository; |
49
|
|
|
|
50
|
|
|
/** @var ArticleImageUploadListener */ |
51
|
|
|
private $imageUploader; |
52
|
|
|
|
53
|
|
|
/** @var FileLocatorInterface|null */ |
54
|
|
|
private $fileLocator; |
55
|
|
|
|
56
|
|
|
/** @var OptionsResolver */ |
57
|
|
|
private $optionsResolver; |
58
|
|
|
|
59
|
|
|
/** @var \Faker\Generator */ |
60
|
|
|
private $faker; |
61
|
|
|
|
62
|
|
|
public function __construct( |
63
|
|
|
ObjectManager $objectManager, |
64
|
|
|
FactoryInterface $articleFactory, |
65
|
|
|
FactoryInterface $articleCategoryFactory, |
66
|
|
|
FactoryInterface $articleImageFactory, |
67
|
|
|
FactoryInterface $articleCommentFactory, |
68
|
|
|
ChannelRepositoryInterface $channelRepository, |
69
|
|
|
RepositoryInterface $localeRepository, |
70
|
|
|
ArticleImageUploadListener $imageUploader, |
71
|
|
|
?FileLocatorInterface $fileLocator = null |
72
|
|
|
) { |
73
|
|
|
$this->objectManager = $objectManager; |
74
|
|
|
$this->articleFactory = $articleFactory; |
75
|
|
|
$this->articleCategoryFactory = $articleCategoryFactory; |
76
|
|
|
$this->articleImageFactory = $articleImageFactory; |
77
|
|
|
$this->articleCommentFactory = $articleCommentFactory; |
78
|
|
|
$this->channelRepository = $channelRepository; |
79
|
|
|
$this->localeRepository = $localeRepository; |
80
|
|
|
$this->imageUploader = $imageUploader; |
81
|
|
|
$this->fileLocator = $fileLocator; |
82
|
|
|
|
83
|
|
|
$this->faker = Factory::create(); |
84
|
|
|
$this->optionsResolver = new OptionsResolver(); |
85
|
|
|
|
86
|
|
|
$this->configureOptions($this->optionsResolver); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
93
|
|
|
{ |
94
|
|
|
$resolver |
95
|
|
|
->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3)) |
96
|
|
|
->setAllowedTypes('channels', 'array') |
97
|
|
|
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) |
98
|
|
|
|
99
|
|
|
->setDefault('image', function (Options $options): string { |
|
|
|
|
100
|
|
|
return __DIR__.'/../../Resources/fixtures/article/0'.rand(1, 4).'.png'; |
101
|
|
|
}) |
102
|
|
|
->setAllowedTypes('image', ['string']) |
103
|
|
|
; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function create(array $options = []): ArticleInterface |
110
|
|
|
{ |
111
|
|
|
$options = $this->optionsResolver->resolve($options); |
112
|
|
|
|
113
|
|
|
/** @var ArticleInterface $article */ |
114
|
|
|
$article = $this->articleFactory->createNew(); |
115
|
|
|
$article->setCode($this->faker->md5); |
116
|
|
|
$article->setEnabled(rand(1, 100) > 30); |
117
|
|
|
|
118
|
|
|
foreach ($options['channels'] as $channel) { |
119
|
|
|
$article->addChannel($channel); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->createArticleCategory($article); |
123
|
|
|
|
124
|
|
|
/** @var string $localeCode */ |
125
|
|
|
foreach ($this->getLocales() as $localeCode) { |
126
|
|
|
$article->setCurrentLocale($localeCode); |
127
|
|
|
$article->setFallbackLocale($localeCode); |
128
|
|
|
|
129
|
|
|
$article->getTranslation()->setTitle($this->faker->text(20)); |
|
|
|
|
130
|
|
|
$article->getTranslation()->setContent($this->faker->text(400)); |
|
|
|
|
131
|
|
|
$article->getTranslation()->setSlug($this->faker->slug); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** @var ArticleImageInterface $image */ |
135
|
|
|
$image = $this->articleImageFactory->createNew(); |
136
|
|
|
$image->setFile($this->createImage($options['image'])); |
137
|
|
|
|
138
|
|
|
$article->addImage($image); |
139
|
|
|
|
140
|
|
|
$this->imageUploader->uploadImages(new ResourceControllerEvent($article)); |
141
|
|
|
|
142
|
|
|
if (rand(0, 100) > 50) { |
143
|
|
|
$this->addArticleComments($article); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $article; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param ArticleInterface $article |
151
|
|
|
*/ |
152
|
|
|
private function createArticleCategory(ArticleInterface $article): void |
153
|
|
|
{ |
154
|
|
|
/** @var ArticleCategoryInterface $articleCategory */ |
155
|
|
|
$articleCategory = $this->articleCategoryFactory->createNew(); |
156
|
|
|
$articleCategory->setCode($this->faker->md5); |
157
|
|
|
$articleCategory->setEnabled(true); |
158
|
|
|
|
159
|
|
|
foreach ($this->getLocales() as $localeCode) { |
160
|
|
|
$articleCategory->setCurrentLocale($localeCode); |
161
|
|
|
$articleCategory->setFallbackLocale($localeCode); |
162
|
|
|
|
163
|
|
|
$articleCategory->getTranslation()->setTitle($this->faker->text(20)); |
164
|
|
|
$articleCategory->getTranslation()->setSlug($this->faker->slug); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$article->addCategory($articleCategory); |
168
|
|
|
|
169
|
|
|
$this->objectManager->persist($articleCategory); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ArticleInterface $article |
174
|
|
|
*/ |
175
|
|
|
private function addArticleComments(ArticleInterface $article): void |
176
|
|
|
{ |
177
|
|
|
for ($i = 0; $i < 8; $i++) { |
178
|
|
|
/** @var ArticleCommentInterface $comment */ |
179
|
|
|
$comment = $this->articleCommentFactory->createNew(); |
180
|
|
|
$comment->setName($this->faker->name); |
181
|
|
|
$comment->setEmail($this->faker->email); |
182
|
|
|
$comment->setComment($this->faker->text); |
183
|
|
|
$comment->setEnabled(rand(1, 100) > 30); |
184
|
|
|
|
185
|
|
|
$article->addComment($comment); |
186
|
|
|
|
187
|
|
|
$this->objectManager->persist($comment); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $imagePath |
193
|
|
|
* @return UploadedFile |
194
|
|
|
*/ |
195
|
|
|
private function createImage(string $imagePath): UploadedFile |
196
|
|
|
{ |
197
|
|
|
$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath); |
198
|
|
|
if(is_array($imagePath) && count($imagePath) > 0) |
199
|
|
|
$imagePath = $imagePath[0]; |
200
|
|
|
|
201
|
|
|
return new UploadedFile($imagePath, basename($imagePath)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return Generator |
206
|
|
|
*/ |
207
|
|
|
private function getLocales(): Generator |
208
|
|
|
{ |
209
|
|
|
/** @var LocaleInterface[] $locales */ |
210
|
|
|
$locales = $this->localeRepository->findAll(); |
211
|
|
|
foreach ($locales as $locale) { |
212
|
|
|
yield $locale->getCode(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.