Passed
Push — master ( 5a8e15...a47b7c )
by Gerrit
16:08
created

GenericServiceInvokeController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 32
loc 96
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A callService() 32 32 3

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;
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 6
    public function __construct(
64
        ControllerHelperInterface $controllerHelper,
65
        ArgumentCompilerInterface $argumentCompiler,
66
        ContainerInterface $container,
67
        array $options
68
    ) {
69 6
        Assert::null($this->controllerHelper);
70 6
        Assert::keyExists($options, 'service');
71 5
        Assert::keyExists($options, 'method');
72
73 4
        $options = array_merge([
74 4
            'arguments' => [],
75
            'authorization-attributes' => null,
76 4
        ], $options);
77
78 4
        $this->controllerHelper = $controllerHelper;
79 4
        $this->argumentCompiler = $argumentCompiler;
80 4
        $this->container = $container;
81 4
        $this->serviceId = $options['service'];
82 4
        $this->method = $options['method'];
83 4
        $this->arguments = $options['arguments'];
84 4
        $this->authorizationAttribute = $options['authorization-attributes'];
85 4
    }
86
87 3 View Code Duplication
    public function callService(Request $request): 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...
88
    {
89 3
        if (!is_null($this->authorizationAttribute)) {
90 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
91
        }
92
93
        /** @var object|null $service */
94 2
        $service = $this->container->get($this->serviceId);
95
96 2
        if (is_null($service)) {
97 1
            throw new ErrorException(sprintf(
98 1
                "Could not find service '%s'!",
99 1
                $this->serviceId
100
            ));
101
        }
102
103 1
        $reflectionObject = new ReflectionObject($service);
104
105
        /** @var ReflectionMethod $reflectionMethod */
106 1
        $reflectionMethod = $reflectionObject->getMethod($this->method);
107
108
        /** @var array $arguments */
109 1
        $arguments = $this->argumentCompiler->buildCallArguments(
110 1
            $reflectionMethod,
111 1
            $this->arguments,
112 1
            $request
113
        );
114
115 1
        $reflectionMethod->invokeArgs($service, $arguments);
116
117 1
        return new Response("Service call completed");
118
    }
119
120
}
121