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

RenditionLoader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 8
loc 75
ccs 6
cts 27
cp 0.2222
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C load() 8 40 11
A isSupported() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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