Completed
Pull Request — master (#90)
by Arnaud
09:40
created

DeleteAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 70
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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