Completed
Pull Request — master (#71)
by Arnaud
15:22 queued 12:55
created

AdminFactory::addDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Factory;
4
5
use LAG\AdminBundle\Action\Factory\ActionFactory;
6
use LAG\AdminBundle\Admin\Admin;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
9
use LAG\AdminBundle\Admin\Event\AdminCreatedEvent;
10
use LAG\AdminBundle\Admin\Event\AdminCreateEvent;
11
use LAG\AdminBundle\Admin\Event\BeforeConfigurationEvent;
12
use LAG\AdminBundle\Admin\Registry\Registry;
13
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory;
14
use LAG\AdminBundle\Admin\Event\AdminEvents;
15
use Exception;
16
use LAG\AdminBundle\DataProvider\DataProviderInterface;
17
use LAG\AdminBundle\DataProvider\Factory\DataProviderFactory;
18
use LAG\AdminBundle\Filter\Factory\RequestFilterFactory;
19
use LAG\AdminBundle\Message\MessageHandlerInterface;
20
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23
/**
24
 * AdminFactory.
25
 *
26
 * Create admin from configuration
27
 */
28
class AdminFactory
29
{
30
    /**
31
     * @var bool
32
     */
33
    protected $isInit = false;
34
35
    /**
36
     * @var EventDispatcherInterface
37
     */
38
    protected $eventDispatcher;
39
40
    /**
41
     * @var ConfigurationFactory
42
     */
43
    protected $configurationFactory;
44
45
    /**
46
     * @var AdminConfiguration[]
47
     */
48
    protected $adminConfigurations;
49
50
    /**
51
     * @var ActionFactory
52
     */
53
    protected $actionFactory;
54
55
    /**
56
     * @var MessageHandlerInterface
57
     */
58
    protected $messageHandler;
59
60
    /**
61
     * @var Registry
62
     */
63
    protected $registry;
64
65
    /**
66
     * @var RequestFilterFactory
67
     */
68
    protected $requestFilterFactory;
69
70
    /**
71
     * @var DataProviderFactory
72
     */
73
    protected $dataProviderFactory;
74
75
    /**
76
     * AdminFactory constructor.
77
     *
78
     * @param array $adminConfigurations
79
     * @param EventDispatcherInterface $eventDispatcher
80
     * @param ConfigurationFactory $configurationFactory
81
     * @param ActionFactory $actionFactory
82
     * @param MessageHandlerInterface $messageHandler
83
     * @param Registry $registry
84
     * @param RequestFilterFactory $requestFilterFactory
85
     * @param DataProviderFactory $dataProviderFactory
86
     */
87 4
    public function __construct(
88
        array $adminConfigurations,
89
        EventDispatcherInterface $eventDispatcher,
90
        ConfigurationFactory $configurationFactory,
91
        ActionFactory $actionFactory,
92
        MessageHandlerInterface $messageHandler,
93
        Registry $registry,
94
        RequestFilterFactory $requestFilterFactory,
95
        DataProviderFactory $dataProviderFactory
96
    ) {
97 4
        $this->eventDispatcher = $eventDispatcher;
98 4
        $this->configurationFactory = $configurationFactory;
99 4
        $this->adminConfigurations = $adminConfigurations;
100 4
        $this->actionFactory = $actionFactory;
101 4
        $this->messageHandler = $messageHandler;
102 4
        $this->dataProviders = new ParameterBag();
0 ignored issues
show
Bug introduced by
The property dataProviders does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103 4
        $this->registry = $registry;
104 4
        $this->requestFilterFactory = $requestFilterFactory;
105 4
        $this->dataProviderFactory = $dataProviderFactory;
106 4
    }
107
108
    /**
109
     * Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event.
110
     */
111 2
    public function init()
112
    {
113
        // init only once
114 2
        if ($this->isInit) {
115 1
            return;
116
        }
117
        // dispatch an event to allow configuration modification before resolving and creating admins
118 2
        $event = new BeforeConfigurationEvent($this->adminConfigurations);
119
        $this
120 2
            ->eventDispatcher
121 2
            ->dispatch(AdminEvents::BEFORE_CONFIGURATION, $event);
122
123
        // get the modified configuration
124 2
        $this->adminConfigurations = $event->getAdminConfigurations();
125
126
        // create Admins according to the given configuration
127 2
        foreach ($this->adminConfigurations as $name => $configuration) {
128
129
            // dispatch an event to allow modification on a specific admin
130 1
            $event = new AdminCreateEvent($name, $configuration);
131
            $this
132 1
                ->eventDispatcher
133 1
                ->dispatch(AdminEvents::ADMIN_CREATE, $event);
134
135
            // create Admin object and add it to the registry
136 1
            $admin = $this->create($name, $event->getAdminConfiguration());
137
            $this
138 1
                ->registry
139 1
                ->add($admin);
140
141
            // dispatch post-create event
142 1
            $event = new AdminCreatedEvent($admin);
143
            $this
144 1
                ->eventDispatcher
145 1
                ->dispatch(AdminEvents::ADMIN_CREATED, $event);
146
        }
147 2
        $this->isInit = true;
148 2
    }
149
150
    /**
151
     * Create an Admin from configuration values. It will be added to AdminFactory admin's list.
152
     *
153
     * @param string $name
154
     * @param array $configuration
155
     * @return Admin
156
     * @throws Exception
157
     */
158 2
    public function create($name, array $configuration)
159
    {
160
        // create AdminConfiguration object
161
        $adminConfiguration = $this
162 2
            ->configurationFactory
163 2
            ->createAdminConfiguration($configuration);
164
165
        // retrieve a data provider
166 2
        $dataProvider = $this->getDataProvider(
167 2
            $adminConfiguration->getParameter('entity'),
168 2
            $adminConfiguration->getParameter('data_provider')
169
        );
170
171
        // retrieve a request filter
172
        $requestFilter = $this
173 2
            ->requestFilterFactory
174 2
            ->create($adminConfiguration);
175
176
        // create Admin object
177 2
        $admin = new Admin(
178
            $name,
179
            $dataProvider,
180
            $adminConfiguration,
181 2
            $this->messageHandler,
182 2
            $this->eventDispatcher,
183
            $requestFilter
184
        );
185
186
        // adding actions
187 2
        foreach ($adminConfiguration->getParameter('actions') as $actionName => $actionConfiguration) {
188
            // create action and add it to the admin instance
189 2
            $this->createAction($admin, $actionName, $actionConfiguration);
190
        }
191
192 2
        return $admin;
193
    }
194
195
    /**
196
     * @return boolean
197
     */
198
    public function isInit()
199
    {
200
        return $this->isInit;
201
    }
202
203
    /**
204
     * @return Registry
205
     */
206 2
    public function getRegistry()
207
    {
208 2
        return $this->registry;
209
    }
210
211
    /**
212
     * Create an Action from the configuration, and add it to the Admin.
213
     *
214
     * @param AdminInterface $admin
215
     * @param $actionName
216
     * @param array $actionConfiguration
217
     */
218 2
    protected function createAction(AdminInterface $admin, $actionName, array $actionConfiguration)
219
    {
220
        // creating action from configuration
221
        $action = $this
222 2
            ->actionFactory
223 2
            ->create($actionName, $actionConfiguration, $admin);
224
225
        // adding action to admin
226 2
        $admin->addAction($action);
227 2
    }
228
229
    /**
230
     * Return a configured data provider or create an new instance of the default one.
231
     *
232
     * @param string $entityClass
233
     * @param string|null $name
234
     * @return DataProviderInterface
235
     * @throws Exception
236
     */
237 2
    protected function getDataProvider($entityClass, $name = null)
238
    {
239
        return $this
240 2
            ->dataProviderFactory
241 2
            ->get($name, $entityClass);
242
    }
243
244
}
245