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

EditAction::__invoke()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 2
cts 2
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 2
nop 1
crap 3
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 1
    /**
18
     * Action constructor.
19
     *
20
     * @param string               $name
21 1
     * @param FormFactoryInterface $formFactory
22 1
     * @param EditResponder        $responder
23
     */
24
    public function __construct(
25
        $name,
26
        FormFactoryInterface $formFactory,
27 1
        EditResponder $responder
28 1
    ) {
29 1
        $this->name = $name;
30 1
        $this->formFactory = $formFactory;
31 1
        $this->responder = $responder;
32
    }
33
    
34 1
    /**
35
     * Edit and update an entity using the EditForm handler.
36 1
     *
37
     * @param Request $request
38
     *
39 1
     * @return Response
40 1
     */
41
    public function __invoke(Request $request)
42
    {
43
        // the admin with automatically injected thanks to the KernelSubscriber
44
        $this
45 1
            ->admin
46 1
            ->handleRequest($request)
47
        ;
48
        
49
        // create the associated form type
50
        $form = $this
51
            ->formFactory
52
            ->create(
53
                $this->configuration->getParameter('form'),
54
                $this->admin->getUniqueEntity(),
55
                $this->configuration->getParameter('form_options')
56
            )
57
        ;
58
        $form->handleRequest($request);
59
    
60
        if ($form->isSubmitted() && $form->isValid()) {
61
            // save the updated entity
62
            $this
63
                ->admin
64
                ->save()
65
            ;
66
        }
67
    
68
        // return a Response using the EditResponder
69
        return $this
70
            ->responder
71
            ->respond($this->configuration, $this->admin, $form, $request->request->get('submit'))
72
        ;
73
    }
74
}
75