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

CreateResponder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 52
loc 52
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

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