Completed
Pull Request — master (#90)
by Arnaud
16:42
created

EditAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 8
dl 0
loc 65
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B __invoke() 0 33 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
    /**
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 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 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->request->get('submit'))
72
        ;
73
    }
74
}
75