Completed
Push — refonte ( a2a436...c098ff )
by Arnaud
07:46
created

Admin::handleEntityForm()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 4
nop 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\AdminEvent;
8
use LAG\AdminBundle\Event\AdminEvents;
9
use LAG\AdminBundle\Event\EntityEvent;
10
use LAG\AdminBundle\Event\FilterEvent;
11
use LAG\AdminBundle\Event\FormEvent;
12
use LAG\AdminBundle\Event\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 array
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array of property $entities.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function handleRequest(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
85
    {
86
        $this->request = $request;
87
        $event = new AdminEvent($this, $request);
88
        $this->eventDispatcher->dispatch(AdminEvents::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(AdminEvents::FILTER, $filterEvent);
98
99
        $event = new EntityEvent($this, $request);
100
        $event->setFilters($filterEvent->getFilters());
101
        $this->eventDispatcher->dispatch(AdminEvents::ENTITY_LOAD, $event);
102
103
        if (null !== $event->getEntities()) {
104
            $this->entities = $event->getEntities();
0 ignored issues
show
Documentation Bug introduced by
It seems like $event->getEntities() of type * is incompatible with the declared type array of property $entities.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
        }
106
107
        $event = new FormEvent($this, $request);
108
        $this->eventDispatcher->dispatch(AdminEvents::HANDLE_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
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function getName(): string
120
    {
121
        return $this->name;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function getResource(): AdminResource
128
    {
129
        return $this->resource;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function getEventDispatcher(): EventDispatcherInterface
136
    {
137
        return $this->eventDispatcher;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function getConfiguration(): AdminConfiguration
144
    {
145
        return $this->configuration;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function createView(): ViewInterface
152
    {
153
        $event = new ViewEvent($this, $this->request);
154
        $this->eventDispatcher->dispatch(AdminEvents::VIEW, $event);
155
156
        return $event->getView();
157
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162
    public function getAction(): ActionInterface
163
    {
164
        return $this->action;
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170
    public function hasAction(): bool
171
    {
172
        return null !== $this->action;
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function getEntities()
179
    {
180
        return $this->entities;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->entities; (array) is incompatible with the return type declared by the interface LAG\AdminBundle\Admin\AdminInterface::getEntities of type Doctrine\Common\Collections\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
181
    }
182
183
    /**
184
     * @inheritdoc
185
     */
186
    public function getForms(): array
187
    {
188
        return $this->forms;
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194
    public function hasForm(string $name): bool
195
    {
196
        return key_exists($name, $this->forms);
197
    }
198
199
    /**
200
     * @inheritdoc
201
     */
202
    public function getForm(string $name): FormInterface
203
    {
204
        if (!$this->hasForm($name)) {
205
            throw new Exception('Form "'.$name.'" does not exists in Admin "'.$this->name.'"');
206
        }
207
208
        return $this->forms[$name];
209
    }
210
211
    /**
212
     * @inheritdoc
213
     */
214
    private function handleEntityForm(Request $request)
215
    {
216
        if (!key_exists('entity', $this->forms)) {
217
            return;
218
        }
219
        $form = $this->forms['entity'];
220
        $form->handleRequest($request);
221
222
        if ($form->isSubmitted() && $form->isValid()) {
223
            if ($this->entities->isEmpty()) {
0 ignored issues
show
Bug introduced by
The method isEmpty cannot be called on $this->entities (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
224
                $this->entities->add($form->getData());
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->entities (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
225
            }
226
            $event = new EntityEvent($this, $request);
227
            $this->eventDispatcher->dispatch(AdminEvents::ENTITY_SAVE, $event);
228
        }
229
    }
230
}
231