AbstractFormValidRequestHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 30
c 1
b 0
f 0
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateElement() 0 7 2
A __construct() 0 6 1
A validateEvent() 0 9 2
A isValidPostRequest() 0 3 2
A getRedirectResponse() 0 12 2
A handleRequest() 0 20 4
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\Admin\Element;
15
use FSi\Bundle\AdminBundle\Admin\RedirectableElement;
16
use FSi\Bundle\AdminBundle\Event\AdminEvent;
17
use FSi\Bundle\AdminBundle\Event\FormEvent;
18
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\Routing\RouterInterface;
24
25
abstract class AbstractFormValidRequestHandler extends AbstractHandler
26
{
27
    /**
28
     * @var RouterInterface
29
     */
30
    protected $router;
31
32
    public function __construct(
33
        EventDispatcherInterface $eventDispatcher,
34
        RouterInterface $router
35
    ) {
36
        parent::__construct($eventDispatcher);
37
        $this->router = $router;
38
    }
39
40
    public function handleRequest(AdminEvent $event, Request $request): ?Response
41
    {
42
        $event = $this->validateEvent($event);
43
        if (!$this->isValidPostRequest($event, $request)) {
44
            return null;
45
        }
46
47
        $this->eventDispatcher->dispatch($this->getPreSaveEventName(), $event);
0 ignored issues
show
Bug introduced by
$this->getPreSaveEventName() 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

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

47
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
48
                                dispatch($this->getPreSaveEventName(), $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...
48
        if ($event->hasResponse()) {
49
            return $event->getResponse();
50
        }
51
52
        $this->action($event, $request);
53
54
        $this->eventDispatcher->dispatch($this->getPostSaveEventName(), $event);
55
        if ($event->hasResponse()) {
56
            return $event->getResponse();
57
        }
58
59
        return $this->getRedirectResponse($event, $request);
60
    }
61
62
    protected function isValidPostRequest(FormEvent $event, Request $request): bool
63
    {
64
        return $request->isMethod(Request::METHOD_POST) && $event->getForm()->isValid();
65
    }
66
67
    protected function getRedirectResponse(FormEvent $event, Request $request): RedirectResponse
68
    {
69
        if ($request->query->has('redirect_uri')) {
70
            return new RedirectResponse($request->query->get('redirect_uri'));
71
        }
72
73
        $element = $this->validateElement($event->getElement());
74
75
        return new RedirectResponse(
76
            $this->router->generate(
77
                $element->getSuccessRoute(),
78
                $element->getSuccessRouteParameters()
79
            )
80
        );
81
    }
82
83
    abstract protected function action(FormEvent $event, Request $request): void;
84
85
    abstract protected function getPreSaveEventName(): string;
86
87
    abstract protected function getPostSaveEventName(): string;
88
89
    private function validateEvent(AdminEvent $event): FormEvent
90
    {
91
        if (!$event instanceof FormEvent) {
92
            throw new RequestHandlerException(sprintf('%s requires FormEvent', get_class($this)));
93
        }
94
95
        $this->validateElement($event->getElement());
96
97
        return $event;
98
    }
99
100
    private function validateElement(Element $element): RedirectableElement
101
    {
102
        if (!$element instanceof RedirectableElement) {
103
            throw new RequestHandlerException(sprintf('%s requires RedirectableElement', get_class($this)));
104
        }
105
106
        return $element;
107
    }
108
}
109