Completed
Push — master ( bc414b...6634bd )
by Paweł
10:09
created

MediaFactory::createImageMedia()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 6
nc 5
nop 3
crap 42
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 Psr\Log\LoggerInterface;
20
use SWP\Bundle\ContentBundle\File\FileDownloaderInterface;
21
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
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\FileInterface;
26
use SWP\Bundle\ContentBundle\Model\ImageInterface;
27
use SWP\Bundle\ContentBundle\Provider\ORM\ArticleMediaAssetProviderInterface;
28
use SWP\Component\Bridge\Model\ItemInterface;
29
use SWP\Component\Bridge\Model\RenditionInterface;
30
use SWP\Component\Storage\Factory\FactoryInterface;
31
32
class MediaFactory implements MediaFactoryInterface
33
{
34
    protected $articleMediaAssetProvider;
35
36
    protected $factory;
37
38
    protected $imageRenditionFactory;
39
40
    protected $mediaManager;
41
42
    protected $logger;
43
44
    protected $fileDownloader;
45
46
    public function __construct(
47
        ArticleMediaAssetProviderInterface $articleMediaAssetProvider,
48
        FactoryInterface $factory,
49
        ImageRenditionFactoryInterface $imageRenditionFactory,
50
        MediaManagerInterface $mediaManager,
51
        LoggerInterface $logger,
52
        FileDownloaderInterface $fileDownloader
53
    ) {
54
        $this->articleMediaAssetProvider = $articleMediaAssetProvider;
55
        $this->factory = $factory;
56
        $this->imageRenditionFactory = $imageRenditionFactory;
57
        $this->mediaManager = $mediaManager;
58
        $this->logger = $logger;
59
        $this->fileDownloader = $fileDownloader;
60
    }
61
62
    public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface
63
    {
64
        /** @var ArticleMediaInterface $articleMedia */
65 12
        $articleMedia = $this->factory->create();
66
        $articleMedia->setArticle($article);
67
        $articleMedia->setFromItem($item);
68
69
        if (ItemInterface::TYPE_PICTURE === $item->getType()) {
70
            return $this->createImageMedia($articleMedia, $key, $item);
71 12
        }
72 12
73 12
        return $this->createFileMedia($articleMedia, $key, $item);
74 12
    }
75 12
76
    public function createEmpty(): ArticleMediaInterface
77
    {
78
        return $this->factory->create();
79
    }
80
81
    protected function createFileMedia(ArticleMediaInterface $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface
82
    {
83
        if (0 === $item->getRenditions()->count()) {
84
            return $articleMedia;
85
        }
86
87
        $originalRendition = $this->findOriginalRendition($item);
88
        $articleMedia->setMimetype($originalRendition->getMimetype());
89
        $articleMedia->setKey($key);
90
        $file = $this->articleMediaAssetProvider->getFile($originalRendition);
91
        $articleMedia->setFile($this->getFile($originalRendition, $file));
92
93
        return $articleMedia;
94
    }
95
96
    protected function createImageMedia(ArticleMediaInterface $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface
97
    {
98
        $articleMedia->setKey($key);
99
        $articleMedia->setMimetype('unknown');
100
        if (0 === $item->getRenditions()->count()) {
101
            return $articleMedia;
102
        }
103
104
        $originalRendition = $this->findOriginalRendition($item);
105
        $articleMedia->setMimetype($originalRendition->getMimetype());
106
        /** @var ImageInterface $image */
107
        $image = $this->getFile($originalRendition, $this->articleMediaAssetProvider->getImage($originalRendition));
108
        if (!$image instanceof ImageInterface) {
109
            return $articleMedia;
110
        }
111
        $articleMedia->setImage($image);
112
113
        foreach ($item->getRenditions() as $itemRendition) {
114
            $image = $this->getFile($itemRendition, $this->articleMediaAssetProvider->getImage($itemRendition));
115
            if (null === $image || !$image instanceof ImageInterface) {
116
                continue;
117
            }
118
119
            $articleMedia->addRendition($this->imageRenditionFactory->createWith($articleMedia, $image, $itemRendition));
120
        }
121
122
        return $articleMedia;
123
    }
124
125
    private function getFile(RenditionInterface $rendition, ?FileInterface $file): ?FileInterface
126
    {
127
        if (null !== $file) {
128
            return $file;
129
        }
130
131
        try {
132
            return $this->downloadAsset($rendition->getHref(), $rendition->getMedia(), $rendition->getMimetype());
133
        } catch (\Exception $e) {
134
            $this->logger->error(\sprintf('%s: %s', $rendition->getHref(), $e->getMessage()));
135
136
            return null;
137
        }
138
    }
139
140
    private function downloadAsset(string $url, string $media, string $mimetype): FileInterface
141
    {
142
        $this->logger->info(\sprintf('Downloading %s for media %s', $url, $media));
143
        $uploadedFile = $this->fileDownloader->download($url, $media, $mimetype);
144
        $file = $this->mediaManager->handleUploadedFile($uploadedFile, $media);
145
146
        if ($file instanceof ImageInterface) {
147
            [$width, $height] = \getimagesize($uploadedFile->getRealPath());
0 ignored issues
show
Bug introduced by
The variable $width does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $height does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
148
            $file->setWidth($width);
149
            $file->setHeight($height);
150
        }
151
152
        return $file;
153
    }
154
155
    private function findOriginalRendition(ItemInterface $item): RenditionInterface
156
    {
157
        return $item->getRenditions()->filter(
158
            static function (RenditionInterface $rendition) {
159
                return 'original' === $rendition->getName();
160
            }
161
        )->first();
162
    }
163
}
164