Passed
Push — master ( 5e3deb...621a9a )
by Gerrit
07:56 queued 05:39
created

invokeEntityMethod()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 6.042

Importance

Changes 0
Metric Value
cc 6
nc 18
nop 1
dl 0
loc 69
ccs 34
cts 38
cp 0.8947
crap 6.042
rs 8.0541
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 $entityIdSource;
52
53
    /**
54
     * @var string
55
     */
56
    private $methodName;
57
58
    /**
59
     * @var array
60
     */
61
    private $arguments;
62
63
    /**
64
     * @var string|null
65
     */
66
    private $denyAccessAttribute;
67
68
    /**
69
     * @var string
70
     */
71
    private $successMessage;
72
73
    /**
74
     * @var string|null
75
     */
76
    private $redirectRoute;
77
78
    /**
79
     * @var array
80
     */
81
    private $redirectRouteParameters;
82
83
    /**
84
     * @var int
85
     */
86
    private $redirectStatus;
87
88
    /**
89
     * @var bool
90
     */
91
    private $sendReturnValueInResponse = false;
92
93 12
    public function __construct(
94
        ControllerHelperInterface $controllerHelper,
95
        ArgumentCompilerInterface $argumentCompiler,
96
        array $options
97
    ) {
98 12
        Assert::null($this->controllerHelper);
99 12
        Assert::keyExists($options, 'entity-class');
100 11
        Assert::keyExists($options, 'method');
101
102 10
        $options = array_merge([
103 10
            'arguments' => [],
104
            'deny-access-attribute' => null,
105
            'success-message' => "Entity method invoked!",
106
            'redirect-route' => null,
107
            'redirect-route-parameters' => [],
108
            'redirect-status' => 301,
109
            'entity-id-key' => 'entityId',
110
            'entity-id-source' => 'request',
111
            'send-return-value-in-response' => false,
112 10
        ], $options);
113
114 10
        Assert::classExists($options['entity-class']);
115 9
        Assert::methodExists($options['entity-class'], $options['method']);
116 8
        Assert::isArray($options['arguments'], 'Method-arguments must be array!');
117 7
        Assert::oneOf($options['entity-id-source'], ['request', 'argument']);
118
119 7
        $this->controllerHelper = $controllerHelper;
120 7
        $this->argumentCompiler = $argumentCompiler;
121 7
        $this->entityClass = $options['entity-class'];
122 7
        $this->entityIdKey = $options['entity-id-key'];
123 7
        $this->entityIdSource = $options['entity-id-source'];
124 7
        $this->methodName = $options['method'];
125 7
        $this->arguments = $options['arguments'];
126 7
        $this->denyAccessAttribute = $options['deny-access-attribute'];
127 7
        $this->successMessage = $options['success-message'];
128 7
        $this->redirectRoute = $options['redirect-route'];
129 7
        $this->redirectStatus = $options['redirect-status'];
130 7
        $this->redirectRouteParameters = $options['redirect-route-parameters'];
131 7
        $this->sendReturnValueInResponse = $options['send-return-value-in-response'];
132 7
    }
133
134 2
    public function __invoke(): Response
135
    {
136
        /** @var Request $request */
137 2
        $request = $this->controllerHelper->getCurrentRequest();
138
139 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
140
141
        /** @var Response $response */
142 1
        $response = null;
143
144 1
        if ($this->entityIdSource === 'request') {
145
            /** @var string $entityId */
146 1
            $entityId = $request->get($this->entityIdKey);
147
148 1
            $response = $this->invokeEntityMethod($entityId);
149
150
        } elseif ($this->entityIdSource === 'argument') {
151
            $response = $this->invokeEntityMethod('');
152
        }
153
154 1
        return $response;
155
156
    }
157
158 5
    public function invokeEntityMethod(string $entityId): Response
159
    {
160
        /** @var object|null $entity */
161 5
        $entity = null;
162
163 5
        if ($this->entityIdSource === 'request') {
164 5
            $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
165 5
            Assert::object($entity, sprintf("Entity with id '%s' not found!", $entityId));
166
167
        } elseif ($this->entityIdSource === 'argument') {
168
            $entity = $this->argumentCompiler->buildArgument($this->entityIdKey);
169
            Assert::object($entity, "Entity not found!");
170
        }
171
172 4
        Assert::isInstanceOf($entity, $this->entityClass, sprintf(
173 4
            "Found entity is not of expected class '%s', but of class '%s' instead!",
174 4
            $this->entityClass,
175 4
            get_class($entity)
176
        ));
177
178 4
        if (!empty($this->denyAccessAttribute)) {
179 1
            $this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity);
180
        }
181
182 3
        $reflectionObject = new ReflectionObject($entity);
183
184
        /** @var ReflectionMethod $reflectionMethod */
185 3
        $reflectionMethod = $reflectionObject->getMethod($this->methodName);
186
187
        /** @var array $callArguments */
188 3
        $callArguments = $this->argumentCompiler->buildCallArguments(
189 3
            $reflectionMethod,
190 3
            $this->arguments
191
        );
192
193 3
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
194 3
            $this->entityClass,
195 3
            $entityId,
196 3
            $entity,
197 3
            $this->methodName,
198 3
            $callArguments
199
        ));
200
201
        /** @var mixed $result */
202 3
        $result = $reflectionMethod->invokeArgs($entity, $callArguments);
203
204 3
        $this->controllerHelper->flushORM();
205
206
        /** @var Response $response */
207 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...
208
209 3
        if ($this->sendReturnValueInResponse) {
210
            return new Response((string)$result);
211
212 3
        } elseif (is_null($this->redirectRoute)) {
213 2
            $response = new Response($this->successMessage);
214
215
        } else {
216 1
            $response = $this->controllerHelper->redirectToRoute(
217 1
                $this->redirectRoute,
218 1
                $this->argumentCompiler->buildArguments($this->redirectRouteParameters, [
219 1
                    'result' => $result
220
                ]),
221 1
                $this->redirectStatus
222
            );
223
        }
224
225 3
        return $response;
226
    }
227
228
}
229