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

EditResponder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 8
dl 71
loc 71
ccs 0
cts 20
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B respond() 41 41 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\Action\Responder;
4
5
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
6
use LAG\AdminBundle\Admin\AdminInterface;
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\RouterInterface;
12
use Twig_Environment;
13
14 View Code Duplication
class EditResponder implements ResponderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    use ResponderTrait;
17
    
18
    /**
19
     * @var RouterInterface
20
     */
21
    private $router;
22
    
23
    /**
24
     * EditResponder constructor.
25
     *
26
     * @param Twig_Environment $twig
27
     * @param RouterInterface  $router
28
     */
29
    public function __construct(Twig_Environment $twig, RouterInterface $router)
30
    {
31
        $this->twig = $twig;
32
        $this->router = $router;
33
    }
34
    
35
    /**
36
     * @param ActionConfiguration $configuration
37
     * @param AdminInterface      $admin
38
     * @param FormInterface       $form
39
     * @param Request             $request
40
     *
41
     * @return Response
42
     */
43
    public function respond(
44
        ActionConfiguration $configuration,
45
        AdminInterface $admin,
46
        FormInterface $form,
47
        Request $request
48
    ) {
49
        $template = $configuration->getParameter('template');
50
        
51
        // if the form is submitted and validated, the user should be redirected
52
        if ($form->isSubmitted() && $form->isValid()) {
53
            $submitButton = $request
54
                ->request
55
                ->get('submit')
56
            ;
57
            
58
            // if the save button is pressed, the user will stay on the edit view
59
            if ('save' === $submitButton) {
60
                $url = $this
61
                    ->router
62
                    ->generate($admin->generateRouteName('edit'), [
63
                        'id' => $admin->getUniqueEntity()->getId(),
64
                    ])
65
                ;
66
                
67
                return new RedirectResponse($url);
68
            } else {
69
                // otherwise the user will be redirected to the list view
70
                $url = $this
71
                    ->router
72
                    ->generate($admin->generateRouteName('list'))
73
                ;
74
                
75
                return new RedirectResponse($url);
76
            }
77
        }
78
        
79
        return $this->render($template, [
80
            'admin' => $admin,
81
            'form' => $form->createView(),
82
        ]);
83
    }
84
}
85