Completed
Pull Request — master (#324)
by Piotr
04:01
created

DeleteRequestHandler::validateDeletion()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
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 Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
15
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
16
use FSi\Bundle\AdminBundle\Admin\CRUD\DeleteElement;
17
use FSi\Bundle\AdminBundle\Event\AdminEvent;
18
use FSi\Bundle\AdminBundle\Event\FormEvent;
19
use FSi\Bundle\AdminBundle\Message\FlashMessages;
20
use LogicException;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Routing\RouterInterface;
25
26
class DeleteRequestHandler implements HandlerInterface
27
{
28
    /**
29
     * @var HandlerInterface
30
     */
31
    private $batchHandler;
32
33
    /**
34
     * @var FlashMessages
35
     */
36
    private $flashMessages;
37
38
    /**
39
     * @var RouterInterface
40
     */
41
    private $router;
42
43
    public function __construct(
44
        HandlerInterface $batchHandler,
45
        FlashMessages $flashMessages,
46
        RouterInterface $router
47
    ) {
48
        $this->batchHandler = $batchHandler;
49
        $this->flashMessages = $flashMessages;
50
        $this->router = $router;
51
    }
52
53
    public function handleRequest(AdminEvent $event, Request $request): ?Response
54
    {
55
        try {
56
            $this->validateDeletion($event);
57
            $response = $this->batchHandler->handleRequest($event, $request);
58
        } catch (ForeignKeyConstraintViolationException $ex) {
59
            $this->flashMessages->error('crud.delete.error.foreign_key');
60
        }
61
62
        return isset($response) ? $response : $this->getRedirectResponse($event, $request);
63
    }
64
65
    private function validateDeletion(AdminEvent $event): void
66
    {
67
        $element = $event->getElement();
68
        if ($element->hasOption('allow_delete') && false === $element->getOption('allow_delete')) {
69
            throw new LogicException(sprintf(
70
                'Tried to delete objects through element "%s", which has option "allow_delete" set to false',
71
                get_class($element)
72
            ));
73
        }
74
    }
75
76
    private function getRedirectResponse(FormEvent $event, Request $request): RedirectResponse
77
    {
78
        if ($request->query->has('redirect_uri')) {
79
            return new RedirectResponse($request->query->get('redirect_uri'));
80
        }
81
82
        $element = $event->getElement();
83
        return new RedirectResponse(
84
            $this->router->generate(
85
                $element->getSuccessRoute(),
0 ignored issues
show
Bug introduced by
The method getSuccessRoute() 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\Admin\RedirectableElement or FSi\Bundle\AdminBundle\A...RUD\GenericBatchElement or FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement or FSi\Bundle\AdminBundle\A...CRUD\GenericFormElement or FSi\Bundle\AdminBundle\A...\GenericResourceElement or FSi\Bundle\AdminBundle\Admin\CRUD\FormElement or FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\A...D\DependentBatchElement or FSi\Bundle\AdminBundle\D...in\DependentFormElement or FSi\Bundle\AdminBundle\A...UD\DependentFormElement or FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement or FSi\Bundle\AdminBundle\D...in\DependentCRUDElement or FSi\Bundle\AdminBundle\D...n\DependentBatchElement or FSi\Bundle\AdminBundle\D...\DependentDeleteElement or FSi\Bundle\AdminBundle\A...\DependentDeleteElement. ( Ignorable by Annotation )

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

85
                $element->/** @scrutinizer ignore-call */ 
86
                          getSuccessRoute(),
Loading history...
86
                $element->getSuccessRouteParameters()
0 ignored issues
show
Bug introduced by
The method getSuccessRouteParameters() 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\Admin\RedirectableElement or FSi\Bundle\AdminBundle\A...RUD\GenericBatchElement or FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement or FSi\Bundle\AdminBundle\A...CRUD\GenericFormElement or FSi\Bundle\AdminBundle\A...\GenericResourceElement or FSi\Bundle\AdminBundle\Admin\CRUD\FormElement or FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement or FSi\Bundle\AdminBundle\Admin\CRUD\CRUDElement or FSi\Bundle\AdminBundle\A...D\DependentBatchElement or FSi\Bundle\AdminBundle\D...in\DependentFormElement or FSi\Bundle\AdminBundle\A...UD\DependentFormElement or FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement or FSi\Bundle\AdminBundle\D...in\DependentCRUDElement or FSi\Bundle\AdminBundle\D...n\DependentBatchElement or FSi\Bundle\AdminBundle\D...\DependentDeleteElement or FSi\Bundle\AdminBundle\A...\DependentDeleteElement. ( Ignorable by Annotation )

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

86
                $element->/** @scrutinizer ignore-call */ 
87
                          getSuccessRouteParameters()
Loading history...
87
            )
88
        );
89
    }
90
}
91