Test Failed
Push — master ( 603a4c...197ba0 )
by Gerrit
03:37
created

GenericEntityInvokeController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 5
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
17
use Webmozart\Assert\Assert;
18
use InvalidArgumentException;
19
use ReflectionObject;
20
use ReflectionMethod;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Addiks\SymfonyGenerics\Events\EntityInteractionEvent;
24
25
final class GenericEntityInvokeController
26
{
27
28
    /**
29
     * @var ControllerHelperInterface
30
     */
31
    private $controllerHelper;
32
33
    /**
34
     * @var ArgumentCompilerInterface
35
     */
36
    private $argumentCompiler;
37
38
    /**
39
     * @var string
40
     */
41
    private $entityClass;
42
43
    /**
44
     * @var string
45
     */
46
    private $entityIdKey;
47
48
    /**
49
     * @var string
50
     */
51
    private $methodName;
52
53
    /**
54
     * @var array
55
     */
56
    private $arguments;
57
58
    /**
59
     * @var string|null
60
     */
61
    private $denyAccessAttribute;
62
63
    /**
64
     * @var string
65
     */
66
    private $successMessage;
67
68
    /**
69
     * @var string|null
70
     */
71
    private $redirectRoute;
72
73
    /**
74
     * @var array
75
     */
76
    private $redirectRouteParameters;
77
78
    /**
79
     * @var bool
80
     */
81
    private $sendReturnValueInResponse = false;
82
83 12
    public function __construct(
84
        ControllerHelperInterface $controllerHelper,
85
        ArgumentCompilerInterface $argumentCompiler,
86
        array $options
87
    ) {
88 12
        Assert::null($this->controllerHelper);
89 12
        Assert::keyExists($options, 'entity-class');
90 11
        Assert::keyExists($options, 'method');
91
92 10
        $options = array_merge([
93 10
            'arguments' => [],
94
            'deny-access-attribute' => null,
95
            'success-message' => "Entity method invoked!",
96
            'redirect-route' => null,
97
            'redirect-route-parameters' => [],
98
            'entity-id-key' => 'entityId',
99
            'send-return-value-in-response' => false,
100 10
        ], $options);
101
102 10
        Assert::classExists($options['entity-class']);
103 9
        Assert::methodExists($options['entity-class'], $options['method']);
104 8
        Assert::isArray($options['arguments'], 'Method-arguments must be array!');
105
106 7
        $this->controllerHelper = $controllerHelper;
107 7
        $this->argumentCompiler = $argumentCompiler;
108 7
        $this->entityClass = $options['entity-class'];
109 7
        $this->entityIdKey = $options['entity-id-key'];
110 7
        $this->methodName = $options['method'];
111 7
        $this->arguments = $options['arguments'];
112 7
        $this->denyAccessAttribute = $options['deny-access-attribute'];
113 7
        $this->successMessage = $options['success-message'];
114 7
        $this->redirectRoute = $options['redirect-route'];
115 7
        $this->redirectRouteParameters = $options['redirect-route-parameters'];
116 7
        $this->sendReturnValueInResponse = $options['send-return-value-in-response'];
117 7
    }
118
119 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...
120
    {
121
        /** @var Request $request */
122 2
        $request = $this->controllerHelper->getCurrentRequest();
123
124 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
125
126
        /** @var string $entityId */
127 1
        $entityId = $request->get($this->entityIdKey);
128
129 1
        return $this->invokeEntityMethod($entityId);
130
    }
131
132 5
    public function invokeEntityMethod(string $entityId): Response
133
    {
134
        /** @var object|null $entity */
135 5
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
136
137 5
        if (is_null($entity)) {
138 1
            throw new InvalidArgumentException(sprintf(
139 1
                "Entity with id '%s' not found!",
140 1
                $entityId
141
            ));
142
        }
143
144 4
        if (!empty($this->denyAccessAttribute)) {
145 1
            $this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity);
146
        }
147
148 3
        $reflectionObject = new ReflectionObject($entity);
149
150
        /** @var ReflectionMethod $reflectionMethod */
151 3
        $reflectionMethod = $reflectionObject->getMethod($this->methodName);
152
153
        /** @var array $callArguments */
154 3
        $callArguments = $this->argumentCompiler->buildCallArguments(
155 3
            $reflectionMethod,
156 3
            $this->arguments
157
        );
158
159 3
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
160 3
            $this->entityClass,
161 3
            $entityId,
162 3
            $entity,
163 3
            $this->methodName,
164 3
            $callArguments
165
        ));
166
167
        /** @var mixed $result */
168 3
        $result = $reflectionMethod->invokeArgs($entity, $callArguments);
169
170 3
        $this->controllerHelper->flushORM();
171
172
        /** @var Response $response */
173 3
        $response = null;
0 ignored issues
show
Unused Code introduced by
$response 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...
174
175 3
        if ($this->sendReturnValueInResponse) {
176
            return new Response((string)$result);
177
178 3
        } elseif (is_null($this->redirectRoute)) {
179 2
            $response = new Response($this->successMessage);
180
181
        } else {
182 1
            $response = $this->controllerHelper->redirectToRoute(
183 1
                $this->redirectRoute,
184 1
                $this->argumentCompiler->buildArguments($this->redirectRouteParameters, [
185 1
                    'result' => $result
186
                ])
187
            );
188
        }
189
190 3
        return $response;
191
    }
192
193
}
194