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

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