Completed
Pull Request — master (#116)
by Arnaud
05:17
created

Admin::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
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\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\Form\FormInterface;
18
use Symfony\Component\HttpFoundation\Request;
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
     * @param AdminResource            $resource
66
     * @param AdminConfiguration       $configuration
67
     * @param EventDispatcherInterface $eventDispatcher
68
     */
69
    public function __construct(
70
        AdminResource $resource,
71
        AdminConfiguration $configuration,
72
        EventDispatcherInterface $eventDispatcher
73
    ) {
74
        $this->name = $resource->getName();
75
        $this->configuration = $configuration;
76
        $this->resource = $resource;
77
        $this->eventDispatcher = $eventDispatcher;
78
        $this->entities = new ArrayCollection();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function handleRequest(Request $request)
85
    {
86
        $this->request = $request;
87
        $event = new AdminEvent($this, $request);
88
        $this->eventDispatcher->dispatch(Events::HANDLE_REQUEST, $event);
89
90
        if (!$event->hasAction()) {
91
            throw new Exception('The current action was not set during the dispatch of the event');
92
        }
93
        $this->action = $event->getAction();
94
95
        // Dispatch an event to allow entities to be filtered
96
        $filterEvent = new FilterEvent($this, $request);
97
        $this->eventDispatcher->dispatch(Events::FILTER, $filterEvent);
98
99
        $event = new EntityEvent($this, $request);
100
        $event->setFilters($filterEvent->getFilters());
101
        $this->eventDispatcher->dispatch(Events::ENTITY_LOAD, $event);
102
103
        if (null !== $event->getEntities()) {
104
            $this->entities = $event->getEntities();
105
        }
106
107
        $event = new FormEvent($this, $request);
108
        $this->eventDispatcher->dispatch(Events::CREATE_FORM, $event);
109
110
        // Merge the regular forms and the filter forms
111
        $this->forms = array_merge($event->getForms(), $filterEvent->getForms());
112
113
        $this->handleEntityForm($request);
114
        $this->eventDispatcher->dispatch(Events::HANDLE_FORM, new FormEvent($this, $request));
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getName(): string
121
    {
122
        return $this->name;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getEntityClass(): string
129
    {
130
        return $this->configuration->get('entity');
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getResource(): AdminResource
137
    {
138
        return $this->resource;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getEventDispatcher(): EventDispatcherInterface
145
    {
146
        return $this->eventDispatcher;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getConfiguration(): AdminConfiguration
153
    {
154
        return $this->configuration;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function createView(): ViewInterface
161
    {
162
        $event = new ViewEvent($this, $this->request);
163
        $this->eventDispatcher->dispatch(Events::VIEW, $event);
164
165
        return $event->getView();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getAction(): ActionInterface
172
    {
173
        return $this->action;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function hasAction(): bool
180
    {
181
        return null !== $this->action;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function getEntities()
188
    {
189
        return $this->entities;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getForms(): array
196
    {
197
        return $this->forms;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function hasForm(string $name): bool
204
    {
205
        return key_exists($name, $this->forms);
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getForm(string $name): FormInterface
212
    {
213
        if (!$this->hasForm($name)) {
214
            throw new Exception('Form "'.$name.'" does not exists in Admin "'.$this->name.'"');
215
        }
216
217
        return $this->forms[$name];
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    private function handleEntityForm(Request $request)
224
    {
225
        if (!key_exists('entity', $this->forms)) {
226
            return;
227
        }
228
        $form = $this->forms['entity'];
229
        $form->handleRequest($request);
230
231
        if ($form->isSubmitted() && $form->isValid()) {
232
            if ($this->entities->isEmpty()) {
233
                $this->entities->add($form->getData());
234
            }
235
            $event = new EntityEvent($this, $request);
236
            $this->eventDispatcher->dispatch(Events::ENTITY_SAVE, $event);
237
        }
238
    }
239
}
240