Passed
Push — master ( 5bc951...af3bd6 )
by Gerrit
01:59
created

invokeEntityMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 32
loc 32
ccs 16
cts 16
cp 1
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
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 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
24
final class GenericEntityInvokeController
25
{
26
27
    /**
28
     * @var ControllerHelperInterface
29
     */
30
    private $controllerHelper;
31
32
    /**
33
     * @var ArgumentCompilerInterface
34
     */
35
    private $argumentCompiler;
36
37
    /**
38
     * @var string
39
     */
40
    private $entityClass;
41
42
    /**
43
     * @var string
44
     */
45
    private $methodName;
46
47
    /**
48
     * @var array
49
     */
50
    private $arguments;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $denyAccessAttribute;
56
57 9
    public function __construct(
58
        ControllerHelperInterface $controllerHelper,
59
        ArgumentCompilerInterface $argumentCompiler,
60
        array $options
61
    ) {
62 9
        Assert::null($this->controllerHelper);
63 9
        Assert::keyExists($options, 'entity-class');
64 8
        Assert::keyExists($options, 'method');
65
66 7
        $options = array_merge([
67 7
            'arguments' => [],
68
            'deny-access-attribute' => null
69 7
        ], $options);
70
71 7
        Assert::classExists($options['entity-class']);
72 6
        Assert::methodExists($options['entity-class'], $options['method']);
73 5
        Assert::isArray($options['arguments']);
74
75 4
        $this->controllerHelper = $controllerHelper;
76 4
        $this->argumentCompiler = $argumentCompiler;
77 4
        $this->entityClass = $options['entity-class'];
78 4
        $this->methodName = $options['method'];
79 4
        $this->arguments = $options['arguments'];
80 4
        $this->denyAccessAttribute = $options['deny-access-attribute'];
81 4
    }
82
83 3 View Code Duplication
    public function invokeEntityMethod(Request $request, string $entityId): 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...
84
    {
85
        /** @var object|null $entity */
86 3
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
87
88 3
        if (is_null($entity)) {
89 1
            throw new InvalidArgumentException(sprintf(
90 1
                "Entity with id '%s' not found!",
91 1
                $entityId
92
            ));
93
        }
94
95 2
        if (!empty($this->denyAccessAttribute)) {
96 1
            $this->controllerHelper->denyAccessUnlessGranted($this->denyAccessAttribute, $entity);
97
        }
98
99 1
        $reflectionObject = new ReflectionObject($entity);
100
101
        /** @var ReflectionMethod $reflectionMethod */
102 1
        $reflectionMethod = $reflectionObject->getMethod($this->methodName);
103
104
        /** @var array $callArguments */
105 1
        $callArguments = $this->argumentCompiler->buildCallArguments(
106 1
            $reflectionMethod,
107 1
            $this->arguments,
108 1
            $request
109
        );
110
111 1
        $reflectionMethod->invokeArgs($entity, $callArguments);
112
113 1
        return new Response("Entity method invoked!");
114
    }
115
116
}
117