Completed
Pull Request — master (#150)
by Arnaud
05:49 queued 01:55
created

Admin   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
c 1
b 0
f 0
dl 0
loc 182
rs 10
wmc 25

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getEntityClass() 0 3 1
A handleRequest() 0 30 3
A getRequest() 0 7 2
A getForm() 0 7 2
A getResource() 0 3 1
A createView() 0 9 2
A hasForm() 0 3 1
A getEntities() 0 3 1
A getAction() 0 3 1
A getConfiguration() 0 3 1
A hasAction() 0 3 1
A handleEntityForm() 0 14 5
A getForms() 0 3 1
A getName() 0 3 1
A getEventDispatcher() 0 3 1
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
    public function __construct(
66
        AdminResource $resource,
67
        AdminConfiguration $configuration,
68
        EventDispatcherInterface $eventDispatcher
69
    ) {
70
        $this->name = $resource->getName();
71
        $this->configuration = $configuration;
72
        $this->resource = $resource;
73
        $this->eventDispatcher = $eventDispatcher;
74
        $this->entities = new ArrayCollection();
75
    }
76
77
    public function handleRequest(Request $request)
78
    {
79
        $this->request = $request;
80
        $event = new AdminEvent($this, $request);
81
        $this->eventDispatcher->dispatch(Events::ADMIN_HANDLE_REQUEST, $event);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
82
                                dispatch(Events::ADMIN_HANDLE_REQUEST, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
LAG\AdminBundle\Event\Events::ADMIN_HANDLE_REQUEST of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ Events::ADMIN_HANDLE_REQUEST, $event);
Loading history...
82
83
        if (!$event->hasAction()) {
84
            throw new Exception('The current action was not set during the dispatch of the event');
85
        }
86
        $this->action = $event->getAction();
87
88
        // Dispatch an event to allow entities to be filtered
89
        $filterEvent = new FilterEvent($this, $request);
90
        $this->eventDispatcher->dispatch(Events::ADMIN_FILTER, $filterEvent);
91
92
        $event = new EntityEvent($this, $request);
93
        $event->setFilters($filterEvent->getFilters());
94
        $this->eventDispatcher->dispatch(Events::ENTITY_LOAD, $event);
95
96
        if (null !== $event->getEntities()) {
97
            $this->entities = $event->getEntities();
98
        }
99
        $event = new FormEvent($this, $request);
100
        $this->eventDispatcher->dispatch(Events::ADMIN_CREATE_FORM, $event);
101
102
        // Merge the regular forms and the filter forms
103
        $this->forms = array_merge($filterEvent->getForms(), $event->getForms());
104
105
        $this->handleEntityForm($request);
106
        $this->eventDispatcher->dispatch(Events::ADMIN_HANDLE_FORM, new FormEvent($this, $request));
107
    }
108
109
    public function getRequest(): Request
110
    {
111
        if (null === $this->request) {
112
            throw new Exception('The handleRequest() method should be called before calling getRequest()');
113
        }
114
115
        return $this->request;
116
    }
117
118
    public function getName(): string
119
    {
120
        return $this->name;
121
    }
122
123
    public function getEntityClass(): string
124
    {
125
        return $this->configuration->get('entity');
126
    }
127
128
    public function getResource(): AdminResource
129
    {
130
        return $this->resource;
131
    }
132
133
    public function getEventDispatcher(): EventDispatcherInterface
134
    {
135
        return $this->eventDispatcher;
136
    }
137
138
    public function getConfiguration(): AdminConfiguration
139
    {
140
        return $this->configuration;
141
    }
142
143
    public function createView(): ViewInterface
144
    {
145
        if (null === $this->request) {
146
            throw new Exception('The handleRequest() method should be called before creating a view');
147
        }
148
        $event = new ViewEvent($this, $this->request);
149
        $this->eventDispatcher->dispatch(Events::ADMIN_VIEW, $event);
0 ignored issues
show
Bug introduced by
LAG\AdminBundle\Event\Events::ADMIN_VIEW of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ Events::ADMIN_VIEW, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
150
                                dispatch(Events::ADMIN_VIEW, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
150
151
        return $event->getView();
152
    }
153
154
    public function getAction(): ActionInterface
155
    {
156
        return $this->action;
157
    }
158
159
    public function hasAction(): bool
160
    {
161
        return null !== $this->action;
162
    }
163
164
    public function getEntities()
165
    {
166
        return $this->entities;
167
    }
168
169
    public function getForms(): array
170
    {
171
        return $this->forms;
172
    }
173
174
    public function hasForm(string $name): bool
175
    {
176
        return key_exists($name, $this->forms);
177
    }
178
179
    public function getForm(string $name): FormInterface
180
    {
181
        if (!$this->hasForm($name)) {
182
            throw new Exception('Form "'.$name.'" does not exists in Admin "'.$this->name.'"');
183
        }
184
185
        return $this->forms[$name];
186
    }
187
188
    private function handleEntityForm(Request $request)
189
    {
190
        if (!key_exists('entity', $this->forms)) {
191
            return;
192
        }
193
        $form = $this->forms['entity'];
194
        $form->handleRequest($request);
195
196
        if ($form->isSubmitted() && $form->isValid()) {
197
            if ($this->entities->isEmpty()) {
198
                $this->entities->add($form->getData());
199
            }
200
            $event = new EntityEvent($this, $request);
201
            $this->eventDispatcher->dispatch(Events::ENTITY_SAVE, $event);
0 ignored issues
show
Bug introduced by
LAG\AdminBundle\Event\Events::ENTITY_SAVE of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
            $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ Events::ENTITY_SAVE, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
202
                                    dispatch(Events::ENTITY_SAVE, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
202
        }
203
    }
204
}
205