Completed
Pull Request — master (#90)
by Arnaud
07:12 queued 36s
created

CreateFormHandler::handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 38
Code Lines 20

Duplication

Lines 11
Ratio 28.95 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 11
loc 38
ccs 0
cts 25
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 3
nop 2
crap 20
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\ClickableInterface;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Response;
12
use Twig_Environment;
13
14
/**
15
 * Create a response from the form data.
16
 */
17
class CreateFormHandler
18
{
19
    /**
20
     * @var Twig_Environment
21
     */
22
    private $twig;
23
    
24
    /**
25
     * @var Router
26
     */
27
    private $router;
28
    
29
    /**
30
     * EditFormHandler constructor.
31
     *
32
     * @param Twig_Environment $twig
33
     * @param Router $router
34
     */
35
    public function __construct(Twig_Environment $twig, Router $router)
36
    {
37
        $this->twig = $twig;
38
        $this->router = $router;
39
    }
40
    
41
    /**
42
     * Save the entity if the form is valid, and redirect to the list action if
43
     * required
44
     *
45
     * @param FormInterface $form
46
     * @param AdminInterface $admin
47
     *
48
     * @return RedirectResponse|Response
49
     */
50
    public function handle(FormInterface $form, AdminInterface $admin)
51
    {
52
        $template = $admin
53
            ->getView()
54
            ->getConfiguration()
55
            ->getParameter('template')
56
        ;
57
        
58
        if ($form->isValid()) {
59
            // if the form is valid, we save the entity
60
            $admin->create();
61
    
62
            /** @var ClickableInterface $input */
63
            $input = $form->get('save-and-redirect');
64
    
65 View Code Duplication
            if ($input->isClicked() && $admin->hasAction('list')) {
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
                $generator = new RouteNameGenerator();
67
                
68
                // if the redirect input is clicked and the list action exists, we redirect to the list action
69
                $url = $this
70
                    ->router
71
                    ->generate($generator->generate('list', $admin->getName(), $admin->getConfiguration()))
72
                ;
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