Completed
Pull Request — master (#90)
by Arnaud
03:18
created

EditAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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