1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Fixtures Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. 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\Doctrine\ORM\Article; |
21
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\ArticleMedia; |
22
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\ImageRendition; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
24
|
|
|
use SWP\Bundle\FixturesBundle\AbstractFixture; |
25
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
26
|
|
|
|
27
|
|
|
class LoadArticlesMediaData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface |
28
|
|
|
{ |
29
|
|
|
private $manager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
4 |
|
public function load(ObjectManager $manager) |
35
|
|
|
{ |
36
|
4 |
|
$this->manager = $manager; |
37
|
4 |
|
$env = $this->getEnvironment(); |
38
|
4 |
|
$this->loadArticles($env, $manager); |
39
|
|
|
|
40
|
4 |
|
$manager->flush(); |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sets articles manually (not via Alice) for test env due to fatal error: |
45
|
|
|
* Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception. |
46
|
|
|
*/ |
47
|
4 |
|
public function loadArticles($env, $manager) |
48
|
|
|
{ |
49
|
|
|
$articles = [ |
50
|
|
|
'test' => [ |
51
|
|
|
[ |
52
|
|
|
'title' => 'Test news article', |
53
|
|
|
'content' => 'Test news article content', |
54
|
|
|
'locale' => 'en', |
55
|
|
|
], |
56
|
4 |
|
], |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$renditions = [ |
60
|
|
|
'original' => [ |
61
|
|
|
'width' => '4000', |
62
|
|
|
'height' => '2667', |
63
|
|
|
'media' => '12345678987654321a', |
64
|
4 |
|
], |
65
|
|
|
'16-9' => [ |
66
|
|
|
'width' => '1079', |
67
|
|
|
'height' => '720', |
68
|
|
|
'media' => '12345678987654321b', |
69
|
|
|
], |
70
|
|
|
'4-3' => [ |
71
|
|
|
'width' => '800', |
72
|
|
|
'height' => '533', |
73
|
|
|
'media' => '12345678987654321c', |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
|
77
|
4 |
|
$mediaManager = $this->container->get('swp_content_bundle.manager.media'); |
78
|
4 |
|
$fakeImage = __DIR__.'/../../Resources/assets/test_cc_image.jpg'; |
79
|
|
|
|
80
|
4 |
|
if (isset($articles[$env])) { |
81
|
4 |
|
foreach ($articles[$env] as $articleData) { |
82
|
4 |
|
$article = new Article(); |
83
|
4 |
|
$article->setTitle($articleData['title']); |
84
|
4 |
|
$article->setBody($articleData['content']); |
85
|
4 |
|
$article->setLocale($articleData['locale']); |
86
|
4 |
|
$article->setPublishedAt(new \DateTime()); |
87
|
4 |
|
$article->setPublishable(true); |
88
|
4 |
|
$article->setStatus(ArticleInterface::STATUS_PUBLISHED); |
89
|
4 |
|
$manager->persist($article); |
90
|
|
|
|
91
|
|
|
// create Media |
92
|
4 |
|
$articleMedia = new ArticleMedia(); |
93
|
4 |
|
$articleMedia->setArticle($article); |
94
|
4 |
|
$articleMedia->setKey('embedded6358005131'); |
95
|
4 |
|
$articleMedia->setBody('article media body'); |
96
|
4 |
|
$articleMedia->setByLine('By Best Editor'); |
97
|
4 |
|
$articleMedia->setLocated('Porto'); |
98
|
4 |
|
$articleMedia->setDescription('Media description'); |
99
|
4 |
|
$articleMedia->setUsageTerms('Some super open terms'); |
100
|
4 |
|
$articleMedia->setMimetype('image/jpeg'); |
101
|
4 |
|
$manager->persist($articleMedia); |
102
|
|
|
|
103
|
|
|
/* @var $rendition Rendition */ |
104
|
4 |
View Code Duplication |
foreach ($renditions as $key => $rendition) { |
|
|
|
|
105
|
4 |
|
$uploadedFile = new UploadedFile($fakeImage, $rendition['media'], 'image/jpeg', filesize($fakeImage), null, true); |
106
|
4 |
|
$image = $mediaManager->handleUploadedFile($uploadedFile, $rendition['media']); |
107
|
|
|
|
108
|
4 |
|
if ($key === 'original') { |
109
|
4 |
|
$articleMedia->setImage($image); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
$imageRendition = new ImageRendition(); |
113
|
4 |
|
$imageRendition->setImage($image); |
114
|
4 |
|
$imageRendition->setHeight($rendition['height']); |
115
|
4 |
|
$imageRendition->setWidth($rendition['width']); |
116
|
4 |
|
$imageRendition->setName($key); |
117
|
4 |
|
$imageRendition->setMedia($articleMedia); |
118
|
4 |
|
$articleMedia->addRendition($imageRendition); |
119
|
4 |
|
$manager->persist($imageRendition); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
$manager->flush(); |
124
|
|
|
} |
125
|
4 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
4 |
|
public function getOrder() |
131
|
|
|
{ |
132
|
4 |
|
return 999; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.