Passed
Push — master ( 5bc951...af3bd6 )
by Gerrit
01:59
created

GenericEntityListingController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 10.29 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 14
loc 136
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 31 3
B listEntities() 14 53 7

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
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Controllers\API;
14
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Webmozart\Assert\Assert;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
20
use Addiks\SymfonyGenerics\Controllers\ApplyDataTemplateTrait;
21
use Symfony\Component\Serializer\Encoder\EncoderInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
use ErrorException;
24
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
25
26
final class GenericEntityListingController
27
{
28
    use ApplyDataTemplateTrait;
29
30
    /**
31
     * @var ControllerHelperInterface
32
     */
33
    private $controllerHelper;
34
35
    /**
36
     * @var ArgumentCompilerInterface
37
     */
38
    private $argumentCompiler;
39
40
    /**
41
     * @var string
42
     */
43
    private $entityClass;
44
45
    /**
46
     * @var array
47
     */
48
    private $criteria;
49
50
    /**
51
     * @var string|null
52
     */
53
    private $authorizationAttribute;
54
55
    /**
56
     * @var string
57
     */
58
    private $format;
59
60
    /**
61
     * @var EncoderInterface|null
62
     */
63
    private $encoder;
64
65
    /**
66
     * @var NormalizerInterface|null
67
     */
68
    private $normalizer;
69
70
    /**
71
     * @var array|null
72
     */
73
    private $dataTemplate;
74
75 8
    public function __construct(
76
        ControllerHelperInterface $controllerHelper,
77
        ArgumentCompilerInterface $argumentCompiler,
78
        array $options
79
    ) {
80 8
        Assert::null($this->controllerHelper);
81 8
        Assert::keyExists($options, 'entity-class');
82 7
        Assert::classExists($options['entity-class']);
83
84 6
        $options = array_merge([
85 6
            'format' => 'json',
86
            'criteria' => [],
87
            'authorization-attribute' => null,
88
            'encoder' => null,
89
            'normalizer' => null,
90
            'data-template' => null,
91 6
        ], $options);
92
93 6
        Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface);
94 5
        Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface);
95
96 4
        $this->controllerHelper = $controllerHelper;
97 4
        $this->argumentCompiler = $argumentCompiler;
98 4
        $this->entityClass = $options['entity-class'];
99 4
        $this->criteria = $options['criteria'];
100 4
        $this->encoder = $options['encoder'];
101 4
        $this->normalizer = $options['normalizer'];
102 4
        $this->format = $options['format'];
103 4
        $this->dataTemplate = $options['data-template'];
104 4
        $this->authorizationAttribute = $options['authorization-attribute'];
105 4
    }
106
107 3
    public function listEntities(Request $request): Response
108
    {
109 3
        if (!is_null($this->authorizationAttribute)) {
110 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
111
        }
112
113
        /** @var array $criteria */
114 2
        $criteria = $this->argumentCompiler->buildArguments($this->criteria, $request);
115
116
        /** @var array<object> $entities */
117 2
        $entities = $this->controllerHelper->findEntities($this->entityClass, $criteria);
118
119
        /** @var array<int, array> $resultData */
120 2
        $resultEntries = array();
121
122 2
        foreach ($entities as $entity) {
123
            /** @var object $entity */
124
125
            /** @var array $normalizedEntity */
126 2
            $normalizedEntity = array();
0 ignored issues
show
Unused Code introduced by
$normalizedEntity 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...
127
128 2 View Code Duplication
            if ($this->normalizer instanceof NormalizerInterface) {
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...
129 1
                $normalizedEntity = $this->normalizer->normalize($entity);
130
131
            } else {
132 1
                $normalizer = new ObjectNormalizer();
133
134 1
                $normalizedEntity = $normalizer->normalize($entity);
135
            }
136
137 2
            if (!is_array($normalizedEntity)) {
138 1
                throw new ErrorException("Result of normalize process must be an array!");
139
            }
140
141 1
            if (!is_null($this->dataTemplate)) {
142 1
                $normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate);
143
            }
144
145 1
            $resultEntries[] = $normalizedEntity;
146
        }
147
148
        /** @var string $serializedEntity */
149 1
        $serializedEntity = "";
0 ignored issues
show
Unused Code introduced by
$serializedEntity 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...
150
151 1 View Code Duplication
        if ($this->encoder instanceof EncoderInterface) {
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...
152
            $serializedEntity = $this->encoder->encode($resultEntries, $this->format);
153
154
        } else {
155 1
            $serializedEntity = json_encode($resultEntries);
156
        }
157
158 1
        return new Response($serializedEntity);
159
    }
160
161
}
162