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

CreateAction::__invoke()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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