Completed
Pull Request — master (#306)
by Piotr
02:10
created

AbstractFormSubmitHandler::handleRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
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\Admin\Context\Request;
11
12
use FSi\Bundle\AdminBundle\Event\AdminEvent;
13
use FSi\Bundle\AdminBundle\Event\FormEvent;
14
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
15
use Symfony\Component\HttpFoundation\Request;
16
17
abstract class AbstractFormSubmitHandler extends AbstractHandler
18
{
19
    /**
20
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
21
     * @param \Symfony\Component\HttpFoundation\Request $request
22
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
23
     * @return null|\Symfony\Component\HttpFoundation\Response
24
     */
25
    public function handleRequest(AdminEvent $event, Request $request)
26
    {
27
        $event = $this->validateEvent($event);
28
29
        if (!$request->isMethod(Request::'POST')) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
Loading history...
30
            return;
31
        }
32
33
        $this->eventDispatcher->dispatch($this->getPreSubmitEventName(), $event);
34
        if ($event->hasResponse()) {
35
            return $event->getResponse();
36
        }
37
38
        $event->getForm()->handleRequest($request);
39
40
        $this->eventDispatcher->dispatch($this->getPostSubmitEventName(), $event);
41
        if ($event->hasResponse()) {
42
            return $event->getResponse();
43
        }
44
    }
45
46
    /**
47
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
48
     * @return \FSi\Bundle\AdminBundle\Event\FormEvent
49
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
50
     */
51
    protected function validateEvent(AdminEvent $event)
52
    {
53
        if (!$event instanceof FormEvent) {
54
            throw new RequestHandlerException(sprintf("%s requires FormEvent", get_class($this)));
55
        }
56
57
        return $event;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    abstract protected function getPreSubmitEventName();
64
65
    /**
66
     * @return string
67
     */
68
    abstract protected function getPostSubmitEventName();
69
}
70