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

CreateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 8
dl 0
loc 75
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B __invoke() 0 43 3
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