Completed
Pull Request — master (#90)
by Arnaud
03:09 queued 01:17
created

DeleteFormHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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