Completed
Pull Request — master (#90)
by Arnaud
05:12 queued 02:13
created

EditResponder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 49
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() 36 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 View Code Duplication
class EditResponder extends AbstractResponder
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...
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                     $submitButtonName
20
     *
21
     * @return Response|RedirectResponse
22
     */
23 3
    public function respond(
24
        ActionConfiguration $configuration,
25
        AdminInterface $admin,
26
        FormInterface $form,
27
        $submitButtonName
28
    ) {
29 3
        $template = $configuration->getParameter('template');
30
        
31
        // if the form is submitted and validated, the user should be redirected
32 3
        if ($form->isSubmitted() && $form->isValid()) {
33
            // if the save button is pressed, the user will stay on the edit view
34 2
            if ('save' === $submitButtonName) {
35
                $url = $this
36 1
                    ->router
37 1
                    ->generate($admin->generateRouteName('edit'), [
38 1
                        'id' => $admin->getUniqueEntity()->getId(),
39
                    ])
40
                ;
41
                
42 1
                return new RedirectResponse($url);
43
            } else {
44
                // otherwise the user will be redirected to the list view
45
                $url = $this
46 1
                    ->router
47 1
                    ->generate($admin->generateRouteName('list'))
48
                ;
49
                
50 1
                return new RedirectResponse($url);
51
            }
52
        }
53
        
54 1
        return $this->render($template, [
55 1
            'admin' => $admin,
56 1
            'form' => $form->createView(),
57
        ]);
58
    }
59
}
60