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

EditResponder::respond()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 36
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 36
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 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