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

EditResponder::respond()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 18
Ratio 50 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 18
loc 36
ccs 14
cts 14
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 3
nop 4
crap 4
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