Completed
Pull Request — master (#4369)
by Craig
11:44
created

StaticAction::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle\Controller;
15
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
use Twig\Environment;
20
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
21
22
final class StaticAction
23
{
24
    /**
25
     * @var PermissionApiInterface
26
     */
27
    private $permissionApi;
28
29
    /**
30
     * @var Environment
31
     */
32
    private $twig;
33
34
    public function __construct(
35
        PermissionApiInterface $permissionApi,
36
        Environment $twig
37
    ) {
38
        $this->permissionApi = $permissionApi;
39
        $this->twig = $twig;
40
    }
41
42
    /**
43
     * This controller action is designed for the "/p/*" route.
44
     * The route definition is set in `CoreBundle/Resources/config/routing.xml`
45
     */
46
    public function __invoke(string $name): Response
47
    {
48
        if (!$this->permissionApi->hasPermission('ZikulaCoreBundle::', 'static::' . $name, ACCESS_OVERVIEW)) {
49
            throw new AccessDeniedException();
50
        }
51
52
        if (!$this->twig->getLoader()->exists('p/' . $name . '.html.twig')) {
53
            throw new NotFoundHttpException(sprintf('No route found for "%s"', 'GET /p/' . $name));
54
        }
55
56
        return new Response($this->twig->render('p/' . $name . '.html.twig'));
57
    }
58
}
59