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\Model\ArticleMedia; |
20
|
|
|
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Provider\ORM\ArticleMediaAssetProviderInterface; |
24
|
|
|
use SWP\Component\Bridge\Model\ItemInterface; |
25
|
|
|
use SWP\Component\Bridge\Model\RenditionInterface; |
26
|
|
|
use SWP\Component\Storage\Factory\FactoryInterface; |
27
|
|
|
|
28
|
|
|
class MediaFactory implements MediaFactoryInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ArticleMediaAssetProviderInterface |
32
|
|
|
*/ |
33
|
|
|
protected $articleMediaAssetProvider; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FactoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $factory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ImageRenditionFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $imageRenditionFactory; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
ArticleMediaAssetProviderInterface $articleMediaAssetProvider, |
47
|
|
|
FactoryInterface $factory, |
48
|
|
|
ImageRenditionFactoryInterface $imageRenditionFactory |
49
|
|
|
) { |
50
|
|
|
$this->articleMediaAssetProvider = $articleMediaAssetProvider; |
51
|
|
|
$this->factory = $factory; |
52
|
|
|
$this->imageRenditionFactory = $imageRenditionFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface |
56
|
|
|
{ |
57
|
|
|
$articleMedia = $this->factory->create(); |
58
|
|
|
$articleMedia->setArticle($article); |
59
|
|
|
$articleMedia->setFromItem($item); |
60
|
|
|
|
61
|
|
|
if (ItemInterface::TYPE_PICTURE === $item->getType()) { |
62
|
|
|
return $this->createImageMedia($articleMedia, $key, $item); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->createFileMedia($articleMedia, $key, $item); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function createEmpty(): ArticleMediaInterface |
69
|
|
|
{ |
70
|
|
|
return $this->factory->create(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function createFileMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface |
74
|
|
|
{ |
75
|
|
|
if (0 === $item->getRenditions()->count()) { |
76
|
|
|
return $articleMedia; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$originalRendition = $this->findOriginalRendition($item); |
80
|
|
|
|
81
|
|
|
$articleMedia->setMimetype($originalRendition->getMimetype()); |
82
|
|
|
$articleMedia->setKey($key); |
83
|
|
|
|
84
|
|
|
$file = $this->articleMediaAssetProvider->getFile($originalRendition); |
85
|
|
|
$articleMedia->setFile($file); |
86
|
|
|
|
87
|
|
|
return $articleMedia; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface |
91
|
|
|
{ |
92
|
|
|
if (0 === $item->getRenditions()->count()) { |
93
|
|
|
return $articleMedia; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$originalRendition = $this->findOriginalRendition($item); |
97
|
|
|
|
98
|
|
|
$articleMedia->setMimetype($originalRendition->getMimetype()); |
99
|
|
|
$articleMedia->setKey($key); |
100
|
|
|
|
101
|
|
|
$image = $this->articleMediaAssetProvider->getImage($originalRendition); |
102
|
|
|
$articleMedia->setImage($image); |
103
|
|
|
|
104
|
|
|
foreach ($item->getRenditions() as $rendition) { |
105
|
|
|
$image = $this->articleMediaAssetProvider->getImage($rendition); |
106
|
|
|
|
107
|
|
|
if (null === $image) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$imageRendition = $this->imageRenditionFactory->createWith($articleMedia, $image, $rendition); |
112
|
|
|
$articleMedia->addRendition($imageRendition); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $articleMedia; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function findOriginalRendition(ItemInterface $item): RenditionInterface |
119
|
|
|
{ |
120
|
|
|
return $item->getRenditions()->filter( |
121
|
|
|
function (RenditionInterface $rendition) { |
122
|
|
|
return 'original' === $rendition->getName(); |
123
|
|
|
} |
124
|
|
|
)->first(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.