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

DeleteFormHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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