1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusBlogPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Faker\Factory; |
8
|
|
|
use Odiseo\BlogBundle\Model\ImageInterface; |
9
|
|
|
use Odiseo\BlogBundle\Uploader\ImageUploaderInterface; |
10
|
|
|
use Odiseo\SyliusBlogPlugin\Entity\ArticleCommentInterface; |
11
|
|
|
use Odiseo\SyliusBlogPlugin\Entity\ArticleInterface; |
12
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
13
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
14
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
15
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
16
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
17
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
18
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
19
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
21
|
|
|
use Symfony\Component\OptionsResolver\Options; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
final class BlogArticleExampleFactory extends AbstractExampleFactory |
25
|
|
|
{ |
26
|
|
|
/** @var FactoryInterface */ |
27
|
|
|
private $articleFactory; |
28
|
|
|
|
29
|
|
|
/** @var FactoryInterface */ |
30
|
|
|
private $articleImageFactory; |
31
|
|
|
|
32
|
|
|
/** @var FactoryInterface */ |
33
|
|
|
private $articleCommentFactory; |
34
|
|
|
|
35
|
|
|
/** @var ChannelRepositoryInterface */ |
36
|
|
|
private $channelRepository; |
37
|
|
|
|
38
|
|
|
/** @var RepositoryInterface */ |
39
|
|
|
private $articleCategoryRepository; |
40
|
|
|
|
41
|
|
|
/** @var RepositoryInterface */ |
42
|
|
|
private $localeRepository; |
43
|
|
|
|
44
|
|
|
/** @var ImageUploaderInterface */ |
45
|
|
|
private $imageUploader; |
46
|
|
|
|
47
|
|
|
/** @var FileLocatorInterface|null */ |
48
|
|
|
private $fileLocator; |
49
|
|
|
|
50
|
|
|
/** @var \Faker\Generator */ |
51
|
|
|
private $faker; |
52
|
|
|
|
53
|
|
|
/** @var OptionsResolver */ |
54
|
|
|
private $optionsResolver; |
55
|
|
|
|
56
|
|
|
public function __construct( |
57
|
|
|
FactoryInterface $articleFactory, |
58
|
|
|
FactoryInterface $articleImageFactory, |
59
|
|
|
FactoryInterface $articleCommentFactory, |
60
|
|
|
ChannelRepositoryInterface $channelRepository, |
61
|
|
|
RepositoryInterface $articleCategoryRepository, |
62
|
|
|
RepositoryInterface $localeRepository, |
63
|
|
|
ImageUploaderInterface $imageUploader, |
64
|
|
|
?FileLocatorInterface $fileLocator = null |
65
|
|
|
) { |
66
|
|
|
$this->articleFactory = $articleFactory; |
67
|
|
|
$this->articleImageFactory = $articleImageFactory; |
68
|
|
|
$this->articleCommentFactory = $articleCommentFactory; |
69
|
|
|
$this->channelRepository = $channelRepository; |
70
|
|
|
$this->articleCategoryRepository = $articleCategoryRepository; |
71
|
|
|
$this->localeRepository = $localeRepository; |
72
|
|
|
$this->imageUploader = $imageUploader; |
73
|
|
|
$this->fileLocator = $fileLocator; |
74
|
|
|
|
75
|
|
|
$this->faker = Factory::create(); |
76
|
|
|
$this->optionsResolver = new OptionsResolver(); |
77
|
|
|
|
78
|
|
|
$this->configureOptions($this->optionsResolver); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function create(array $options = []): ArticleInterface |
85
|
|
|
{ |
86
|
|
|
$options = $this->optionsResolver->resolve($options); |
87
|
|
|
|
88
|
|
|
/** @var ArticleInterface $article */ |
89
|
|
|
$article = $this->articleFactory->createNew(); |
90
|
|
|
$article->setCode($options['code']); |
91
|
|
|
$article->setEnabled($options['enabled']); |
92
|
|
|
|
93
|
|
|
foreach ($options['channels'] as $channel) { |
94
|
|
|
$article->addChannel($channel); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($options['categories'] as $category) { |
98
|
|
|
$article->addCategory($category); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @var string $localeCode */ |
102
|
|
|
foreach ($this->getLocales() as $localeCode) { |
103
|
|
|
$article->setCurrentLocale($localeCode); |
104
|
|
|
$article->setFallbackLocale($localeCode); |
105
|
|
|
|
106
|
|
|
$article->setTitle($options['title']); |
107
|
|
|
$article->setContent($options['content']); |
108
|
|
|
$article->setSlug($options['slug']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->createImages($article, $options); |
112
|
|
|
|
113
|
|
|
if (rand(0, 100) > 50) { |
114
|
|
|
$this->addArticleComments($article); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $article; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function createImages(ArticleInterface $article, array $options): void |
121
|
|
|
{ |
122
|
|
|
foreach ($options['images'] as $imagePath) { |
123
|
|
|
$imagePath = null === $this->fileLocator ? $imagePath : $this->fileLocator->locate($imagePath); |
124
|
|
|
$uploadedImage = new UploadedFile($imagePath, basename($imagePath)); |
|
|
|
|
125
|
|
|
|
126
|
|
|
/** @var ImageInterface $articleImage */ |
127
|
|
|
$articleImage = $this->articleImageFactory->createNew(); |
128
|
|
|
$articleImage->setFile($uploadedImage); |
129
|
|
|
|
130
|
|
|
$this->imageUploader->upload($articleImage); |
131
|
|
|
|
132
|
|
|
$article->addImage($articleImage); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function addArticleComments(ArticleInterface $article): void |
137
|
|
|
{ |
138
|
|
|
for ($i = 0; $i < 8; ++$i) { |
139
|
|
|
/** @var ArticleCommentInterface $comment */ |
140
|
|
|
$comment = $this->articleCommentFactory->createNew(); |
141
|
|
|
$comment->setName($this->faker->name); |
142
|
|
|
$comment->setEmail($this->faker->email); |
143
|
|
|
$comment->setComment($this->faker->text); |
144
|
|
|
$comment->setEnabled($this->faker->boolean(70)); |
145
|
|
|
|
146
|
|
|
$article->addComment($comment); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function getLocales(): \Generator |
151
|
|
|
{ |
152
|
|
|
/** @var LocaleInterface[] $locales */ |
153
|
|
|
$locales = $this->localeRepository->findAll(); |
154
|
|
|
foreach ($locales as $locale) { |
155
|
|
|
yield $locale->getCode(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
163
|
|
|
{ |
164
|
|
|
$resolver |
165
|
|
|
->setDefault('code', function (Options $options): string { |
166
|
|
|
return StringInflector::nameToCode((string) $options['title']); |
167
|
|
|
}) |
168
|
|
|
->setDefault('enabled', function (Options $options): bool { |
|
|
|
|
169
|
|
|
return $this->faker->boolean(90); |
170
|
|
|
}) |
171
|
|
|
->setAllowedTypes('enabled', 'bool') |
172
|
|
|
->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3)) |
173
|
|
|
->setAllowedTypes('channels', 'array') |
174
|
|
|
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) |
175
|
|
|
->setDefault('categories', LazyOption::randomOnes($this->articleCategoryRepository, 1)) |
176
|
|
|
->setAllowedTypes('categories', 'array') |
177
|
|
|
->setNormalizer('categories', LazyOption::findBy($this->articleCategoryRepository, 'code')) |
178
|
|
|
->setDefault('title', function (Options $options): string { |
|
|
|
|
179
|
|
|
return $this->faker->text(20); |
180
|
|
|
}) |
181
|
|
|
->setDefault('content', function (Options $options): string { |
|
|
|
|
182
|
|
|
return $this->faker->text; |
183
|
|
|
}) |
184
|
|
|
->setDefault('slug', function (Options $options): string { |
185
|
|
|
return StringInflector::nameToCode((string) $options['title']); |
186
|
|
|
}) |
187
|
|
|
->setDefault('images', function (Options $options): array { |
|
|
|
|
188
|
|
|
return [ |
189
|
|
|
__DIR__.'/../../Resources/fixtures/article/images/0'.rand(1, 6).'.jpg', |
190
|
|
|
]; |
191
|
|
|
}) |
192
|
|
|
->setAllowedTypes('images', 'array') |
193
|
|
|
; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|