Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

ViewHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace LAG\AdminBundle\View\Handler;
4
5
use LAG\AdminBundle\Admin\View\AdminView;
6
use LAG\AdminBundle\Exception\Exception;
7
use LAG\AdminBundle\View\RedirectView;
8
use LAG\AdminBundle\View\ViewInterface;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\RouterInterface;
12
use Twig\Environment;
13
14
class ViewHandler implements ViewHandlerInterface
15
{
16
    public function __construct(
17
        private Environment $environment,
18
        private RouterInterface $router,
19
    ) {
20
    }
21
22
    public function handle(ViewInterface $view): Response
23
    {
24
        if ($view instanceof AdminView) {
25
            return new Response($this->environment->render($view->getTemplate(), [
26
                'admin' => $view,
27
            ]));
28
        }
29
30
        if ($view instanceof RedirectView) {
31
            return new RedirectResponse($this->router->generate($view->getRoute(), $view->getRouteParameters()));
32
        }
33
34
        throw new Exception(sprintf(
35
            'The view "%s" is not handle. Decorates the "%s" service to add your view logic',
36
            get_class($view),
37
            self::class,
38
        ));
39
    }
40
}
41