Completed
Pull Request — master (#90)
by Arnaud
02:11
created

EditResponder::respond()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
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 LAG\AdminBundle\Routing\RouteNameGenerator;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class EditResponder extends AbstractResponder
13
{
14
    /**
15
     * Display the edit form and redirect if required when the form is submitted.
16
     *
17
     * @param ActionConfiguration $configuration
18
     * @param AdminInterface      $admin
19
     * @param FormInterface       $form
20
     * @param string|null         $submitButtonName
21
     *
22 3
     * @return Response|RedirectResponse
23
     */
24
    public function respond(
25
        ActionConfiguration $configuration,
26
        AdminInterface $admin,
27
        FormInterface $form,
28 3
        $submitButtonName = null
29
    ) {
30
        $template = $configuration->getParameter('template');
31 3
        
32
        // if the form is submitted and validated, the user should be redirected
33 2
        if ($form->isSubmitted() && $form->isValid()) {
34 2
            $generator = new RouteNameGenerator();
35
            
36
            // if the save button is pressed, the user will stay on the edit view
37
            if ('save' === $submitButtonName) {
38 2
                $url = $this
39
                    ->router
40 1
                    ->generate($generator->generate('edit', $admin->getName(), $admin->getConfiguration()), [
41 1
                        'id' => $admin->getUniqueEntity()->getId(),
42 1
                    ])
43
                ;
44
                
45
                return new RedirectResponse($url);
46 1
            } else {
47
                // otherwise the user will be redirected to the list view
48
                $url = $this
49
                    ->router
50 1
                    ->generate($generator->generate('list', $admin->getName(), $admin->getConfiguration()))
51 1
                ;
52
                
53
                return new RedirectResponse($url);
54 1
            }
55
        }
56
        
57
        return $this->render($template, [
58 1
            'admin' => $admin->getView(),
59 1
            'form' => $form->createView(),
60 1
        ]);
61
    }
62
}
63