Completed
Push — master ( 3a8823...81c6e7 )
by Piotr
02:46
created

getPostSaveEventName()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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\Admin\Context\Request;
11
12
use FSi\Bundle\AdminBundle\Admin\RedirectableElement;
13
use FSi\Bundle\AdminBundle\Event\AdminEvent;
14
use FSi\Bundle\AdminBundle\Event\FormEvent;
15
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\RouterInterface;
20
21
abstract class AbstractFormValidRequestHandler extends AbstractHandler
22
{
23
    /**
24
     * @var \Symfony\Component\Routing\RouterInterface
25
     */
26
    protected $router;
27
28
    /**
29
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
30
     * @param \Symfony\Component\Routing\RouterInterface $router
31
     */
32
    public function __construct(EventDispatcherInterface $eventDispatcher, RouterInterface $router)
33
    {
34
        parent::__construct($eventDispatcher);
35
        $this->router = $router;
36
    }
37
38
    /**
39
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
40
     * @param \Symfony\Component\HttpFoundation\Request $request
41
     * @return null|\Symfony\Component\HttpFoundation\Response
42
     */
43
    public function handleRequest(AdminEvent $event, Request $request)
44
    {
45
        $event = $this->validateEvent($event);
46
        if (!$this->isValidPostRequest($event, $request)) {
47
            return;
48
        }
49
50
        $this->eventDispatcher->dispatch($this->getPreSaveEventName(), $event);
51
        if ($event->hasResponse()) {
52
            return $event->getResponse();
53
        }
54
55
        $this->action($event, $request);
56
57
        $this->eventDispatcher->dispatch($this->getPostSaveEventName(), $event);
58
        if ($event->hasResponse()) {
59
            return $event->getResponse();
60
        }
61
62
        return $this->getRedirectResponse($event, $request);
63
    }
64
65
    /**
66
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
67
     * @param \Symfony\Component\HttpFoundation\Request $request
68
     * @return bool
69
     */
70
    protected function isValidPostRequest(FormEvent $event, Request $request)
71
    {
72
        return $request->isMethod('POST') && $event->getForm()->isValid();
73
    }
74
75
    /**
76
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
77
     * @return \FSi\Bundle\AdminBundle\Event\FormEvent
78
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
79
     */
80
    protected function validateEvent(AdminEvent $event)
81
    {
82
        if (!$event instanceof FormEvent) {
83
            throw new RequestHandlerException(sprintf("%s require FormEvent", get_class($this)));
84
        }
85
        if (!$event->getElement() instanceof RedirectableElement) {
86
            throw new RequestHandlerException(sprintf("%s require RedirectableElement", get_class($this)));
87
        }
88
89
        return $event;
90
    }
91
92
    /**
93
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
94
     * @param \Symfony\Component\HttpFoundation\Request $request
95
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
96
     */
97
    protected function getRedirectResponse(FormEvent $event, Request $request)
98
    {
99
        /** @var \FSi\Bundle\AdminBundle\Admin\RedirectableElement $element */
100
        $element = $event->getElement();
101
102
        return new RedirectResponse(
103
            $this->router->generate(
104
                $element->getSuccessRoute(),
105
                $element->getSuccessRouteParameters()
106
            )
107
        );
108
    }
109
110
    /**
111
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
112
     * @param \Symfony\Component\HttpFoundation\Request $request
113
     */
114
    abstract protected function action(FormEvent $event, Request $request);
115
116
    /**
117
     * @return string
118
     */
119
    abstract protected function getPreSaveEventName();
120
121
    /**
122
     * @return string
123
     */
124
    abstract protected function getPostSaveEventName();
125
}
126