Completed
Pull Request — master (#90)
by Arnaud
10:09 queued 08:09
created

CreateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 8
dl 0
loc 71
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B __invoke() 0 39 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 1
    /**
18
     * Action constructor.
19
     *
20
     * @param string               $name
21 1
     * @param FormFactoryInterface $formFactory
22 1
     * @param CreateResponder      $responder
23
     */
24
    public function __construct(
25
        $name,
26 1
        FormFactoryInterface $formFactory,
27 1
        CreateResponder $responder
28
    ) {
29
        $this->name = $name;
30
        $this->formFactory = $formFactory;
31 1
        $this->responder = $responder;
32 1
    }
33
    
34
    /**
35 1
     * Create an action using the create action form handler.
36 1
     *
37
     * @param Request $request
38
     *
39 1
     * @return Response
40 1
     */
41
    public function __invoke(Request $request)
42 1
    {
43
        // The Admin with auto injected in the KernelSubscriber
44 1
        $this
45
            ->admin
46
            ->handleRequest($request)
47
        ;
48
        $entity = $this
49
            ->admin
50
            ->create()
51
        ;
52 1
        
53 1
        // Create the associated form type
54
        $formType = $this
55
            ->configuration
56
            ->getParameter('form')
57
        ;
58
        $formOptions = $this
59
            ->configuration
60
            ->getParameter('form_options')
61
        ;
62
        $form = $this
63
            ->formFactory
64
            ->create($formType, $entity, $formOptions)
65
        ;
66
        $form->handleRequest($request);
67
    
68
        if ($form->isSubmitted() && $form->isValid()) {
69
            $this
70
                ->admin
71
                ->save()
72
            ;
73
        }
74
    
75
        return $this
76
            ->responder
77
            ->respond($this->configuration, $this->admin, $form, $request->request->get('submit'))
78
        ;
79
    }
80
}
81