Completed
Pull Request — master (#244)
by Łukasz
09:35
created

ControllerAbstract::handleRequest()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 21
nc 9
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\HttpKernel\Exception\NotFoundHttpException;
22
23
abstract class ControllerAbstract
24
{
25
    /**
26
     * @var EngineInterface
27
     */
28
    protected $templating;
29
30
    /**
31
     * @var ContextManager
32
     */
33
    protected $contextManager;
34
35
    /**
36
     * @var string
37
     */
38
    protected $template;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
47
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $contextManager
48
     * @param string|null $resourceActionTemplate
49
     */
50
    function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
        EngineInterface $templating,
52
        ContextManager $contextManager,
53
        $resourceActionTemplate = null
54
    ) {
55
        $this->templating = $templating;
56
        $this->contextManager = $contextManager;
57
        $this->template = $resourceActionTemplate;
58
    }
59
60
    /**
61
     * @param EventDispatcherInterface $eventDispatcher
62
     */
63
    public function setEventDispatcher($eventDispatcher)
64
    {
65
        $this->eventDispatcher = $eventDispatcher;
66
    }
67
68
    /**
69
     * @param \FSi\Bundle\AdminBundle\Admin\Element $element
70
     * @param \Symfony\Component\HttpFoundation\Request $request
71
     * @param string $route
72
     * @return null|\Symfony\Component\HttpFoundation\Response
73
     */
74
    protected function handleRequest(Element $element, Request $request, $route)
75
    {
76
        if ($this->eventDispatcher) {
77
            $event = new AdminEvent($element, $request);
78
            $this->eventDispatcher->dispatch(AdminEvents::CONTEXT_PRE_CREATE, $event);
79
            if ($event->hasResponse()) {
80
                return $event->getResponse();
81
            }
82
        }
83
        
84
        $context = $this->contextManager->createContext($route, $element);
85
86
        if (!($context instanceof ContextInterface)) {
87
            throw new NotFoundHttpException(sprintf(
88
                'Cant find context builder that supports element with id "%s"',
89
                $element->getId()
90
            ));
91
        }
92
93
        if (($response = $context->handleRequest($request)) !== null) {
94
            return $response;
95
        }
96
97
        if (!isset($this->template) && !$context->hasTemplateName()) {
98
            throw new ContextException(sprintf(
99
                "Context %s did not returned any response and controller %s has no template",
100
                get_class($context),
101
                __CLASS__
102
            ));
103
        }
104
105
        return $this->templating->renderResponse(
106
            $context->hasTemplateName() ? $context->getTemplateName() : $this->template,
107
            $context->getData()
108
        );
109
    }
110
}
111