Completed
Pull Request — master (#90)
by Arnaud
16:16
created

DeleteFormHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 52
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 28 3
1
<?php
2
3
namespace LAG\AdminBundle\Form\Handler;
4
5
use LAG\AdminBundle\Admin\AdminInterface;
6
use Symfony\Bundle\FrameworkBundle\Routing\Router;
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
10
class DeleteFormHandler implements FormHandlerInterface
11
{
12
    /**
13
     * @var Router
14
     */
15
    private $router;
16
    
17
    /**
18
     * DeleteFormHandler constructor.
19
     *
20
     * @param Router $router
21
     */
22
    public function __construct(Router $router)
23
    {
24
        $this->router = $router;
25
    }
26
    
27
    /**
28
     * @param FormInterface $form
29
     * @param AdminInterface $admin
30
     *
31
     * @return RedirectResponse
32
     */
33
    public function handle(FormInterface $form, AdminInterface $admin)
34
    {
35
        if ($form->isValid()) {
36
            // remove the current entity
37
            $admin->remove();
38
            
39
            // redirect to list if the action exist
40
            $allowedActions = $admin
41
                ->getConfiguration()
42
                ->getParameter('actions')
43
            ;
44
    
45
            if (array_key_exists('list', $allowedActions)) {
46
                $url = $this
47
                    ->router
48
                    ->generate($admin->generateRouteName('list'));
49
                
50
                return new RedirectResponse($url);
51
            }
52
            return new RedirectResponse('/');
53
        }
54
        $url = $this
55
            ->router
56
            ->generate($admin->generateRouteName('delete'))
57
        ;
58
        
59
        return new RedirectResponse($url);
60
    }
61
}
62