AbstractFormSubmitHandler::handleRequest()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 9.9
cc 4
nc 4
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\Context\Request;
13
14
use FSi\Bundle\AdminBundle\Event\AdminEvent;
15
use FSi\Bundle\AdminBundle\Event\FormEvent;
16
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
abstract class AbstractFormSubmitHandler extends AbstractHandler
21
{
22
    public function handleRequest(AdminEvent $event, Request $request): ?Response
23
    {
24
        $event = $this->validateEvent($event);
25
26
        if (!$request->isMethod(Request::METHOD_POST)) {
27
            return null;
28
        }
29
30
        $this->eventDispatcher->dispatch($this->getPreSubmitEventName(), $event);
0 ignored issues
show
Bug introduced by
$this->getPreSubmitEventName() 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

30
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ $this->getPreSubmitEventName(), $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

30
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
31
                                dispatch($this->getPreSubmitEventName(), $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...
31
        if ($event->hasResponse()) {
32
            return $event->getResponse();
33
        }
34
35
        $event->getForm()->handleRequest($request);
36
37
        $this->eventDispatcher->dispatch($this->getPostSubmitEventName(), $event);
38
        if ($event->hasResponse()) {
39
            return $event->getResponse();
40
        }
41
42
        return null;
43
    }
44
45
    private function validateEvent(AdminEvent $event): FormEvent
46
    {
47
        if (!$event instanceof FormEvent) {
48
            throw new RequestHandlerException(sprintf('%s requires FormEvent', get_class($this)));
49
        }
50
51
        return $event;
52
    }
53
54
    abstract protected function getPreSubmitEventName(): string;
55
56
    abstract protected function getPostSubmitEventName(): string;
57
}
58