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

EditResponder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 36.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 18
loc 49
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B respond() 18 36 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\Response;
10
11
class EditResponder extends AbstractResponder
12
{
13
    /**
14
     * Display the edit form and redirect if required when the form is submitted.
15
     *
16
     * @param ActionConfiguration $configuration
17
     * @param AdminInterface      $admin
18
     * @param FormInterface       $form
19
     * @param string|null         $submitButtonName
20
     *
21
     * @return Response|RedirectResponse
22
     */
23 6
    public function respond(
24
        ActionConfiguration $configuration,
25
        AdminInterface $admin,
26
        FormInterface $form,
27
        $submitButtonName = null
28
    ) {
29 6
        $template = $configuration->getParameter('template');
30
        
31
        // if the form is submitted and validated, the user should be redirected
32 6
        if ($form->isSubmitted() && $form->isValid()) {
33
            // if the save button is pressed, the user will stay on the edit view
34 4 View Code Duplication
            if ('save' === $submitButtonName) {
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...
35
                $url = $this
36 2
                    ->router
37 2
                    ->generate($admin->generateRouteName('edit'), [
38 2
                        'id' => $admin->getUniqueEntity()->getId(),
39
                    ])
40
                ;
41
                
42 2
                return new RedirectResponse($url);
43
            } else {
44
                // otherwise the user will be redirected to the list view
45
                $url = $this
46 2
                    ->router
47 2
                    ->generate($admin->generateRouteName('list'))
48
                ;
49
                
50 2
                return new RedirectResponse($url);
51
            }
52
        }
53
        
54 2
        return $this->render($template, [
55 2
            'admin' => $admin,
56 2
            'form' => $form->createView(),
57
        ]);
58
    }
59
}
60