Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
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 11
    public function __construct(
79
        ControllerHelperInterface $controllerHelper,
80
        ArgumentCompilerInterface $argumentCompiler,
81
        array $options
82
    ) {
83 11
        Assert::null($this->controllerHelper);
84 11
        Assert::keyExists($options, 'entity-class');
85 10
        Assert::keyExists($options, 'method');
86
87 9
        $options = array_merge([
88 9
            'arguments' => [],
89
            'deny-access-attribute' => null,
90
            'success-message' => "Entity method invoked!",
91
            'redirect-route' => null,
92
            'redirect-route-parameters' => [],
93
            'entity-id-key' => 'entityId',
94 9
        ], $options);
95
96 9
        Assert::classExists($options['entity-class']);
97 8
        Assert::methodExists($options['entity-class'], $options['method']);
98 7
        Assert::isArray($options['arguments'], 'Method-arguments must be array!');
99
100 6
        $this->controllerHelper = $controllerHelper;
101 6
        $this->argumentCompiler = $argumentCompiler;
102 6
        $this->entityClass = $options['entity-class'];
103 6
        $this->entityIdKey = $options['entity-id-key'];
104 6
        $this->methodName = $options['method'];
105 6
        $this->arguments = $options['arguments'];
106 6
        $this->denyAccessAttribute = $options['deny-access-attribute'];
107 6
        $this->successMessage = $options['success-message'];
108 6
        $this->redirectRoute = $options['redirect-route'];
109 6
        $this->redirectRouteParameters = $options['redirect-route-parameters'];
110 6
    }
111
112 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...
113
    {
114
        /** @var Request $request */
115 2
        $request = $this->controllerHelper->getCurrentRequest();
116
117 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
118
119
        /** @var string $entityId */
120 1
        $entityId = (string)$request->get($this->entityIdKey);
121
122 1
        return $this->invokeEntityMethod($request, $entityId);
123
    }
124
125 4
    public function invokeEntityMethod(Request $request, string $entityId): Response
126
    {
127
        /** @var object|null $entity */
128 4
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
129
130 4
        if (is_null($entity)) {
131 1
            throw new InvalidArgumentException(sprintf(
132 1
                "Entity with id '%s' not found!",
133 1
                $entityId
134
            ));
135
        }
136
137 3
        if (!empty($this->denyAccessAttribute)) {
138 1
            $this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity);
139
        }
140
141 2
        $reflectionObject = new ReflectionObject($entity);
142
143
        /** @var ReflectionMethod $reflectionMethod */
144 2
        $reflectionMethod = $reflectionObject->getMethod($this->methodName);
145
146
        /** @var array $callArguments */
147 2
        $callArguments = $this->argumentCompiler->buildCallArguments(
148 2
            $reflectionMethod,
149 2
            $this->arguments,
150 2
            $request
151
        );
152
153 2
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
154 2
            $this->entityClass,
155 2
            $entityId,
156 2
            $entity,
157 2
            $this->methodName,
158 2
            $callArguments
159
        ));
160
161
        /** @var mixed $result */
162 2
        $result = $reflectionMethod->invokeArgs($entity, $callArguments);
163
164 2
        $this->controllerHelper->flushORM();
165
166
        /** @var Response $response */
167 2
        $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...
168
169 2
        if (is_null($this->redirectRoute)) {
170 2
            $response = new Response($this->successMessage);
171
172
        } else {
173
            $response = $this->controllerHelper->redirectToRoute(
174
                $this->redirectRoute,
175
                $this->argumentCompiler->buildArguments($this->redirectRouteParameters, $request, [
176
                    'result' => $result
177
                ])
178
            );
179
        }
180
181 2
        return $response;
182
    }
183
184
}
185