Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

GenericEntityFetchController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 38.19 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 55
loc 144
ccs 49
cts 50
cp 0.98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 29 29 3
A __invoke() 12 12 1
B fetchEntity() 14 55 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 InvalidArgumentException;
17
use Webmozart\Assert\Assert;
18
use Symfony\Component\Serializer\SerializerInterface;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Encoder\EncoderInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
use DOMDocument;
23
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
24
use ErrorException;
25
use Addiks\SymfonyGenerics\Controllers\ApplyDataTemplateTrait;
26
use Addiks\SymfonyGenerics\Events\EntityInteractionEvent;
27
use Symfony\Component\HttpFoundation\Request;
28
29
final class GenericEntityFetchController
30
{
31
    use ApplyDataTemplateTrait;
32
33
    /**
34
     * @var ControllerHelperInterface
35
     */
36
    private $controllerHelper;
37
38
    /**
39
     * @var EncoderInterface|null
40
     */
41
    private $encoder;
42
43
    /**
44
     * @var NormalizerInterface|null
45
     */
46
    private $normalizer;
47
48
    /**
49
     * @var array|null
50
     */
51
    private $dataTemplate;
52
53
    /**
54
     * @var string|null
55
     */
56
    private $authorizationAttribute;
57
58
    /**
59
     * @var string
60
     */
61
    private $entityClass;
62
63
    /**
64
     * @var string
65
     */
66
    private $entityIdKey;
67
68
    /**
69
     * @var string
70
     */
71
    private $format;
72
73 13 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
        ControllerHelperInterface $controllerHelper,
75
        array $options
76
    ) {
77 13
        Assert::null($this->controllerHelper);
78 13
        Assert::keyExists($options, 'entity-class');
79
80 12
        $options = array_merge([
81 12
            'format' => 'json',
82
            'encoder' => null,
83
            'normalizer' => null,
84
            'data-template' => null,
85
            'authorization-attribute' => null,
86
            'entity-id-key' => 'entityId',
87 12
        ], $options);
88
89 12
        Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface);
90 11
        Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface);
91 10
        Assert::classExists($options['entity-class']);
92
93 9
        $this->controllerHelper = $controllerHelper;
94 9
        $this->entityIdKey = $options['entity-id-key'];
95 9
        $this->encoder = $options['encoder'];
96 9
        $this->normalizer = $options['normalizer'];
97 9
        $this->entityClass = $options['entity-class'];
98 9
        $this->format = $options['format'];
99 9
        $this->dataTemplate = $options['data-template'];
100 9
        $this->authorizationAttribute = $options['authorization-attribute'];
101 9
    }
102
103 2 View Code Duplication
    public function __invoke(): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        /** @var Request $request */
106 2
        $request = $this->controllerHelper->getCurrentRequest();
107
108 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
109
110
        /** @var string $entityId */
111 1
        $entityId = (string)$request->get($this->entityIdKey);
112
113 1
        return $this->fetchEntity($entityId);
114
    }
115
116 7
    public function fetchEntity(string $entityId): Response
117
    {
118
        /** @var object|null $entity */
119 7
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
120
121 7
        if (is_null($entity)) {
122 1
            throw new InvalidArgumentException(sprintf(
123 1
                "Could not find entity with id '%s'!",
124 1
                $entityId
125
            ));
126
        }
127
128 6
        if (!empty($this->authorizationAttribute)) {
129 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity);
130
        }
131
132 5
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
133 5
            $this->entityClass,
134 5
            $entityId,
135 5
            $entity,
136 5
            "*FETCH*"
137
        ));
138
139
        /** @var array $normalizedEntity */
140 5
        $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...
141
142 5 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...
143 1
            $normalizedEntity = $this->normalizer->normalize($entity);
144
145
        } else {
146 4
            $normalizer = new ObjectNormalizer();
147
148 4
            $normalizedEntity = $normalizer->normalize($entity);
149
        }
150
151 5
        if (!is_array($normalizedEntity)) {
152 1
            throw new ErrorException("Result of normalize process must be an array!");
153
        }
154
155 4
        if (!is_null($this->dataTemplate)) {
156 2
            $normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate);
157
        }
158
159
        /** @var string $serializedEntity */
160 3
        $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...
161
162 3 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...
163
            $serializedEntity = $this->encoder->encode($normalizedEntity, $this->format);
164
165
        } else {
166 3
            $serializedEntity = json_encode($normalizedEntity);
167
        }
168
169 3
        return new Response($serializedEntity);
170
    }
171
172
}
173