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

EditFormHandler::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 0
cts 15
cp 0
rs 8.8571
cc 3
eloc 18
nc 3
nop 2
crap 12
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
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\HttpFoundation\Response;
11
use Twig_Environment;
12
13
/**
14
 * Create a response from the form data.
15
 */
16
class EditFormHandler implements FormHandlerInterface
17
{
18
    /**
19
     * @var Twig_Environment
20
     */
21
    private $twig;
22
    
23
    /**
24
     * @var Router
25
     */
26
    private $router;
27
    
28
    /**
29
     * @var RequestStack
30
     */
31
    private $requestStack;
32
    
33
    /**
34
     * EditFormHandler constructor.
35
     *
36
     * @param Twig_Environment $twig
37
     * @param Router           $router
38
     * @param RequestStack     $requestStack
39
     */
40
    public function __construct(Twig_Environment $twig, Router $router, RequestStack $requestStack)
41
    {
42
        $this->twig = $twig;
43
        $this->router = $router;
44
        $this->requestStack = $requestStack;
45
    }
46
    
47
    /**
48
     * Save the entity if the form is valid, and redirect to the list action if
49
     * required
50
     *
51
     * @param FormInterface $form
52
     * @param AdminInterface $admin
53
     *
54
     * @return RedirectResponse|Response
55
     */
56
    public function handle(FormInterface $form, AdminInterface $admin)
57
    {
58
        $template = $admin
59
            ->getCurrentAction()
60
            ->getConfiguration()
61
            ->getParameter('template')
62
        ;
63
        
64
        if ($form->isValid()) {
65
            // if the form is valid, we save the entity
66
            $admin->save();
67
    
68
            if ($this->shouldRedirect($admin)) {
69
                // if the redirect input is clicked and the list action exists, we redirect to the list action
70
                $url = $this
71
                    ->router
72
                    ->generate($admin->generateRouteName('list'));
73
                
74
                return new RedirectResponse($url);
75
            }
76
        }
77
        // display the form after validation or not
78
        $content = $this
79
            ->twig
80
            ->render($template, [
81
                'admin' => $admin,
82
                'form' => $form->createView()
83
            ])
84
        ;
85
    
86
        return new Response($content);
87
    }
88
    
89
    /**
90
     * Return true if the user should be redirected to the list action.
91
     *
92
     * @param AdminInterface $admin
93
     *
94
     * @return bool
95
     */
96
    private function shouldRedirect(AdminInterface $admin)
97
    {
98
        $submit = $this
99
            ->requestStack
100
            ->getCurrentRequest()
101
            ->get('submit')
102
        ;
103
        
104
        if ('save-and-redirect' !== $submit) {
105
            return false;
106
        }
107
    
108
        if (!$admin->hasAction('list')) {
109
            return false;
110
        }
111
        
112
        return true;
113
    }
114
}
115