Passed
Push — master ( 261f35...f81515 )
by Gerrit
09:50
created

GenericEntityFetchController::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 9.488
c 0
b 0
f 0
cc 3
nc 1
nop 2
crap 3
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
28
final class GenericEntityFetchController
29
{
30
    use ApplyDataTemplateTrait;
31
32
    /**
33
     * @var ControllerHelperInterface
34
     */
35
    private $controllerHelper;
36
37
    /**
38
     * @var EncoderInterface|null
39
     */
40
    private $encoder;
41
42
    /**
43
     * @var NormalizerInterface|null
44
     */
45
    private $normalizer;
46
47
    /**
48
     * @var array|null
49
     */
50
    private $dataTemplate;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $authorizationAttribute;
56
57
    /**
58
     * @var string
59
     */
60
    private $entityClass;
61
62
    /**
63
     * @var string
64
     */
65
    private $format;
66
67 11
    public function __construct(
68
        ControllerHelperInterface $controllerHelper,
69
        array $options
70
    ) {
71 11
        Assert::null($this->controllerHelper);
72 11
        Assert::keyExists($options, 'entity-class');
73
74 10
        $options = array_merge([
75 10
            'format' => 'json',
76
            'encoder' => null,
77
            'normalizer' => null,
78
            'data-template' => null,
79
            'authorization-attribute' => null
80 10
        ], $options);
81
82 10
        Assert::true(is_null($options['encoder']) || $options['encoder'] instanceof EncoderInterface);
83 9
        Assert::true(is_null($options['normalizer']) || $options['normalizer'] instanceof NormalizerInterface);
84 8
        Assert::classExists($options['entity-class']);
85
86 7
        $this->controllerHelper = $controllerHelper;
87 7
        $this->encoder = $options['encoder'];
88 7
        $this->normalizer = $options['normalizer'];
89 7
        $this->entityClass = $options['entity-class'];
90 7
        $this->format = $options['format'];
91 7
        $this->dataTemplate = $options['data-template'];
92 7
        $this->authorizationAttribute = $options['authorization-attribute'];
93 7
    }
94
95 6
    public function fetchEntity(string $entityId): Response
96
    {
97
        /** @var object|null $entity */
98 6
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
99
100 6
        if (is_null($entity)) {
101 1
            throw new InvalidArgumentException(sprintf(
102 1
                "Could not find entity with id '%s'!",
103 1
                $entityId
104
            ));
105
        }
106
107 5
        if (!empty($this->authorizationAttribute)) {
108 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity);
109
        }
110
111 4
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
112 4
            $this->entityClass,
113 4
            $entityId,
114 4
            $entity,
115 4
            "*FETCH*"
116
        ));
117
118
        /** @var array $normalizedEntity */
119 4
        $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...
120
121 4 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...
122 1
            $normalizedEntity = $this->normalizer->normalize($entity);
123
124
        } else {
125 3
            $normalizer = new ObjectNormalizer();
126
127 3
            $normalizedEntity = $normalizer->normalize($entity);
128
        }
129
130 4
        if (!is_array($normalizedEntity)) {
131 1
            throw new ErrorException("Result of normalize process must be an array!");
132
        }
133
134 3
        if (!is_null($this->dataTemplate)) {
135 2
            $normalizedEntity = $this->applyDataTemplate($normalizedEntity, $this->dataTemplate);
136
        }
137
138
        /** @var string $serializedEntity */
139 2
        $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...
140
141 2 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...
142
            $serializedEntity = $this->encoder->encode($normalizedEntity, $this->format);
143
144
        } else {
145 2
            $serializedEntity = json_encode($normalizedEntity);
146
        }
147
148 2
        return new Response($serializedEntity);
149
    }
150
151
}
152