ViewListener::onKernelView()   C
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 5
nop 1
1
<?php
2
3
namespace Knp\RadBundle\EventListener;
4
5
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
6
use Symfony\Component\HttpFoundation\Request;
7
use Knp\RadBundle\View\NameDeducer;
8
use Knp\RadBundle\View\NameDeducer\NotInBundleException;
9
use Knp\RadBundle\View\NameDeducer\NoControllerNameException;
10
use Knp\RadBundle\EventListener\MissingViewHandler;
11
use Symfony\Component\Templating\EngineInterface;
12
13
/**
14
 * Adds Response event listener to render no-Response
15
 * controller results (arrays).
16
 */
17
class ViewListener
18
{
19
    private $templating;
20
    private $missingViewHandler;
21
    private $viewNameDeducer;
22
23
    /**
24
     * Initializes listener.
25
     *
26
     * @param EngineInterface      $templating         Templating engine
27
     * @param ViewNameDeducer      $viewNameDeducer    Deduces the view name from controller name
28
     * @param MissingViewHandler   $missingViewHandler handles missing views
29
     */
30
    public function __construct(EngineInterface $templating, NameDeducer $viewNameDeducer, MissingViewHandler $missingViewHandler = null)
31
    {
32
        $this->templating = $templating;
33
        $this->viewNameDeducer = $viewNameDeducer;
34
        $this->missingViewHandler = $missingViewHandler ?: new MissingViewHandler;
35
    }
36
37
    /**
38
     * Patches response on empty responses.
39
     *
40
     * @param GetResponseForControllerResultEvent $event Event instance
41
     */
42
    public function onKernelView(GetResponseForControllerResultEvent $event)
43
    {
44
        $request = $event->getRequest();
45
46
        try {
47
            $viewName = $this->viewNameDeducer->deduce($request);
48
        }
49
        catch (NotInBundleException $e) {
50
            return;
51
        }
52
        catch (NoControllerNameException $e) {
53
            return;
54
        }
55
        $viewParams = $event->getControllerResult();
56
        if (!is_array($viewParams) && !is_null($viewParams)) {
57
            return;
58
        }
59
60
        if ($this->templating->exists($viewName)) {
61
            $response = $this->templating->renderResponse($viewName, $viewParams ?: array());
0 ignored issues
show
Bug introduced by
The method renderResponse() does not exist on Symfony\Component\Templating\EngineInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
62
            $event->setResponse($response);
63
            return;
64
        }
65
66
        $this->missingViewHandler->handleMissingView($event, $viewName, $viewParams ?: array());
67
    }
68
}
69