Completed
Pull Request — 1.4 (#705)
by Paweł
08:56
created

MediaFactory::findOriginalRendition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Manager\MediaManagerInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
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\Image;
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
    /**
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(
60
        ArticleMediaAssetProviderInterface $articleMediaAssetProvider,
61
        FactoryInterface $factory,
62
        ImageRenditionFactoryInterface $imageRenditionFactory,
63
        MediaManagerInterface $mediaManager,
64
        LoggerInterface $logger
65
    ) {
66
        $this->articleMediaAssetProvider = $articleMediaAssetProvider;
67
        $this->factory = $factory;
68
        $this->imageRenditionFactory = $imageRenditionFactory;
69
        $this->mediaManager = $mediaManager;
70
        $this->logger = $logger;
71
    }
72
73
    public function create(ArticleInterface $article, string $key, ItemInterface $item): ArticleMediaInterface
74
    {
75
        $articleMedia = $this->factory->create();
76
        $articleMedia->setArticle($article);
77
        $articleMedia->setFromItem($item);
78
79
        if (ItemInterface::TYPE_PICTURE === $item->getType()) {
80
            return $this->createImageMedia($articleMedia, $key, $item);
81
        }
82
83
        return $this->createFileMedia($articleMedia, $key, $item);
84
    }
85
86
    public function createEmpty(): ArticleMediaInterface
87
    {
88
        return $this->factory->create();
89
    }
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
99
        $articleMedia->setMimetype($originalRendition->getMimetype());
100
        $articleMedia->setKey($key);
101
102
        $file = $this->articleMediaAssetProvider->getFile($originalRendition);
103
        $articleMedia->setFile($file);
104
105
        return $articleMedia;
106
    }
107
108
    protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item): ArticleMediaInterface
109
    {
110
        if (0 === $item->getRenditions()->count()) {
111
            return $articleMedia;
112
        }
113
114
        $originalRendition = $this->findOriginalRendition($item);
115
116
        $articleMedia->setMimetype($originalRendition->getMimetype());
117
        $articleMedia->setKey($key);
118
119
        $image = $this->getImage($originalRendition);
120
        $articleMedia->setImage($image);
121
122
        foreach ($item->getRenditions() as $rendition) {
123
            $image = $this->getImage($rendition);
124
125
            if (null === $image) {
126
                continue;
127
            }
128
129
            $imageRendition = $this->imageRenditionFactory->createWith($articleMedia, $image, $rendition);
130
            $articleMedia->addRendition($imageRendition);
0 ignored issues
show
Compatibility introduced by
$imageRendition of type object<SWP\Bundle\Conten...mageRenditionInterface> is not a sub-type of object<SWP\Bundle\Conten...e\Model\ImageRendition>. It seems like you assume a concrete implementation of the interface SWP\Bundle\ContentBundle...ImageRenditionInterface to be always present.

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.

Loading history...
131
        }
132
133
        return $articleMedia;
134
    }
135
136
    public function getImage(RenditionInterface $rendition): ?ImageInterface
137
    {
138
        $file = $this->articleMediaAssetProvider->getImage($rendition);
139
        if (null !== $file) {
140
            return $file;
141
        }
142
143
        try {
144
            $uploadedFile = $this->mediaManager->downloadFile(
145
                $rendition->getHref(),
146
                $rendition->getMedia(),
147
                $rendition->getMimetype()
148
            );
149
        } catch (\Exception $e) {
150
            $this->logger->error(\sprintf('%s: %s', $rendition->getHref(), $e->getMessage()));
151
152
            return null;
153
        }
154
        /** @var Image $image */
155
        $image = $this->mediaManager->handleUploadedFile($uploadedFile, $rendition->getMedia());
156
157
        list($width, $height) = \getimagesize($uploadedFile->getRealPath());
158
        $image->setWidth($width);
159
        $image->setHeight($height);
160
161
        return $image;
162
    }
163
164
    private function findOriginalRendition(ItemInterface $item): RenditionInterface
165
    {
166
        return $item->getRenditions()->filter(
167
            function (RenditionInterface $rendition) {
168
                return 'original' === $rendition->getName();
169
            }
170
        )->first();
171
    }
172
}
173