|
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\CRUD\Context\Request; |
|
11
|
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractHandler; |
|
13
|
|
|
use FSi\Bundle\AdminBundle\Event\AdminEvent; |
|
14
|
|
|
use FSi\Bundle\AdminBundle\Event\ListEvent; |
|
15
|
|
|
use FSi\Bundle\AdminBundle\Event\ListEvents; |
|
16
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
|
|
19
|
|
|
class DataGridBindDataHandler extends AbstractHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param AdminEvent $event |
|
23
|
|
|
* @param Request $request |
|
24
|
|
|
* @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException |
|
25
|
|
|
* @return null|\Symfony\Component\HttpFoundation\Response |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handleRequest(AdminEvent $event, Request $request) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->validateEvent($event); |
|
30
|
|
|
|
|
31
|
|
|
if ($request->isMethod('POST')) { |
|
32
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event); |
|
33
|
|
|
if ($event->hasResponse()) { |
|
34
|
|
|
return $event->getResponse(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$event->getDataGrid()->bindData($request); |
|
|
|
|
|
|
38
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_POST_BIND, $event); |
|
39
|
|
|
if ($event->hasResponse()) { |
|
40
|
|
|
return $event->getResponse(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$event->getElement()->saveDataGrid(); |
|
|
|
|
|
|
44
|
|
|
$event->getDataSource()->bindParameters($request); |
|
|
|
|
|
|
45
|
|
|
$event->getDataGrid()->setData($event->getDataSource()->getResult()); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_RESPONSE_PRE_RENDER, $event); |
|
49
|
|
|
if ($event->hasResponse()) { |
|
50
|
|
|
return $event->getResponse(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param AdminEvent $event |
|
56
|
|
|
* @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function validateEvent(AdminEvent $event) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$event instanceof ListEvent) { |
|
61
|
|
|
throw new RequestHandlerException(sprintf("%s require ListEvent", get_class($this))); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: