Completed
Push — master ( 026476...5a8e15 )
by Gerrit
30:22
created

GenericTemplateRenderController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 61
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A renderTemplate() 0 11 2
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 4
    public function __construct(
50
        ControllerHelperInterface $controllerHelper,
51
        ArgumentCompilerInterface $argumentCompiler,
52
        array $options
53
    ) {
54 4
        Assert::null($this->controllerHelper);
55 4
        Assert::keyExists($options, 'template');
56
57 3
        $options = array_merge([
58 3
            'arguments' => [],
59
            'authorization-attribute' => null,
60 3
        ], $options);
61
62 3
        $this->controllerHelper = $controllerHelper;
63 3
        $this->argumentCompiler = $argumentCompiler;
64 3
        $this->templatePath = $options['template'];
65 3
        $this->arguments = $options['arguments'];
66 3
        $this->authorizationAttribute = $options['authorization-attribute'];
67 3
    }
68
69 2
    public function renderTemplate(Request $request): Response
70
    {
71 2
        if (!is_null($this->authorizationAttribute)) {
72 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $request);
73
        }
74
75
        /** @var array $arguments */
76 1
        $arguments = $this->argumentCompiler->buildArguments($this->arguments, $request);
77
78 1
        return $this->controllerHelper->renderTemplate($this->templatePath, $arguments);
79
    }
80
81
}
82