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

GenericTemplateRenderController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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