Completed
Pull Request — master (#311)
by Łukasz
03:20
created

BatchFormValidRequestHandler::getObjects()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * (c) FSi sp. z o.o. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
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\Admin\CRUD\DeleteElement;
17
use FSi\Bundle\AdminBundle\Event\BatchEvent;
18
use FSi\Bundle\AdminBundle\Event\BatchEvents;
19
use FSi\Bundle\AdminBundle\Event\BatchPreApplyEvent;
20
use FSi\Bundle\AdminBundle\Event\FormEvent;
21
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
22
use FSi\Bundle\AdminBundle\Message\FlashMessages;
23
use LogicException;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Routing\RouterInterface;
27
28
class BatchFormValidRequestHandler extends AbstractFormValidRequestHandler
29
{
30
    /**
31
     * @var FlashMessages
32
     */
33
    private $flashMessages;
34
35
    public function __construct(
36
        EventDispatcherInterface $eventDispatcher,
37
        RouterInterface $router,
38
        FlashMessages $flashMessages
39
    ) {
40
        parent::__construct($eventDispatcher, $router);
41
42
        $this->flashMessages = $flashMessages;
43
    }
44
45
    protected function action(FormEvent $event, Request $request): void
46
    {
47
        /** @var BatchElement $element */
48
        $element = $event->getElement();
49
        $objects = $this->getObjects($element, $request);
50
51
        if (empty($objects)) {
52
            $this->flashMessages->warning('messages.batch.no_elements');
53
            return;
54
        }
55
56
        foreach ($objects as $object) {
57
            $preEvent = new BatchPreApplyEvent($element, $request, $object);
58
            $this->eventDispatcher->dispatch(BatchEvents::BATCH_OBJECT_PRE_APPLY, $preEvent);
59
60
            if ($preEvent->shouldSkip()) {
61
                continue;
62
            }
63
64
            $element->apply($object);
65
66
            $this->eventDispatcher->dispatch(
67
                BatchEvents::BATCH_OBJECT_POST_APPLY,
68
                new BatchEvent($element, $request, $object)
69
            );
70
        }
71
    }
72
73
    private function getObjects(BatchElement $element, Request $request): array
74
    {
75
        $objects = [];
76
        $indexes = $request->request->get('indexes', []);
77
78
        if (!is_array($indexes) || !count($indexes)) {
79
            return [];
80
        }
81
82
        foreach ($indexes as $index) {
83
            $object = $element->getDataIndexer()->getData($index);
84
85
            if (null === $object) {
86
                throw new RequestHandlerException(sprintf("Can't find object with id %s", $index));
87
            }
88
89
            $objects[] = $object;
90
        }
91
92
        return $objects;
93
    }
94
95
    protected function isValidPostRequest(FormEvent $event, Request $request): bool
96
    {
97
        $element = $event->getElement();
98
        if ($element instanceof DeleteElement
99
            && $element->hasOption('allow_delete')
100
            && !$element->getOption('allow_delete')
101
        ) {
102
            throw new LogicException(sprintf(
103
                'Tried to delete objects through element "%s", which has option "allow_delete" set to false',
104
                get_class($element)
105
            ));
106
        }
107
108
        return parent::isValidPostRequest($event, $request);
109
    }
110
111
    protected function getPreSaveEventName(): string
112
    {
113
        return BatchEvents::BATCH_OBJECTS_PRE_APPLY;
114
    }
115
116
    protected function getPostSaveEventName(): string
117
    {
118
        return BatchEvents::BATCH_OBJECTS_POST_APPLY;
119
    }
120
}
121