Completed
Push — master ( 9107b2...4da1f4 )
by Paweł
19s
created

MediaFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 1
cts 1
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 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 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);
0 ignored issues
show
Bug introduced by
It seems like $image defined by $this->findImage($originalRendition->getMedia()) on line 119 can be null; however, SWP\Bundle\ContentBundle...rticleMedia::setImage() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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);
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...
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