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

CreateFormHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 15.28 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 10
dl 11
loc 72
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handle() 11 38 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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