|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2016 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 2016 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Factory\ORM; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ImageRepositoryInterface; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\ArticleMedia; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\File; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\Image; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ORM\ImageRendition; |
|
24
|
|
|
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface; |
|
25
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
26
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
|
27
|
|
|
use SWP\Bundle\ContentBundle\Model\FileInterface; |
|
28
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageInterface; |
|
29
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
|
30
|
|
|
use SWP\Component\Bridge\Model\ItemInterface; |
|
31
|
|
|
use SWP\Component\Bridge\Model\Rendition; |
|
32
|
|
|
use SWP\Component\Common\Criteria\Criteria; |
|
33
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
34
|
|
|
|
|
35
|
|
|
class MediaFactory implements MediaFactoryInterface |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var ImageRepositoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $imageRepository; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* MediaFactory constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param ImageRepositoryInterface $imageRepository |
|
46
|
|
|
*/ |
|
47
|
22 |
|
public function __construct( |
|
48
|
|
|
ImageRepositoryInterface $imageRepository |
|
49
|
|
|
) { |
|
50
|
22 |
|
$this->imageRepository = $imageRepository; |
|
51
|
22 |
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface |
|
54
|
|
|
{ |
|
55
|
4 |
|
$articleMedia = new ArticleMedia(); |
|
56
|
4 |
|
$articleMedia->setArticle($article); |
|
57
|
4 |
|
$articleMedia->setFromItem($item); |
|
58
|
|
|
|
|
59
|
4 |
|
$articleMedia = $this->createImageMedia($articleMedia, $key, $item); |
|
60
|
|
|
|
|
61
|
4 |
|
return $articleMedia; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
9 |
|
public function createMediaAsset(UploadedFile $uploadedFile, string $assetId): FileInterface |
|
68
|
|
|
{ |
|
69
|
9 |
|
$asset = $this->getProperObject($uploadedFile); |
|
70
|
9 |
|
$asset->setAssetId($assetId); |
|
71
|
9 |
|
$asset->setFileExtension($uploadedFile->guessClientExtension()); |
|
72
|
|
|
|
|
73
|
9 |
|
return $asset; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function createImageRendition( |
|
80
|
|
|
ImageInterface $image, |
|
81
|
|
|
ArticleMediaInterface $articleMedia, |
|
82
|
|
|
string $key, Rendition $rendition |
|
83
|
|
|
): ImageRenditionInterface { |
|
84
|
2 |
|
$imageRendition = new ImageRendition(); |
|
85
|
2 |
|
$imageRendition->setImage($image); |
|
86
|
2 |
|
$imageRendition->setMedia($articleMedia); |
|
87
|
2 |
|
$imageRendition->setHeight($rendition->getHeight()); |
|
88
|
2 |
|
$imageRendition->setWidth($rendition->getWidth()); |
|
89
|
2 |
|
$imageRendition->setName($key); |
|
90
|
|
|
|
|
91
|
2 |
|
return $imageRendition; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Handle Article Media with Image (add renditions, set mimetype etc.). |
|
96
|
|
|
* |
|
97
|
|
|
* @param ArticleMedia $articleMedia |
|
98
|
|
|
* @param string $key unique key shared between media and image rendition |
|
99
|
|
|
* @param ItemInterface $item |
|
100
|
|
|
* |
|
101
|
|
|
* @return ArticleMedia |
|
102
|
|
|
*/ |
|
103
|
4 |
|
protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item) |
|
104
|
|
|
{ |
|
105
|
4 |
|
if (0 === $item->getRenditions()->count()) { |
|
106
|
1 |
|
return $articleMedia; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
$originalRendition = $item->getRenditions()['original']; |
|
110
|
3 |
|
$criteria = new Criteria(); |
|
111
|
3 |
|
$criteria->set('assetId', ArticleMedia::handleMediaId($originalRendition->getMedia())); |
|
112
|
|
|
|
|
113
|
3 |
|
$articleMedia->setMimetype($originalRendition->getMimetype()); |
|
114
|
3 |
|
$articleMedia->setKey($key); |
|
115
|
3 |
|
$image = $this->imageRepository->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult(); |
|
116
|
3 |
|
$articleMedia->setImage($image); |
|
117
|
|
|
|
|
118
|
3 |
|
foreach ($item->getRenditions() as $key => $rendition) { |
|
119
|
3 |
|
$criteria->set('assetId', ArticleMedia::handleMediaId($rendition->getMedia())); |
|
120
|
3 |
|
$image = $this->imageRepository->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult(); |
|
121
|
3 |
|
if (null === $image) { |
|
122
|
1 |
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
$imageRendition = $image->getRendition(); |
|
126
|
3 |
|
if (null === $image->getRendition()) { |
|
127
|
2 |
|
$imageRendition = $this->createImageRendition($image, $articleMedia, $key, $rendition); |
|
128
|
2 |
|
$this->imageRepository->persist($imageRendition); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
3 |
|
$articleMedia->addRendition($imageRendition); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
3 |
|
return $articleMedia; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
9 |
|
protected function getProperObject(UploadedFile $uploadedFile) |
|
138
|
|
|
{ |
|
139
|
9 |
|
if (in_array(exif_imagetype($uploadedFile->getRealPath()), [ |
|
140
|
9 |
|
IMAGETYPE_GIF, |
|
141
|
9 |
|
IMAGETYPE_JPEG, |
|
142
|
9 |
|
IMAGETYPE_PNG, |
|
143
|
9 |
|
IMAGETYPE_BMP, |
|
144
|
|
|
])) { |
|
145
|
9 |
|
return new Image(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return new File(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: