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\Model\ArticleMedia; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
22
|
|
|
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
25
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageInterface; |
26
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
27
|
|
|
use SWP\Component\Bridge\Model\ItemInterface; |
28
|
|
|
use SWP\Component\Bridge\Model\Rendition; |
29
|
|
|
use SWP\Component\Bridge\Model\RenditionInterface; |
30
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
31
|
|
|
|
32
|
|
|
class MediaFactory implements MediaFactoryInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var ImageRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
protected $imageRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var FactoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected $factory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* MediaFactory constructor. |
46
|
|
|
* |
47
|
|
|
* @param ImageRepositoryInterface $imageRepository |
48
|
|
|
* @param FactoryInterface $factory |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ImageRepositoryInterface $imageRepository, FactoryInterface $factory) |
51
|
|
|
{ |
52
|
|
|
$this->imageRepository = $imageRepository; |
53
|
|
|
$this->factory = $factory; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface |
60
|
|
|
{ |
61
|
|
|
$articleMedia = $this->factory->create(); |
62
|
|
|
$articleMedia->setArticle($article); |
63
|
|
|
$articleMedia->setFromItem($item); |
64
|
|
|
$articleMedia = $this->createImageMedia($articleMedia, $key, $item); |
65
|
12 |
|
|
66
|
|
|
return $articleMedia; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
12 |
|
*/ |
72
|
12 |
|
public function createEmpty(): ArticleMediaInterface |
73
|
12 |
|
{ |
74
|
12 |
|
return $this->factory->create(); |
75
|
12 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function createImageRendition( |
81
|
|
|
ImageInterface $image, |
82
|
|
|
ArticleMediaInterface $articleMedia, |
83
|
|
|
string $key, |
84
|
|
|
Rendition $rendition |
85
|
|
|
): ImageRenditionInterface { |
86
|
|
|
$imageRendition = new ImageRendition(); |
87
|
|
|
$imageRendition->setImage($image); |
88
|
|
|
$imageRendition->setMedia($articleMedia); |
89
|
|
|
$imageRendition->setHeight($rendition->getHeight()); |
90
|
|
|
$imageRendition->setWidth($rendition->getWidth()); |
91
|
|
|
$imageRendition->setName($key); |
92
|
|
|
|
93
|
|
|
return $imageRendition; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Handle Article Media with Image (add renditions, set mimetype etc.). |
98
|
|
|
* |
99
|
|
|
* @param ArticleMedia $articleMedia |
100
|
|
|
* @param string $key unique key shared between media and image rendition |
101
|
|
|
* @param ItemInterface $item |
102
|
|
|
* |
103
|
|
|
* @return ArticleMedia |
104
|
|
|
*/ |
105
|
|
|
protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item) |
106
|
|
|
{ |
107
|
|
|
if (0 === $item->getRenditions()->count()) { |
108
|
|
|
return $articleMedia; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$originalRendition = $item->getRenditions()->filter( |
112
|
|
|
function (RenditionInterface $rendition) { |
113
|
|
|
return 'original' === $rendition->getName(); |
114
|
|
|
} |
115
|
|
|
)->first(); |
116
|
|
|
|
117
|
|
|
$articleMedia->setMimetype($originalRendition->getMimetype()); |
118
|
|
|
$articleMedia->setKey($key); |
119
|
|
|
$image = $this->findImage($originalRendition->getMedia()); |
120
|
|
|
$articleMedia->setImage($image); |
|
|
|
|
121
|
|
|
foreach ($item->getRenditions() as $rendition) { |
122
|
|
|
$image = $this->findImage($rendition->getMedia()); |
123
|
|
|
if (null === $image) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$imageRendition = $this->createImageRendition($image, $articleMedia, $rendition->getName(), $rendition); |
128
|
|
|
$articleMedia->addRendition($imageRendition); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $articleMedia; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function findImage(string $mediaId) |
135
|
|
|
{ |
136
|
|
|
return $this->imageRepository->findImageByAssetId(ArticleMedia::handleMediaId($mediaId)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: