1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Theme\Generator; |
18
|
|
|
|
19
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
20
|
|
|
use Doctrine\Common\Collections\Collection; |
21
|
|
|
use SWP\Bundle\ContentBundle\Factory\ArticleFactoryInterface; |
22
|
|
|
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
25
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
26
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleMediaInterface; |
27
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleStatisticsInterface; |
28
|
|
|
use SWP\Bundle\CoreBundle\Model\Image; |
29
|
|
|
use SWP\Bundle\CoreBundle\Repository\ArticleRepositoryInterface; |
30
|
|
|
use Faker; |
31
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
32
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
33
|
|
|
|
34
|
|
|
class FakeArticlesGenerator implements FakeArticlesGeneratorInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ArticleFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $articleFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MediaManagerInterface |
43
|
|
|
*/ |
44
|
|
|
protected $mediaManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var MediaFactoryInterface |
48
|
|
|
*/ |
49
|
|
|
protected $articleMediaFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ArticleRepositoryInterface |
53
|
|
|
*/ |
54
|
|
|
protected $articleRepository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var FactoryInterface |
58
|
|
|
*/ |
59
|
|
|
protected $articleStatisticsFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* FakeArticlesGenerator constructor. |
63
|
|
|
* |
64
|
|
|
* @param ArticleFactoryInterface $articleFactory |
65
|
|
|
* @param MediaManagerInterface $mediaManager |
66
|
|
|
* @param MediaFactoryInterface $articleMediaFactory |
67
|
|
|
* @param ArticleRepositoryInterface $articleRepository |
68
|
|
|
* @param FactoryInterface $articleStatisticsFactory |
69
|
|
|
*/ |
70
|
|
|
public function __construct(ArticleFactoryInterface $articleFactory, MediaManagerInterface $mediaManager, MediaFactoryInterface $articleMediaFactory, ArticleRepositoryInterface $articleRepository, FactoryInterface $articleStatisticsFactory) |
71
|
|
|
{ |
72
|
|
|
$this->articleFactory = $articleFactory; |
73
|
|
|
$this->mediaManager = $mediaManager; |
74
|
|
|
$this->articleMediaFactory = $articleMediaFactory; |
75
|
|
|
$this->articleRepository = $articleRepository; |
76
|
|
|
$this->articleStatisticsFactory = $articleStatisticsFactory; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function generate(int $numberOfArticles): array |
83
|
|
|
{ |
84
|
|
|
$articles = []; |
85
|
|
|
for (; $numberOfArticles > 0; --$numberOfArticles) { |
86
|
|
|
/** @var ArticleInterface $article */ |
87
|
|
|
$article = $this->articleFactory->create(); |
88
|
|
|
$faker = Faker\Factory::create(); |
89
|
|
|
$article->setTitle($faker->catchPhrase()); |
90
|
|
|
$article->setBody($faker->paragraph(20)); |
91
|
|
|
$article->setLead($faker->paragraph(3)); |
92
|
|
|
$article->setLocale('en'); |
93
|
|
|
$article->setMetadata(['located' => 'Porto']); |
94
|
|
|
$article->setStatus(ArticleInterface::STATUS_PUBLISHED); |
95
|
|
|
$article->setPublishedAt(new \DateTime()); |
96
|
|
|
$article->setPublishable(true); |
97
|
|
|
$article->setCode($faker->uuid); |
98
|
|
|
$this->articleRepository->persist($article); |
99
|
|
|
$article->setMedia($this->createArticleMedia($article)); |
100
|
|
|
$article->setArticleStatistics($this->createArticleStatistics($article)); |
101
|
|
|
|
102
|
|
|
$articles[] = $article; |
103
|
|
|
} |
104
|
|
|
$this->articleRepository->flush(); |
105
|
|
|
|
106
|
|
|
return $articles; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ArticleInterface $article |
111
|
|
|
* |
112
|
|
|
* @return ArticleStatisticsInterface |
113
|
|
|
*/ |
114
|
|
|
protected function createArticleStatistics(ArticleInterface $article): ArticleStatisticsInterface |
115
|
|
|
{ |
116
|
|
|
/** @var ArticleStatisticsInterface $articleStatistics */ |
117
|
|
|
$articleStatistics = $this->articleStatisticsFactory->create(); |
118
|
|
|
$articleStatistics->setArticle($article); |
119
|
|
|
$articleStatistics->setPageViewsNumber(0); |
120
|
|
|
$this->articleRepository->persist($articleStatistics); |
121
|
|
|
|
122
|
|
|
return $articleStatistics; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param ArticleInterface $article |
127
|
|
|
* |
128
|
|
|
* @return Collection |
129
|
|
|
*/ |
130
|
|
|
protected function createArticleMedia(ArticleInterface $article): Collection |
131
|
|
|
{ |
132
|
|
|
$mediaId = uniqid(); |
133
|
|
|
$faker = Faker\Factory::create(); |
134
|
|
|
$fakeImage = $faker->image(sys_get_temp_dir(), 800, 800, 'cats', true, true, $article->getSlug()); |
135
|
|
|
if (!is_string($fakeImage)) { |
136
|
|
|
$im = imagecreatetruecolor(800, 800); |
137
|
|
|
$textColor = imagecolorallocate($im, 233, 14, 91); |
138
|
|
|
imagestring($im, 1, 5, 5, $article->getTitle(), $textColor); |
139
|
|
|
$fakeImage = sys_get_temp_dir().'/'.$article->getSlug().'.jpg'; |
140
|
|
|
imagejpeg($im, $fakeImage); |
141
|
|
|
imagedestroy($im); |
142
|
|
|
} |
143
|
|
|
$uploadedFile = new UploadedFile($fakeImage, $mediaId, 'image/jpeg', filesize($fakeImage), null, true); |
144
|
|
|
/** @var Image $image */ |
145
|
|
|
$image = $this->mediaManager->handleUploadedFile($uploadedFile, $mediaId); |
146
|
|
|
/** @var ArticleMediaInterface $articleMedia */ |
147
|
|
|
$articleMedia = $this->articleMediaFactory->createEmpty(); |
148
|
|
|
$articleMedia->setImage($image); |
149
|
|
|
$articleMedia->setArticle($article); |
150
|
|
|
$articleMedia->setKey('embedded'.uniqid()); |
151
|
|
|
$articleMedia->setBody('This is very nice image caption...'); |
152
|
|
|
$articleMedia->setByLine('By Best Editor'); |
153
|
|
|
$articleMedia->setLocated('Porto'); |
154
|
|
|
$articleMedia->setDescription('Media description'); |
155
|
|
|
$articleMedia->setUsageTerms('Some super open terms'); |
156
|
|
|
$articleMedia->setMimetype('image/jpeg'); |
157
|
|
|
$article->setFeatureMedia($articleMedia); |
158
|
|
|
$this->articleRepository->persist($articleMedia); |
159
|
|
|
|
160
|
|
|
$imageRendition = new ImageRendition(); |
161
|
|
|
$imageRendition->setImage($image); |
162
|
|
|
$imageRendition->setHeight(800); |
163
|
|
|
$imageRendition->setWidth(800); |
164
|
|
|
$imageRendition->setName('original'); |
165
|
|
|
$imageRendition->setMedia($articleMedia); |
166
|
|
|
$articleMedia->addRendition($imageRendition); |
167
|
|
|
$this->articleRepository->persist($imageRendition); |
168
|
|
|
|
169
|
|
|
return new ArrayCollection([$articleMedia]); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|