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

DeleteAction::__invoke()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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