FormValidRequestHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 36
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostSaveEventName() 0 3 1
A getPreSaveEventName() 0 3 1
A action() 0 8 2
A handleRequest() 0 13 3
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\ResourceRepository\Context\Request;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractFormValidRequestHandler;
15
use FSi\Bundle\AdminBundle\Admin\ResourceRepository\GenericResourceElement;
16
use FSi\Bundle\AdminBundle\Event\AdminEvent;
17
use FSi\Bundle\AdminBundle\Event\FormEvent;
18
use FSi\Bundle\AdminBundle\Event\FormEvents;
19
use FSi\Bundle\ResourceRepositoryBundle\Model\ResourceValue;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
class FormValidRequestHandler extends AbstractFormValidRequestHandler
24
{
25
    protected function action(FormEvent $event, Request $request): void
26
    {
27
        /* @var $element GenericResourceElement */
28
        $element = $event->getElement();
29
        /* @var $data ResourceValue[] */
30
        $data = $event->getForm()->getData();
31
        foreach ($data as $resource) {
32
            $element->save($resource);
33
        }
34
    }
35
36
    protected function getPreSaveEventName(): string
37
    {
38
        return FormEvents::FORM_DATA_PRE_SAVE;
39
    }
40
41
    protected function getPostSaveEventName(): string
42
    {
43
        return FormEvents::FORM_DATA_POST_SAVE;
44
    }
45
46
    public function handleRequest(AdminEvent $event, Request $request): ?Response
47
    {
48
        $response = parent::handleRequest($event, $request);
49
        if ($response) {
0 ignored issues
show
introduced by
$response is of type Symfony\Component\HttpFoundation\Response, thus it always evaluated to true.
Loading history...
50
            return $response;
51
        }
52
53
        $this->eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event);
54
        if ($event->hasResponse()) {
55
            return $event->getResponse();
56
        }
57
58
        return null;
59
    }
60
}
61