Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

RenditionLoader::load()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 40
Code Lines 22

Duplication

Lines 8
Ratio 20 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 8
loc 40
ccs 0
cts 21
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 22
nc 14
nop 3
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 2015 Sourcefabric z.u. 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 2015 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Loader;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
20
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface;
21
use SWP\Component\TemplatesSystem\Gimme\Context\Context;
22
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
23
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
25
26
/**
27
 * Class RenditionLoader.
28
 */
29
class RenditionLoader implements LoaderInterface
30
{
31
    /**
32
     * Fallback rendition name.
33
     */
34
    const FALLBACK_NAME = 'original';
35
36
    /**
37
     * @var Context
38
     */
39
    protected $templateContext;
40
41
    /**
42
     * @var MetaFactoryInterface
43
     */
44
    protected $metaFactory;
45
46 109
    public function __construct(Context $templateContext, MetaFactoryInterface $metaFactory)
47
    {
48 109
        $this->templateContext = $templateContext;
49 109
        $this->metaFactory = $metaFactory;
50 109
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function load($metaType, $parameters = [], $responseType = self::SINGLE)
56
    {
57
        if ($responseType === LoaderInterface::SINGLE) {
58
            $renditionName = null;
0 ignored issues
show
Unused Code introduced by
$renditionName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
            $fallbackRenditionName = self::FALLBACK_NAME;
60
            $fallbackRendition = null;
61
            if (!$this->templateContext['articleMedia'] instanceof Meta) {
62
                return false;
63
            }
64
            /** @var ArticleMediaInterface $articleMedia */
65
            $articleMedia = $this->templateContext['articleMedia']->getValues();
66
67 View Code Duplication
            if (array_key_exists('fallback', $parameters) && is_string($parameters['fallback'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                $fallbackRenditionName = $parameters['fallback'];
69
            }
70
71 View Code Duplication
            if (array_key_exists('name', $parameters) && is_string($parameters['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                $renditionName = $parameters['name'];
73
            } else {
74
                $renditionName = $fallbackRenditionName;
75
            }
76
77
            /** @var ImageRenditionInterface $rendition */
78
            foreach ($articleMedia->getRenditions() as $rendition) {
79
                if ($rendition->getName() === $renditionName) {
80
                    return $this->metaFactory->create($rendition);
81
                }
82
83
                if ($rendition->getName() === $fallbackRenditionName) {
84
                    $fallbackRendition = $rendition;
85
                }
86
            }
87
88
            if (null !== $fallbackRendition) {
89
                return $this->metaFactory->create($fallbackRendition);
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 11
    public function isSupported(string $type): bool
100
    {
101 11
        return in_array($type, ['rendition']);
102
    }
103
}
104