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

EditFormHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
use Twig_Environment;
13
14
/**
15
 * Create a response from the form data.
16
 */
17
class EditFormHandler
18
{
19
    /**
20
     * @var Twig_Environment
21
     */
22
    private $twig;
23
    
24
    /**
25
     * @var Router
26
     */
27
    private $router;
28
    
29
    /**
30
     * @var RequestStack
31
     */
32
    private $requestStack;
33
    
34
    /**
35
     * EditFormHandler constructor.
36
     *
37
     * @param Twig_Environment $twig
38
     * @param Router           $router
39
     * @param RequestStack     $requestStack
40
     */
41
    public function __construct(Twig_Environment $twig, Router $router, RequestStack $requestStack)
42
    {
43
        $this->twig = $twig;
44
        $this->router = $router;
45
        $this->requestStack = $requestStack;
46
    }
47
    
48
    /**
49
     * Save the entity if the form is valid, and redirect to the list action if
50
     * required
51
     *
52
     * @param FormInterface $form
53
     * @param AdminInterface $admin
54
     *
55
     * @return RedirectResponse|Response
56
     */
57
    public function handle(FormInterface $form, AdminInterface $admin)
58
    {
59
        $template = $admin
60
            ->getView()
61
            ->getConfiguration()
62
            ->getParameter('template')
63
        ;
64
        
65 View Code Duplication
        if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            // if the form is valid, we save the entity
67
            $admin->save();
68
    
69
            if ($this->shouldRedirect($admin)) {
70
                $generator = new RouteNameGenerator();
71
                
72
                // if the redirect input is clicked and the list action exists, we redirect to the list action
73
                $url = $this
74
                    ->router
75
                    ->generate($generator->generate('list', $admin->getName(), $admin->getConfiguration()));
76
                
77
                return new RedirectResponse($url);
78
            }
79
        }
80
        // display the form after validation or not
81
        $content = $this
82
            ->twig
83
            ->render($template, [
84
                'admin' => $admin,
85
                'form' => $form->createView()
86
            ])
87
        ;
88
    
89
        return new Response($content);
90
    }
91
    
92
    /**
93
     * Return true if the user should be redirected to the list action.
94
     *
95
     * @param AdminInterface $admin
96
     *
97
     * @return bool
98
     */
99
    private function shouldRedirect(AdminInterface $admin)
100
    {
101
        $submit = $this
102
            ->requestStack
103
            ->getCurrentRequest()
104
            ->get('submit')
105
        ;
106
        
107
        if ('save-and-redirect' !== $submit) {
108
            return false;
109
        }
110
    
111
        if (!$admin->hasAction('list')) {
112
            return false;
113
        }
114
        
115
        return true;
116
    }
117
}
118