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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractFormValidRequestHandler; |
15
|
|
|
use FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement; |
16
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchEvent; |
17
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchEvents; |
18
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchPreApplyEvent; |
19
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
20
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
21
|
|
|
use FSi\Bundle\AdminBundle\Message\FlashMessages; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
25
|
|
|
|
26
|
|
|
class BatchFormValidRequestHandler extends AbstractFormValidRequestHandler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var FlashMessages |
30
|
|
|
*/ |
31
|
|
|
private $flashMessages; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
EventDispatcherInterface $eventDispatcher, |
35
|
|
|
RouterInterface $router, |
36
|
|
|
FlashMessages $flashMessages |
37
|
|
|
) { |
38
|
|
|
parent::__construct($eventDispatcher, $router); |
39
|
|
|
|
40
|
|
|
$this->flashMessages = $flashMessages; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function action(FormEvent $event, Request $request): void |
44
|
|
|
{ |
45
|
|
|
/* @var $element BatchElement */ |
46
|
|
|
$element = $event->getElement(); |
47
|
|
|
$objects = $this->getObjects($element, $request); |
48
|
|
|
|
49
|
|
|
if (!count($objects)) { |
50
|
|
|
$this->flashMessages->warning('messages.batch.no_elements'); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($objects as $object) { |
55
|
|
|
$preEvent = new BatchPreApplyEvent($element, $request, $object); |
56
|
|
|
$this->eventDispatcher->dispatch(BatchEvents::BATCH_OBJECT_PRE_APPLY, $preEvent); |
|
|
|
|
57
|
|
|
|
58
|
|
|
if ($preEvent->shouldSkip()) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$element->apply($object); |
63
|
|
|
|
64
|
|
|
$this->eventDispatcher->dispatch( |
65
|
|
|
BatchEvents::BATCH_OBJECT_POST_APPLY, |
66
|
|
|
new BatchEvent($element, $request, $object) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getObjects(BatchElement $element, Request $request): array |
72
|
|
|
{ |
73
|
|
|
$objects = []; |
74
|
|
|
$indexes = $request->request->get('indexes', []); |
75
|
|
|
|
76
|
|
|
if (!is_array($indexes) || !count($indexes)) { |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($indexes as $index) { |
81
|
|
|
$object = $element->getDataIndexer()->getData($index); |
82
|
|
|
|
83
|
|
|
if (null === $object) { |
84
|
|
|
throw new RequestHandlerException(sprintf("Can't find object with id %s", $index)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$objects[] = $object; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $objects; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getPreSaveEventName(): string |
94
|
|
|
{ |
95
|
|
|
return BatchEvents::BATCH_OBJECTS_PRE_APPLY; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getPostSaveEventName(): string |
99
|
|
|
{ |
100
|
|
|
return BatchEvents::BATCH_OBJECTS_POST_APPLY; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.