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')) { |
|
|
|
|
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
|
|
|
|