1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Fixtures Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\FixturesBundle\DataFixtures\ORM; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface; |
18
|
|
|
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
19
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
20
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
21
|
|
|
use SWP\Bundle\FixturesBundle\AbstractFixture; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
24
|
|
|
|
25
|
|
|
class LoadArticlesData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface |
26
|
|
|
{ |
27
|
|
|
private $manager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
5 |
|
public function load(ObjectManager $manager) |
33
|
|
|
{ |
34
|
5 |
|
$this->manager = $manager; |
35
|
5 |
|
$env = $this->getEnvironment(); |
36
|
|
|
|
37
|
5 |
|
$this->loadRoutes($env, $manager); |
38
|
5 |
|
$this->loadArticles($env, $manager); |
39
|
|
|
|
40
|
5 |
|
$manager->flush(); |
41
|
5 |
|
} |
42
|
|
|
|
43
|
5 |
|
public function loadRoutes($env, $manager) |
44
|
|
|
{ |
45
|
|
|
$routes = [ |
46
|
|
|
'dev' => [ |
47
|
|
|
[ |
48
|
|
|
'name' => 'politics', |
49
|
|
|
'variablePattern' => '/{slug}', |
50
|
|
|
'requirements' => [ |
51
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
52
|
|
|
], |
53
|
|
|
'type' => 'collection', |
54
|
|
|
'defaults' => [ |
55
|
|
|
'slug' => null, |
56
|
|
|
], |
57
|
|
|
'templateName' => 'category.html.twig', |
58
|
|
|
'articlesTemplateName' => 'article.html.twig', |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
'name' => 'business', |
62
|
|
|
'variablePattern' => '/{slug}', |
63
|
|
|
'requirements' => [ |
64
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
65
|
|
|
], |
66
|
|
|
'type' => 'collection', |
67
|
|
|
'defaults' => [ |
68
|
|
|
'slug' => null, |
69
|
|
|
], |
70
|
|
|
'templateName' => 'category.html.twig', |
71
|
|
|
'articlesTemplateName' => 'article.html.twig', |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
'name' => 'scitech', |
75
|
|
|
'variablePattern' => '/{slug}', |
76
|
|
|
'requirements' => [ |
77
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
78
|
|
|
], |
79
|
|
|
'type' => 'collection', |
80
|
|
|
'defaults' => [ |
81
|
|
|
'slug' => null, |
82
|
|
|
], |
83
|
|
|
'templateName' => 'category.html.twig', |
84
|
|
|
'articlesTemplateName' => 'article.html.twig', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'name' => 'health', |
88
|
|
|
'variablePattern' => '/{slug}', |
89
|
|
|
'requirements' => [ |
90
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
91
|
|
|
], |
92
|
|
|
'type' => 'collection', |
93
|
|
|
'defaults' => [ |
94
|
|
|
'slug' => null, |
95
|
|
|
], |
96
|
|
|
'templateName' => 'category.html.twig', |
97
|
|
|
'articlesTemplateName' => 'article.html.twig', |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
'name' => 'entertainment', |
101
|
|
|
'variablePattern' => '/{slug}', |
102
|
|
|
'requirements' => [ |
103
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
104
|
|
|
], |
105
|
|
|
'type' => 'collection', |
106
|
|
|
'defaults' => [ |
107
|
|
|
'slug' => null, |
108
|
|
|
], |
109
|
|
|
'templateName' => 'category.html.twig', |
110
|
|
|
'articlesTemplateName' => 'article.html.twig', |
111
|
|
|
], |
112
|
|
|
[ |
113
|
|
|
'name' => 'sports', |
114
|
|
|
'variablePattern' => '/{slug}', |
115
|
|
|
'requirements' => [ |
116
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
117
|
|
|
], |
118
|
|
|
'type' => 'collection', |
119
|
|
|
'defaults' => [ |
120
|
|
|
'slug' => null, |
121
|
|
|
], |
122
|
|
|
'templateName' => 'category.html.twig', |
123
|
|
|
'articlesTemplateName' => 'article.html.twig', |
124
|
|
|
], |
125
|
|
|
[ |
126
|
|
|
'name' => 'about', |
127
|
|
|
'variablePattern' => '/{slug}', |
128
|
|
|
'requirements' => [ |
129
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
130
|
|
|
], |
131
|
|
|
'type' => 'collection', |
132
|
|
|
'defaults' => [ |
133
|
|
|
'slug' => null, |
134
|
|
|
], |
135
|
|
|
'articlesTemplateName' => 'page.html.twig', |
136
|
|
|
], |
137
|
|
|
[ |
138
|
|
|
'name' => 'home', |
139
|
|
|
'type' => 'content', |
140
|
|
|
'templateName' => 'index.html.twig', |
141
|
|
|
], |
142
|
5 |
|
], |
143
|
|
|
'test' => [ |
144
|
|
|
[ |
145
|
|
|
'name' => 'news', |
146
|
|
|
'variablePattern' => '/{slug}', |
147
|
|
|
'requirements' => [ |
148
|
|
|
'slug' => '[a-zA-Z0-9*\-_\/]+', |
149
|
|
|
], |
150
|
|
|
'type' => 'collection', |
151
|
|
|
'defaults' => [ |
152
|
|
|
'slug' => null, |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
[ |
156
|
|
|
'name' => 'articles', |
157
|
|
|
'type' => 'content', |
158
|
|
|
], |
159
|
|
|
[ |
160
|
|
|
'name' => 'articles/features', |
161
|
|
|
'type' => 'content', |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]; |
165
|
|
|
|
166
|
5 |
|
$routeService = $this->container->get('swp.service.route'); |
167
|
|
|
|
168
|
5 |
View Code Duplication |
foreach ($routes[$env] as $routeData) { |
169
|
5 |
|
$route = $this->container->get('swp.factory.route')->create(); |
170
|
5 |
|
$route->setName($routeData['name']); |
171
|
5 |
|
$route->setType($routeData['type']); |
172
|
|
|
|
173
|
5 |
|
if (isset($routeData['cacheTimeInSeconds'])) { |
174
|
|
|
$route->setCacheTimeInSeconds($routeData['cacheTimeInSeconds']); |
175
|
|
|
} |
176
|
|
|
|
177
|
5 |
|
if (isset($routeData['templateName'])) { |
178
|
|
|
$route->setTemplateName($routeData['templateName']); |
179
|
|
|
} |
180
|
|
|
|
181
|
5 |
|
if (isset($routeData['articlesTemplateName'])) { |
182
|
|
|
$route->setArticlesTemplateName($routeData['articlesTemplateName']); |
183
|
|
|
} |
184
|
|
|
|
185
|
5 |
|
$route = $routeService->fillRoute($route); |
186
|
|
|
|
187
|
5 |
|
$manager->persist($route); |
188
|
|
|
} |
189
|
|
|
|
190
|
5 |
|
$manager->flush(); |
191
|
5 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sets articles manually (not via Alice) for test env due to fatal error: |
195
|
|
|
* Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception. |
196
|
|
|
*/ |
197
|
5 |
|
public function loadArticles($env, ObjectManager $manager) |
198
|
|
|
{ |
199
|
5 |
|
if ($env !== 'test') { |
200
|
|
|
$articles = $this->loadFixtures( |
201
|
|
|
'@SWPFixturesBundle/Resources/fixtures/ORM/'.$env.'/article.yml', |
202
|
|
|
$manager, |
203
|
|
|
[ |
204
|
|
|
'providers' => [$this], |
205
|
|
|
] |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$renditions = [ |
209
|
|
|
'770x515' => [ |
210
|
|
|
'width' => '770', |
211
|
|
|
'height' => '515', |
212
|
|
|
], |
213
|
|
|
'478x326' => [ |
214
|
|
|
'width' => '478', |
215
|
|
|
'height' => '326', |
216
|
|
|
], |
217
|
|
|
]; |
218
|
|
|
|
219
|
|
|
$mediaManager = $this->container->get('swp_content_bundle.manager.media'); |
220
|
|
|
|
221
|
|
|
foreach ($articles as $article) { |
222
|
|
|
$images = [ |
223
|
|
|
__DIR__.'/../../Resources/assets/images-cms-image-'.rand(1, 11).'.jpg', |
224
|
|
|
__DIR__.'/../../Resources/assets/images-cms-image-'.rand(1, 11).'.jpg', |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
foreach ($images as $fakeImage) { |
228
|
|
|
// create Media |
229
|
|
|
$articleMediaClass = $this->container->getParameter('swp.model.media.class'); |
230
|
|
|
$articleMedia = new $articleMediaClass(); |
231
|
|
|
$articleMedia->setArticle($article); |
232
|
|
|
$articleMedia->setKey('embedded'.uniqid()); |
233
|
|
|
$articleMedia->setBody('This is very nice image caption...'); |
234
|
|
|
$articleMedia->setByLine('By Best Editor'); |
235
|
|
|
$articleMedia->setLocated('Porto'); |
236
|
|
|
$articleMedia->setDescription('Media description'); |
237
|
|
|
$articleMedia->setUsageTerms('Some super open terms'); |
238
|
|
|
$articleMedia->setMimetype('image/jpeg'); |
239
|
|
|
$manager->persist($articleMedia); |
240
|
|
|
|
241
|
|
|
/* @var $rendition Rendition */ |
242
|
|
|
foreach ($renditions as $key => $rendition) { |
243
|
|
|
$mediaId = uniqid(); |
244
|
|
|
$uploadedFile = new UploadedFile( |
245
|
|
|
$fakeImage, |
246
|
|
|
$mediaId, |
247
|
|
|
'image/jpeg', |
248
|
|
|
filesize($fakeImage), |
249
|
|
|
null, |
250
|
|
|
true |
251
|
|
|
); |
252
|
|
|
$image = $mediaManager->handleUploadedFile($uploadedFile, $mediaId); |
253
|
|
|
|
254
|
|
|
$imageRendition = new ImageRendition(); |
255
|
|
|
$imageRendition->setImage($image); |
256
|
|
|
$imageRendition->setHeight($rendition['height']); |
257
|
|
|
$imageRendition->setWidth($rendition['width']); |
258
|
|
|
$imageRendition->setName($key); |
259
|
|
|
$imageRendition->setMedia($articleMedia); |
260
|
|
|
$articleMedia->addRendition($imageRendition); |
261
|
|
|
$manager->persist($imageRendition); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$articles = [ |
268
|
|
|
'test' => [ |
269
|
|
|
[ |
270
|
|
|
'title' => 'Test news article', |
271
|
|
|
'content' => 'Test news article content', |
272
|
|
|
'route' => 'news', |
273
|
|
|
'locale' => 'en', |
274
|
|
|
], |
275
|
|
|
[ |
276
|
|
|
'title' => 'Test article', |
277
|
|
|
'content' => 'Test article content', |
278
|
|
|
'route' => 'news', |
279
|
|
|
'locale' => 'en', |
280
|
|
|
], |
281
|
|
|
[ |
282
|
|
|
'title' => 'Features', |
283
|
|
|
'content' => 'Features content', |
284
|
|
|
'route' => 'news', |
285
|
|
|
'locale' => 'en', |
286
|
|
|
], |
287
|
|
|
[ |
288
|
|
|
'title' => 'Features client1', |
289
|
|
|
'content' => 'Features client1 content', |
290
|
|
|
'route' => 'articles/features', |
291
|
|
|
'locale' => 'en', |
292
|
|
|
], |
293
|
5 |
|
], |
294
|
|
|
]; |
295
|
|
|
|
296
|
5 |
|
if (isset($articles[$env])) { |
297
|
5 |
View Code Duplication |
foreach ($articles[$env] as $articleData) { |
|
|
|
|
298
|
5 |
|
$article = $this->container->get('swp.factory.article')->create(); |
299
|
5 |
|
$article->setTitle($articleData['title']); |
300
|
5 |
|
$article->setBody($articleData['content']); |
301
|
5 |
|
$article->setRoute($this->getRouteByName($articleData['route'])); |
302
|
5 |
|
$article->setLocale($articleData['locale']); |
303
|
5 |
|
$article->setPublishable(true); |
304
|
5 |
|
$article->setPublishedAt(new \DateTime()); |
305
|
5 |
|
$article->setStatus(ArticleInterface::STATUS_PUBLISHED); |
306
|
5 |
|
$manager->persist($article); |
307
|
|
|
|
308
|
5 |
|
$this->addReference($article->getSlug(), $article); |
309
|
|
|
} |
310
|
|
|
|
311
|
5 |
|
$manager->flush(); |
312
|
|
|
} |
313
|
5 |
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Article example metadata. |
317
|
|
|
* |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
|
|
public function articleMetadata() |
321
|
|
|
{ |
322
|
|
|
$authors = [ |
323
|
|
|
'Sarrah Staffwriter', |
324
|
|
|
'John Smith', |
325
|
|
|
'Test Persona', |
326
|
|
|
'Jane Stockwriter', |
327
|
|
|
'James Q. Reporter', |
328
|
|
|
'Karen Ruhiger', |
329
|
|
|
'George Langsamer', |
330
|
|
|
]; |
331
|
|
|
|
332
|
|
|
return [ |
333
|
|
|
'located' => 'Sydney', |
334
|
|
|
'byline' => $authors[array_rand($authors)], |
335
|
|
|
'place' => [ |
336
|
|
|
[ |
337
|
|
|
'qcode' => 'AUS', |
338
|
|
|
'world_region' => 'Rest Of World', |
339
|
|
|
], [ |
340
|
|
|
'qcode' => 'EUR', |
341
|
|
|
'world_region' => 'Europe', |
342
|
|
|
], |
343
|
|
|
], |
344
|
|
|
]; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* {@inheritdoc} |
349
|
|
|
*/ |
350
|
5 |
|
public function getOrder() |
351
|
|
|
{ |
352
|
5 |
|
return 1; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.