Passed
Push — master ( 80fb70...e0e012 )
by Gerrit
04:17
created

GenericServiceInvokeController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 155
Duplicated Lines 5.81 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.96%

Importance

Changes 0
Metric Value
dl 9
loc 155
ccs 48
cts 49
cp 0.9796
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 34 1
A __invoke() 9 9 1
B callService() 0 50 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Webmozart\Assert\Assert;
17
use Psr\Container\ContainerInterface;
18
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpFoundation\Request;
21
use ErrorException;
22
use ReflectionObject;
23
use ReflectionMethod;
24
25
final class GenericServiceInvokeController
26
{
27
28
    /**
29
     * @var ControllerHelperInterface
30
     */
31
    private $controllerHelper;
32
33
    /**
34
     * @var ArgumentCompilerInterface
35
     */
36
    private $argumentCompiler;
37
38
    /**
39
     * @var ContainerInterface
40
     */
41
    private $container;
42
43
    /**
44
     * @var string
45
     */
46
    private $serviceId;
47
48
    /**
49
     * @var string
50
     */
51
    private $method;
52
53
    /**
54
     * @var array
55
     */
56
    private $arguments;
57
58
    /**
59
     * @var string|null
60
     */
61
    private $authorizationAttribute;
62
63
    /**
64
     * @var string|null
65
     */
66
    private $successRedirectRoute;
67
68
    /**
69
     * @var array
70
     */
71
    private $successRedirectArguments;
72
73
    /**
74
     * @var integer
75
     */
76
    private $successRedirectStatus;
77
78
    /**
79
     * @var bool
80
     */
81
    private $sendReturnValueInResponse = false;
82
83 9
    public function __construct(
84
        ControllerHelperInterface $controllerHelper,
85
        ArgumentCompilerInterface $argumentCompiler,
86
        ContainerInterface $container,
87
        array $options
88
    ) {
89 9
        Assert::null($this->controllerHelper);
90 9
        Assert::keyExists($options, 'service');
91 8
        Assert::keyExists($options, 'method');
92
93
        /** @var int $defaultRedirectStatus */
94 7
        $defaultRedirectStatus = 303;
95
96 7
        $options = array_merge([
97 7
            'arguments' => [],
98
            'authorization-attributes' => null,
99
            'success-redirect' => null,
100
            'success-redirect-arguments' => [],
101 7
            'success-redirect-status' => $defaultRedirectStatus,
102
            'send-return-value-in-response' => false,
103 7
        ], $options);
104
105 7
        $this->controllerHelper = $controllerHelper;
106 7
        $this->argumentCompiler = $argumentCompiler;
107 7
        $this->container = $container;
108 7
        $this->serviceId = $options['service'];
109 7
        $this->method = $options['method'];
110 7
        $this->arguments = $options['arguments'];
111 7
        $this->authorizationAttribute = $options['authorization-attributes'];
112 7
        $this->successRedirectRoute = $options['success-redirect'];
113 7
        $this->successRedirectArguments = $options['success-redirect-arguments'];
114 7
        $this->successRedirectStatus = $options['success-redirect-status'];
115 7
        $this->sendReturnValueInResponse = $options['send-return-value-in-response'];
116 7
    }
117
118 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...
119
    {
120
        /** @var Request $request */
121 2
        $request = $this->controllerHelper->getCurrentRequest();
122
123 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
124
125 1
        return $this->callService($request);
126
    }
127
128 5
    public function callService(Request $request): Response
129
    {
130 5
        if (!is_null($this->authorizationAttribute)) {
131 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
132
        }
133
134
        /** @var object|null $service */
135 4
        $service = $this->container->get($this->serviceId);
136
137 4
        if (is_null($service)) {
138 1
            throw new ErrorException(sprintf(
139 1
                "Could not find service '%s'!",
140 1
                $this->serviceId
141
            ));
142
        }
143
144 3
        $reflectionObject = new ReflectionObject($service);
145
146
        /** @var ReflectionMethod $reflectionMethod */
147 3
        $reflectionMethod = $reflectionObject->getMethod($this->method);
148
149
        /** @var array $arguments */
150 3
        $arguments = $this->argumentCompiler->buildCallArguments(
151 3
            $reflectionMethod,
152 3
            $this->arguments
153
        );
154
155
        /** @var mixed $returnValue */
156 3
        $returnValue = $reflectionMethod->invokeArgs($service, $arguments);
157
158 3
        $this->controllerHelper->flushORM();
159
160 3
        if (!empty($this->successRedirectRoute)) {
161
            /** @var array $redirectArguments */
162 1
            $redirectArguments = $this->argumentCompiler->buildArguments($this->successRedirectArguments);
163
164 1
            return $this->controllerHelper->redirectToRoute(
165 1
                $this->successRedirectRoute,
166 1
                $redirectArguments,
167 1
                $this->successRedirectStatus
168
            );
169
        }
170
171 2
        if ($this->sendReturnValueInResponse) {
172
            return new Response((string)$returnValue);
173
174
        } else {
175 2
            return new Response("Service call completed");
176
        }
177
    }
178
179
}
180