Completed
Pull Request — master (#90)
by Arnaud
03:09 queued 01:17
created

CreateAction::__invoke()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0146

Importance

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