Passed
Pull Request — master (#203)
by Arnaud
02:53
created

Admin::handleEntityForm()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Admin;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use LAG\AdminBundle\Configuration\AdminConfiguration;
7
use LAG\AdminBundle\Event\Events;
8
use LAG\AdminBundle\Event\Events\AdminEvent;
9
use LAG\AdminBundle\Event\Events\EntityEvent;
10
use LAG\AdminBundle\Event\Events\FilterEvent;
11
use LAG\AdminBundle\Event\Events\FormEvent;
12
use LAG\AdminBundle\Event\Events\ViewEvent;
13
use LAG\AdminBundle\Exception\Exception;
14
use LAG\AdminBundle\Resource\AdminResource;
15
use LAG\AdminBundle\View\ViewInterface;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19
20
class Admin implements AdminInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * @var AdminConfiguration
29
     */
30
    private $configuration;
31
32
    /**
33
     * @var AdminResource
34
     */
35
    private $resource;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * @var ActionInterface
44
     */
45
    private $action;
46
47
    /**
48
     * @var ArrayCollection
49
     */
50
    private $entities;
51
52
    /**
53
     * @var FormInterface[]
54
     */
55
    private $forms = [];
56
57
    /**
58
     * @var Request
59
     */
60
    private $request;
61
62
    /**
63
     * Admin constructor.
64
     */
65 20
    public function __construct(
66
        AdminResource $resource,
67
        AdminConfiguration $configuration,
68
        EventDispatcherInterface $eventDispatcher
69
    ) {
70 20
        $this->name = $resource->getName();
71 20
        $this->configuration = $configuration;
72 20
        $this->resource = $resource;
73 20
        $this->eventDispatcher = $eventDispatcher;
74 20
        $this->entities = new ArrayCollection();
75 20
    }
76
77 10
    public function handleRequest(Request $request)
78
    {
79 10
        $this->request = $request;
80 10
        $event = new AdminEvent($this, $request);
81 10
        $this->eventDispatcher->dispatch($event, Events::ADMIN_HANDLE_REQUEST);
82
83 10
        if (!$event->hasAction()) {
84 2
            throw new Exception('The current action was not set during the dispatch of the event');
85
        }
86 8
        $this->action = $event->getAction();
87
88
        // Dispatch an event to allow entities to be filtered
89 8
        $filterEvent = new FilterEvent($this, $request);
90 8
        $this->eventDispatcher->dispatch($filterEvent, Events::ADMIN_FILTER);
91
92 8
        $event = new EntityEvent($this, $request);
93 8
        $event->setFilters($filterEvent->getFilters());
94 8
        $this->eventDispatcher->dispatch($event, Events::ENTITY_LOAD);
95
96 8
        if (null !== $event->getEntities()) {
97 6
            $this->entities = $event->getEntities();
98
        }
99 8
        $event = new FormEvent($this, $request);
100 8
        $this->eventDispatcher->dispatch($event, Events::ADMIN_CREATE_FORM);
101
102
        // Merge the regular forms and the filter forms
103 8
        $this->forms = array_merge($filterEvent->getForms(), $event->getForms());
104
105 8
        $this->handleEntityForm($request);
106 8
        $this->eventDispatcher->dispatch(new FormEvent($this, $request), Events::ADMIN_HANDLE_FORM);
107 8
    }
108
109 4
    public function getRequest(): Request
110
    {
111 4
        if (null === $this->request) {
112 4
            throw new Exception('The handleRequest() method should be called before calling getRequest()');
113
        }
114
115 4
        return $this->request;
116
    }
117
118 4
    public function getName(): string
119
    {
120 4
        return $this->name;
121
    }
122
123 2
    public function getEntityClass(): string
124
    {
125 2
        return $this->configuration->get('entity');
126
    }
127
128 4
    public function getResource(): AdminResource
129
    {
130 4
        return $this->resource;
131
    }
132
133 4
    public function getEventDispatcher(): EventDispatcherInterface
134
    {
135 4
        return $this->eventDispatcher;
136
    }
137
138 4
    public function getConfiguration(): AdminConfiguration
139
    {
140 4
        return $this->configuration;
141
    }
142
143 4
    public function createView(): ViewInterface
144
    {
145 4
        if (null === $this->request) {
146 2
            throw new Exception('The handleRequest() method should be called before creating a view');
147
        }
148 2
        $event = new ViewEvent($this, $this->request);
149 2
        $this->eventDispatcher->dispatch($event, Events::ADMIN_VIEW);
150
151 2
        return $event->getView();
152
    }
153
154 2
    public function getAction(): ActionInterface
155
    {
156 2
        return $this->action;
157
    }
158
159 2
    public function hasAction(): bool
160
    {
161 2
        return null !== $this->action;
162
    }
163
164 4
    public function getEntities()
165
    {
166 4
        return $this->entities;
167
    }
168
169 4
    public function getForms(): array
170
    {
171 4
        return $this->forms;
172
    }
173
174 4
    public function hasForm(string $name): bool
175
    {
176 4
        return key_exists($name, $this->forms);
177
    }
178
179 4
    public function getData()
180
    {
181 4
182 4
    }
183
184
    public function getForm(string $name): FormInterface
185 4
    {
186
        if (!$this->hasForm($name)) {
187
            throw new Exception('Form "'.$name.'" does not exists in Admin "'.$this->name.'"');
188 8
        }
189
190 8
        return $this->forms[$name];
191 2
    }
192
193 6
    private function handleEntityForm(Request $request)
194 6
    {
195
        if (!key_exists('entity', $this->forms)) {
196 6
            return;
197 4
        }
198 2
        $form = $this->forms['entity'];
199
        $form->handleRequest($request);
200 4
201 4
        if ($form->isSubmitted() && $form->isValid()) {
202
            if ($this->entities->isEmpty()) {
203 6
                $this->entities->add($form->getData());
204
            }
205
            $event = new EntityEvent($this, $request);
206
            $this->eventDispatcher->dispatch($event, Events::ENTITY_SAVE);
207
        }
208
    }
209
}
210