Completed
Pull Request — master (#90)
by Arnaud
07:39 queued 05:31
created

EditFormHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 15
loc 105
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 15 34 3
A shouldRedirect() 0 22 3

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\FormInterface;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\RouterInterface;
13
use Twig_Environment;
14
15
/**
16
 * Create a response from the form data.
17
 */
18
class EditFormHandler
19
{
20
    /**
21
     * @var Twig_Environment
22
     */
23
    private $twig;
24
    
25
    /**
26
     * @var Router
27
     */
28
    private $router;
29
    
30
    /**
31
     * @var RequestStack
32
     */
33
    private $requestStack;
34
    
35
    /**
36
     * EditFormHandler constructor.
37
     *
38
     * @param Twig_Environment $twig
39
     * @param RouterInterface  $router
40
     * @param RequestStack     $requestStack
41
     */
42
    public function __construct(Twig_Environment $twig, RouterInterface $router, RequestStack $requestStack)
43
    {
44
        $this->twig = $twig;
45
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
$router is of type object<Symfony\Component\Routing\RouterInterface>, but the property $router was declared to be of type object<Symfony\Bundle\Fr...kBundle\Routing\Router>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
46
        $this->requestStack = $requestStack;
47
    }
48
    
49
    /**
50
     * Save the entity if the form is valid, and redirect to the list action if
51
     * required
52
     *
53
     * @param FormInterface $form
54
     * @param AdminInterface $admin
55
     *
56
     * @return RedirectResponse|Response
57
     */
58
    public function handle(FormInterface $form, AdminInterface $admin)
59
    {
60
        $template = $admin
61
            ->getView()
62
            ->getConfiguration()
63
            ->getParameter('template')
64
        ;
65
        
66 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...
67
            // if the form is valid, we save the entity
68
            $admin->save();
69
    
70
            if ($this->shouldRedirect($admin)) {
71
                $generator = new RouteNameGenerator();
72
                
73
                // if the redirect input is clicked and the list action exists, we redirect to the list action
74
                $url = $this
75
                    ->router
76
                    ->generate($generator->generate('list', $admin->getName(), $admin->getConfiguration()));
77
                
78
                return new RedirectResponse($url);
79
            }
80
        }
81
        // display the form after validation or not
82
        $content = $this
83
            ->twig
84
            ->render($template, [
85
                'admin' => $admin,
86
                'form' => $form->createView()
87
            ])
88
        ;
89
    
90
        return new Response($content);
91
    }
92
    
93
    /**
94
     * Return true if the user should be redirected to the list action.
95
     *
96
     * @param AdminInterface $admin
97
     *
98
     * @return bool
99
     */
100
    private function shouldRedirect(AdminInterface $admin)
101
    {
102
        $submit = $this
103
            ->requestStack
104
            ->getCurrentRequest()
105
            ->get('submit')
106
        ;
107
        
108
        if ('save-and-redirect' !== $submit) {
109
            return false;
110
        }
111
        $actions = $admin
112
            ->getConfiguration()
113
            ->getParameter('actions')
114
        ;
115
    
116
        if (!key_exists('list', $actions)) {
117
            return false;
118
        }
119
        
120
        return true;
121
    }
122
}
123