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

GenericTemplateRenderController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 39.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 28
loc 71
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 19 19 1
A __invoke() 9 9 1
A renderTemplate() 0 11 2

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 Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
17
use Webmozart\Assert\Assert;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
final class GenericTemplateRenderController
22
{
23
24
    /**
25
     * @var ControllerHelperInterface
26
     */
27
    private $controllerHelper;
28
29
    /**
30
     * @var ArgumentCompilerInterface
31
     */
32
    private $argumentCompiler;
33
34
    /**
35
     * @var string
36
     */
37
    private $templatePath;
38
39
    /**
40
     * @var array
41
     */
42
    private $arguments;
43
44
    /**
45
     * @var string|null
46
     */
47
    private $authorizationAttribute;
48
49 6 View Code Duplication
    public function __construct(
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...
50
        ControllerHelperInterface $controllerHelper,
51
        ArgumentCompilerInterface $argumentCompiler,
52
        array $options
53
    ) {
54 6
        Assert::null($this->controllerHelper);
55 6
        Assert::keyExists($options, 'template');
56
57 5
        $options = array_merge([
58 5
            'arguments' => [],
59
            'authorization-attribute' => null,
60 5
        ], $options);
61
62 5
        $this->controllerHelper = $controllerHelper;
63 5
        $this->argumentCompiler = $argumentCompiler;
64 5
        $this->templatePath = $options['template'];
65 5
        $this->arguments = $options['arguments'];
66 5
        $this->authorizationAttribute = $options['authorization-attribute'];
67 5
    }
68
69 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...
70
    {
71
        /** @var Request $request */
72 2
        $request = $this->controllerHelper->getCurrentRequest();
73
74 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
75
76 1
        return $this->renderTemplate($request);
77
    }
78
79 3
    public function renderTemplate(Request $request): Response
80
    {
81 3
        if (!is_null($this->authorizationAttribute)) {
82 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
83
        }
84
85
        /** @var array $arguments */
86 2
        $arguments = $this->argumentCompiler->buildArguments($this->arguments, $request);
87
88 2
        return $this->controllerHelper->renderTemplate($this->templatePath, $arguments);
89
    }
90
91
}
92