Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

GenericServiceInvokeController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 140
Duplicated Lines 6.43 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 9
loc 140
ccs 40
cts 45
cp 0.8889
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 1
A __invoke() 9 9 1
A callService() 0 45 4

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 8
    public function __construct(
79
        ControllerHelperInterface $controllerHelper,
80
        ArgumentCompilerInterface $argumentCompiler,
81
        ContainerInterface $container,
82
        array $options
83
    ) {
84 8
        Assert::null($this->controllerHelper);
85 8
        Assert::keyExists($options, 'service');
86 7
        Assert::keyExists($options, 'method');
87
88 6
        $options = array_merge([
89 6
            'arguments' => [],
90
            'authorization-attributes' => null,
91
            'success-redirect' => null,
92
            'success-redirect-arguments' => [],
93
            'success-redirect-status' => 303,
94 6
        ], $options);
95
96 6
        $this->controllerHelper = $controllerHelper;
97 6
        $this->argumentCompiler = $argumentCompiler;
98 6
        $this->container = $container;
99 6
        $this->serviceId = $options['service'];
100 6
        $this->method = $options['method'];
101 6
        $this->arguments = $options['arguments'];
102 6
        $this->authorizationAttribute = $options['authorization-attributes'];
103 6
        $this->successRedirectRoute = $options['success-redirect'];
104 6
        $this->successRedirectArguments = $options['success-redirect-arguments'];
105 6
        $this->successRedirectStatus = (int)$options['success-redirect-status'];
106 6
    }
107
108 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...
109
    {
110
        /** @var Request $request */
111 2
        $request = $this->controllerHelper->getCurrentRequest();
112
113 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
114
115 1
        return $this->callService($request);
116
    }
117
118 4
    public function callService(Request $request): Response
119
    {
120 4
        if (!is_null($this->authorizationAttribute)) {
121 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
122
        }
123
124
        /** @var object|null $service */
125 3
        $service = $this->container->get($this->serviceId);
126
127 3
        if (is_null($service)) {
128 1
            throw new ErrorException(sprintf(
129 1
                "Could not find service '%s'!",
130 1
                $this->serviceId
131
            ));
132
        }
133
134 2
        $reflectionObject = new ReflectionObject($service);
135
136
        /** @var ReflectionMethod $reflectionMethod */
137 2
        $reflectionMethod = $reflectionObject->getMethod($this->method);
138
139
        /** @var array $arguments */
140 2
        $arguments = $this->argumentCompiler->buildCallArguments(
141 2
            $reflectionMethod,
142 2
            $this->arguments,
143 2
            $request
144
        );
145
146 2
        $reflectionMethod->invokeArgs($service, $arguments);
147
148 2
        $this->controllerHelper->flushORM();
149
150 2
        if (!empty($this->successRedirectRoute)) {
151
            /** @var array $redirectArguments */
152
            $redirectArguments = $this->argumentCompiler->buildArguments($this->successRedirectArguments, $request);
153
154
            return $this->controllerHelper->redirectToRoute(
155
                $this->successRedirectRoute,
156
                $redirectArguments,
157
                $this->successRedirectStatus
158
            );
159
        }
160
161 2
        return new Response("Service call completed");
162
    }
163
164
}
165