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

EditAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace LAG\AdminBundle\Controller;
4
5
use LAG\AdminBundle\Action\Action;
6
use LAG\AdminBundle\Action\Responder\EditResponder;
7
use Symfony\Component\Form\FormFactoryInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class EditAction extends Action
12
{
13
    /**
14
     * @var EditResponder
15
     */
16
    protected $responder;
17
    
18
    /**
19
     * Action constructor.
20
     *
21
     * @param string               $name
22
     * @param FormFactoryInterface $formFactory
23
     * @param EditResponder        $responder
24
     */
25
    public function __construct(
26
        $name,
27
        FormFactoryInterface $formFactory,
28
        EditResponder $responder
29
    ) {
30
        $this->name = $name;
31
        $this->formFactory = $formFactory;
32
        $this->responder = $responder;
33
    }
34
    
35
    /**
36
     * Edit and update an entity using the EditForm handler.
37
     *
38
     * @param Request $request
39
     *
40
     * @return Response
41
     */
42
    public function __invoke(Request $request)
43
    {
44
        // the admin with automatically injected thanks to the KernelSubscriber
45
        $this
46
            ->admin
47
            ->handleRequest($request)
48
        ;
49
        
50
        // create the associated form type
51
        $form = $this
52
            ->formFactory
53
            ->create(
54
                $this->configuration->getParameter('form'),
55
                $this->admin->getUniqueEntity(),
56
                $this->configuration->getParameter('form_options')
57
            )
58
        ;
59
        $form->handleRequest($request);
60
    
61
        if ($form->isSubmitted() && $form->isValid()) {
62
            // save the updated entity
63
            $this
64
                ->admin
65
                ->save()
66
            ;
67
        }
68
    
69
        // return a Response using the EditResponder
70
        return $this
71
            ->responder
72
            ->respond($this->configuration, $this->admin, $form, $request->request->get('submit'))
73
        ;
74
    }
75
}
76