Completed
Pull Request — master (#273)
by Piotr
10:33
created

ControllerAbstract::setEventDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Controller;
11
12
use FSi\Bundle\AdminBundle\Admin\Context\ContextInterface;
13
use FSi\Bundle\AdminBundle\Admin\Context\ContextManager;
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
use FSi\Bundle\AdminBundle\Event\AdminEvent;
16
use FSi\Bundle\AdminBundle\Event\AdminEvents;
17
use FSi\Bundle\AdminBundle\Exception\ContextException;
18
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
24
abstract class ControllerAbstract
25
{
26
    /**
27
     * @var EngineInterface
28
     */
29
    protected $templating;
30
31
    /**
32
     * @var ContextManager
33
     */
34
    protected $contextManager;
35
36
    /**
37
     * @var string
38
     */
39
    protected $template;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    private $eventDispatcher;
45
46
    /**
47
     * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
48
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $contextManager
49
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
50
     */
51
    public function __construct(
52
        EngineInterface $templating,
53
        ContextManager $contextManager,
54
        EventDispatcherInterface $eventDispatcher,
55
        $resourceActionTemplate = null
56
    ) {
57
        $this->templating = $templating;
58
        $this->contextManager = $contextManager;
59
        $this->eventDispatcher = $eventDispatcher;
60
        $this->template = $resourceActionTemplate;
61
    }
62
63
    /**
64
     * @param \FSi\Bundle\AdminBundle\Admin\Element $element
65
     * @param \Symfony\Component\HttpFoundation\Request $request
66
     * @param string $route
67
     * @return null|\Symfony\Component\HttpFoundation\Response
68
     */
69
    protected function handleRequest(Element $element, Request $request, $route)
70
    {
71
        $event = new AdminEvent($element, $request);
72
        $this->eventDispatcher->dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event);
73
        if ($event->hasResponse()) {
74
            return $event->getResponse();
75
        }
76
77
        $context = $this->contextManager->createContext($route, $element);
78
        if (!($context instanceof ContextInterface)) {
79
            throw new NotFoundHttpException(sprintf(
80
                'Cannot find context builder that supports element with id "%s"',
81
                $element->getId()
82
            ));
83
        }
84
85
        $response = $context->handleRequest($request);
86
        if ($response instanceof Response) {
87
            return $response;
88
        }
89
90
        if (!isset($this->template) && !$context->hasTemplateName()) {
91
            throw new ContextException(sprintf(
92
                "Context %s did not return any response and controller %s has no template",
93
                get_class($context),
94
                __CLASS__
95
            ));
96
        }
97
98
        return $this->templating->renderResponse(
99
            $context->hasTemplateName() ? $context->getTemplateName() : $this->template,
100
            $context->getData()
101
        );
102
    }
103
}
104