DataGridBindDataHandler::handleRequest()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 9.4222
cc 5
nc 6
nop 2
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\AbstractHandler;
15
use FSi\Bundle\AdminBundle\Event\AdminEvent;
16
use FSi\Bundle\AdminBundle\Event\ListEvent;
17
use FSi\Bundle\AdminBundle\Event\ListEvents;
18
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
class DataGridBindDataHandler extends AbstractHandler
23
{
24
    public function handleRequest(AdminEvent $event, Request $request): ?Response
25
    {
26
        $event = $this->validateEvent($event);
27
28
        if ($request->isMethod(Request::METHOD_POST)) {
29
            $this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event);
0 ignored issues
show
Bug introduced by
FSi\Bundle\AdminBundle\E...TAGRID_REQUEST_PRE_BIND of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
            $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
30
                                    dispatch(ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event);

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.

Loading history...
30
            if ($event->hasResponse()) {
31
                return $event->getResponse();
32
            }
33
34
            $event->getDataGrid()->bindData($request);
35
            $this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_POST_BIND, $event);
36
            if ($event->hasResponse()) {
37
                return $event->getResponse();
38
            }
39
40
            $event->getElement()->saveDataGrid();
0 ignored issues
show
Bug introduced by
The method saveDataGrid() does not exist on FSi\Bundle\AdminBundle\Admin\Element. It seems like you code against a sub-type of FSi\Bundle\AdminBundle\Admin\Element such as FSi\Bundle\AdminBundle\A...CRUD\GenericListElement or FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\ListElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\A...UD\DependentListElement or FSi\Bundle\AdminBundle\D...in\DependentListElement or FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement or FSi\Bundle\AdminBundle\D...in\DependentCRUDElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $event->getElement()->/** @scrutinizer ignore-call */ saveDataGrid();
Loading history...
41
            $event->getDataSource()->bindParameters($request);
0 ignored issues
show
Bug introduced by
$request of type Symfony\Component\HttpFoundation\Request is incompatible with the type array expected by parameter $parameters of FSi\Component\DataSource...rface::bindParameters(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $event->getDataSource()->bindParameters(/** @scrutinizer ignore-type */ $request);
Loading history...
42
            $event->getDataGrid()->setData($event->getDataSource()->getResult());
0 ignored issues
show
Bug introduced by
$event->getDataSource()->getResult() of type Countable is incompatible with the type iterable expected by parameter $data of FSi\Component\DataGrid\D...ridInterface::setData(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $event->getDataGrid()->setData(/** @scrutinizer ignore-type */ $event->getDataSource()->getResult());
Loading history...
43
        }
44
45
        $this->eventDispatcher->dispatch(ListEvents::LIST_RESPONSE_PRE_RENDER, $event);
46
        if ($event->hasResponse()) {
47
            return $event->getResponse();
48
        }
49
50
        return null;
51
    }
52
53
    private function validateEvent(AdminEvent $event): ListEvent
54
    {
55
        if (!$event instanceof ListEvent) {
56
            throw new RequestHandlerException(sprintf('%s require ListEvent', get_class($this)));
57
        }
58
59
        return $event;
60
    }
61
}
62