Completed
Push — master ( af5abc...cc6520 )
by Jarek
14s
created

ControllerAbstract::handleRequest()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 5
nop 3
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 EventDispatcherInterface
38
     */
39
    private $eventDispatcher;
40
41
    /**
42
     * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
43
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $contextManager
44
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
45
     */
46
    public function __construct(
47
        EngineInterface $templating,
48
        ContextManager $contextManager,
49
        EventDispatcherInterface $eventDispatcher
50
    ) {
51
        $this->templating = $templating;
52
        $this->contextManager = $contextManager;
53
        $this->eventDispatcher = $eventDispatcher;
54
    }
55
56
    /**
57
     * @param \FSi\Bundle\AdminBundle\Admin\Element $element
58
     * @param \Symfony\Component\HttpFoundation\Request $request
59
     * @param string $route
60
     * @return null|\Symfony\Component\HttpFoundation\Response
61
     */
62
    protected function handleRequest(Element $element, Request $request, $route)
63
    {
64
        $event = new AdminEvent($element, $request);
65
        $this->eventDispatcher->dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event);
66
        if ($event->hasResponse()) {
67
            return $event->getResponse();
68
        }
69
        $context = $this->contextManager->createContext($route, $element);
70
        if (!($context instanceof ContextInterface)) {
71
            throw new NotFoundHttpException(sprintf(
72
                'Cannot find context builder that supports element with id "%s"',
73
                $element->getId()
74
            ));
75
        }
76
77
        $response = $context->handleRequest($request);
78
        if ($response instanceof Response) {
79
            return $response;
80
        }
81
82
        if (!$context->hasTemplateName()) {
83
            throw new ContextException(sprintf(
84
                "Context %s neither returned a response nor has a template name",
85
                get_class($context),
86
                __CLASS__
87
            ));
88
        }
89
90
        return $this->templating->renderResponse(
91
            $context->getTemplateName(),
92
            $context->getData()
93
        );
94
    }
95
}
96