Completed
Pull Request — master (#90)
by Arnaud
15:25
created

EditResponder::respond()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 41
Code Lines 24

Duplication

Lines 41
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 41
loc 41
ccs 0
cts 21
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 3
nop 4
crap 20
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
use Symfony\Component\Routing\RouterInterface;
12
use Twig_Environment;
13
14 View Code Duplication
class EditResponder implements ResponderInterface
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...
15
{
16
    use ResponderTrait;
17
    
18
    /**
19
     * @var RouterInterface
20
     */
21
    private $router;
22
    
23
    /**
24
     * EditResponder constructor.
25
     *
26
     * @param Twig_Environment $twig
27
     * @param RouterInterface  $router
28
     */
29
    public function __construct(Twig_Environment $twig, RouterInterface $router)
30
    {
31
        $this->twig = $twig;
32
        $this->router = $router;
33
    }
34
    
35
    /**
36
     * @param ActionConfiguration $configuration
37
     * @param AdminInterface      $admin
38
     * @param FormInterface       $form
39
     * @param Request             $request
40
     *
41
     * @return Response
42
     */
43
    public function respond(
44
        ActionConfiguration $configuration,
45
        AdminInterface $admin,
46
        FormInterface $form,
47
        Request $request
48
    ) {
49
        $template = $configuration->getParameter('template');
50
        
51
        // if the form is submitted and validated, the user should be redirected
52
        if ($form->isSubmitted() && $form->isValid()) {
53
            $submitButton = $request
54
                ->request
55
                ->get('submit')
56
            ;
57
            
58
            // if the save button is pressed, the user will stay on the edit view
59
            if ('save' === $submitButton) {
60
                $url = $this
61
                    ->router
62
                    ->generate($admin->generateRouteName('edit'), [
63
                        'id' => $admin->getUniqueEntity()->getId(),
64
                    ])
65
                ;
66
                
67
                return new RedirectResponse($url);
68
            } else {
69
                // otherwise the user will be redirected to the list view
70
                $url = $this
71
                    ->router
72
                    ->generate($admin->generateRouteName('list'))
73
                ;
74
                
75
                return new RedirectResponse($url);
76
            }
77
        }
78
        
79
        return $this->render($template, [
80
            'admin' => $admin,
81
            'form' => $form->createView(),
82
        ]);
83
    }
84
}
85