Completed
Pull Request — master (#71)
by Arnaud
14:19 queued 12:02
created

AdminFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * AdminFactory.
24
 *
25
 * Create admin from configuration
26
 */
27
class AdminFactory
28
{
29
    /**
30
     * @var bool
31
     */
32
    protected $isInit = false;
33
34
    /**
35
     * @var EventDispatcherInterface
36
     */
37
    protected $eventDispatcher;
38
39
    /**
40
     * @var ConfigurationFactory
41
     */
42
    protected $configurationFactory;
43
44
    /**
45
     * @var AdminConfiguration[]
46
     */
47
    protected $adminConfigurations;
48
49
    /**
50
     * @var ActionFactory
51
     */
52
    protected $actionFactory;
53
54
    /**
55
     * @var MessageHandlerInterface
56
     */
57
    protected $messageHandler;
58
59
    /**
60
     * @var Registry
61
     */
62
    protected $registry;
63
64
    /**
65
     * @var RequestFilterFactory
66
     */
67
    protected $requestFilterFactory;
68
69
    /**
70
     * @var DataProviderFactory
71
     */
72
    protected $dataProviderFactory;
73
74
    /**
75
     * AdminFactory constructor.
76
     *
77
     * @param array $adminConfigurations
78
     * @param EventDispatcherInterface $eventDispatcher
79
     * @param ConfigurationFactory $configurationFactory
80
     * @param ActionFactory $actionFactory
81
     * @param MessageHandlerInterface $messageHandler
82
     * @param Registry $registry
83
     * @param RequestFilterFactory $requestFilterFactory
84
     * @param DataProviderFactory $dataProviderFactory
85
     */
86 4
    public function __construct(
87
        array $adminConfigurations,
88
        EventDispatcherInterface $eventDispatcher,
89
        ConfigurationFactory $configurationFactory,
90
        ActionFactory $actionFactory,
91
        MessageHandlerInterface $messageHandler,
92
        Registry $registry,
93
        RequestFilterFactory $requestFilterFactory,
94
        DataProviderFactory $dataProviderFactory
95
    ) {
96 4
        $this->eventDispatcher = $eventDispatcher;
97 4
        $this->configurationFactory = $configurationFactory;
98 4
        $this->adminConfigurations = $adminConfigurations;
99 4
        $this->actionFactory = $actionFactory;
100 4
        $this->messageHandler = $messageHandler;
101 4
        $this->registry = $registry;
102 4
        $this->requestFilterFactory = $requestFilterFactory;
103 4
        $this->dataProviderFactory = $dataProviderFactory;
104 4
    }
105
106
    /**
107
     * Create admins from configuration and load them into the pool. Dispatch ADMIN_CREATE event.
108
     */
109 2
    public function init()
110
    {
111
        // init only once
112 2
        if ($this->isInit) {
113 1
            return;
114
        }
115
        // dispatch an event to allow configuration modification before resolving and creating admins
116 2
        $event = new BeforeConfigurationEvent($this->adminConfigurations);
117
        $this
118 2
            ->eventDispatcher
119 2
            ->dispatch(AdminEvents::BEFORE_CONFIGURATION, $event);
120
121
        // get the modified configuration
122 2
        $this->adminConfigurations = $event->getAdminConfigurations();
123
124
        // create Admins according to the given configuration
125 2
        foreach ($this->adminConfigurations as $name => $configuration) {
126
127
            // dispatch an event to allow modification on a specific admin
128 1
            $event = new AdminCreateEvent($name, $configuration);
129
            $this
130 1
                ->eventDispatcher
131 1
                ->dispatch(AdminEvents::ADMIN_CREATE, $event);
132
133
            // create Admin object and add it to the registry
134 1
            $admin = $this->create($name, $event->getAdminConfiguration());
135
            $this
136 1
                ->registry
137 1
                ->add($admin);
138
139
            // dispatch post-create event
140 1
            $event = new AdminCreatedEvent($admin);
141
            $this
142 1
                ->eventDispatcher
143 1
                ->dispatch(AdminEvents::ADMIN_CREATED, $event);
144
        }
145 2
        $this->isInit = true;
146 2
    }
147
148
    /**
149
     * Create an Admin from configuration values. It will be added to AdminFactory admin's list.
150
     *
151
     * @param string $name
152
     * @param array $configuration
153
     * @return Admin
154
     * @throws Exception
155
     */
156 2
    public function create($name, array $configuration)
157
    {
158
        // create AdminConfiguration object
159
        $adminConfiguration = $this
160 2
            ->configurationFactory
161 2
            ->createAdminConfiguration($configuration);
162
163
        // retrieve a data provider
164 2
        $dataProvider = $this->getDataProvider(
165 2
            $adminConfiguration->getParameter('entity'),
166 2
            $adminConfiguration->getParameter('data_provider')
167
        );
168
169
        // retrieve a request filter
170
        $requestFilter = $this
171 2
            ->requestFilterFactory
172 2
            ->create($adminConfiguration);
173
174
        // create Admin object
175 2
        $admin = new Admin(
176
            $name,
177
            $dataProvider,
178
            $adminConfiguration,
179 2
            $this->messageHandler,
180 2
            $this->eventDispatcher,
181
            $requestFilter
182
        );
183
184
        // adding actions
185 2
        foreach ($adminConfiguration->getParameter('actions') as $actionName => $actionConfiguration) {
186
            // create action and add it to the admin instance
187 2
            $this->createAction($admin, $actionName, $actionConfiguration);
188
        }
189
190 2
        return $admin;
191
    }
192
193
    /**
194
     * @return boolean
195
     */
196
    public function isInit()
197
    {
198
        return $this->isInit;
199
    }
200
201
    /**
202
     * @return Registry
203
     */
204 2
    public function getRegistry()
205
    {
206 2
        return $this->registry;
207
    }
208
209
    /**
210
     * Create an Action from the configuration, and add it to the Admin.
211
     *
212
     * @param AdminInterface $admin
213
     * @param $actionName
214
     * @param array $actionConfiguration
215
     */
216 2
    protected function createAction(AdminInterface $admin, $actionName, array $actionConfiguration)
217
    {
218
        // creating action from configuration
219
        $action = $this
220 2
            ->actionFactory
221 2
            ->create($actionName, $actionConfiguration, $admin);
222
223
        // adding action to admin
224 2
        $admin->addAction($action);
225 2
    }
226
227
    /**
228
     * Return a configured data provider or create an new instance of the default one.
229
     *
230
     * @param string $entityClass
231
     * @param string|null $name
232
     * @return DataProviderInterface
233
     * @throws Exception
234
     */
235 2
    protected function getDataProvider($entityClass, $name = null)
236
    {
237
        return $this
238 2
            ->dataProviderFactory
239 2
            ->get($name, $entityClass);
240
    }
241
242
}
243