1 | <?php |
||
32 | class MediaFactory implements MediaFactoryInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var ArticleMediaAssetProviderInterface |
||
36 | */ |
||
37 | protected $articleMediaAssetProvider; |
||
38 | |||
39 | /** |
||
40 | * @var FactoryInterface |
||
41 | */ |
||
42 | protected $factory; |
||
43 | |||
44 | /** |
||
45 | * @var ImageRenditionFactoryInterface |
||
46 | */ |
||
47 | protected $imageRenditionFactory; |
||
48 | |||
49 | /** |
||
50 | * @var MediaManagerInterface |
||
51 | */ |
||
52 | protected $mediaManager; |
||
53 | |||
54 | /** |
||
55 | * @var LoggerInterface |
||
56 | */ |
||
57 | private $logger; |
||
58 | |||
59 | public function __construct( |
||
72 | |||
73 | public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface |
||
85 | |||
86 | public function createEmpty(): ArticleMediaInterface |
||
90 | |||
91 | protected function createFileMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface |
||
92 | { |
||
93 | if (0 === $item->getRenditions()->count()) { |
||
94 | return $articleMedia; |
||
95 | } |
||
96 | |||
97 | $originalRendition = $this->findOriginalRendition($item); |
||
98 | $articleMedia->setMimetype($originalRendition->getMimetype()); |
||
99 | $articleMedia->setKey($key); |
||
100 | $file = $this->articleMediaAssetProvider->getFile($originalRendition); |
||
101 | $articleMedia->setFile($this->getFile($originalRendition, $file)); |
||
102 | |||
103 | return $articleMedia; |
||
104 | } |
||
105 | |||
106 | protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface |
||
107 | { |
||
108 | if (0 === $item->getRenditions()->count()) { |
||
109 | return $articleMedia; |
||
110 | } |
||
111 | |||
112 | $originalRendition = $this->findOriginalRendition($item); |
||
113 | $articleMedia->setMimetype($originalRendition->getMimetype()); |
||
114 | $articleMedia->setKey($key); |
||
115 | |||
116 | /** @var ImageInterface $image */ |
||
117 | $image = $this->getFile($originalRendition, $this->articleMediaAssetProvider->getImage($originalRendition)); |
||
118 | $articleMedia->setImage($image); |
||
119 | |||
120 | foreach ($item->getRenditions() as $rendition) { |
||
121 | $image = $this->getFile($rendition, $this->articleMediaAssetProvider->getImage($rendition)); |
||
122 | if (null === $image) { |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | $articleMedia->addRendition($this->imageRenditionFactory->createWith($articleMedia, $image, $rendition)); |
||
127 | } |
||
128 | |||
129 | return $articleMedia; |
||
130 | } |
||
131 | |||
132 | private function getFile(RenditionInterface $rendition, ?FileInterface $file): ?FileInterface |
||
133 | { |
||
134 | if (null !== $file) { |
||
135 | return $file; |
||
136 | } |
||
137 | |||
138 | try { |
||
139 | return $this->downloadAsset($rendition->getHref(), $rendition->getMedia(), $rendition->getMimetype()); |
||
140 | } catch (\Exception $e) { |
||
141 | $this->logger->error(\sprintf('%s: %s', $rendition->getHref(), $e->getMessage())); |
||
142 | |||
143 | return null; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | private function downloadAsset(string $url, string $media, string $mimetype): FileInterface |
||
148 | { |
||
149 | $this->logger->info(\sprintf('Downloading %s for media %s', $url, $media)); |
||
150 | $uploadedFile = $this->mediaManager->downloadFile($url, $media, $mimetype); |
||
151 | $file = $this->mediaManager->handleUploadedFile($uploadedFile, $media); |
||
152 | |||
153 | if ($file instanceof ImageInterface) { |
||
154 | list($width, $height) = \getimagesize($uploadedFile->getRealPath()); |
||
155 | $file->setWidth($width); |
||
156 | $file->setHeight($height); |
||
157 | } |
||
158 | |||
159 | return $file; |
||
160 | } |
||
161 | |||
162 | private function findOriginalRendition(ItemInterface $item): RenditionInterface |
||
170 | } |
||
171 |