Completed
Push — 3.x ( f7534c...26d69b )
by Grégoire
04:05
created

Pool::getTitleLogo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Admin;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
17
18
/**
19
 * @author Thomas Rabaix <[email protected]>
20
 */
21
class Pool
22
{
23
    /**
24
     * @var ContainerInterface
25
     */
26
    protected $container;
27
28
    /**
29
     * @var string[]
30
     */
31
    protected $adminServiceIds = array();
32
33
    /**
34
     * @var array
35
     */
36
    protected $adminGroups = array();
37
38
    /**
39
     * @var array
40
     */
41
    protected $adminClasses = array();
42
43
    /**
44
     * @var string[]
45
     */
46
    protected $templates = array();
47
48
    /**
49
     * @var array
50
     */
51
    protected $assets = array();
52
53
    /**
54
     * @var string
55
     */
56
    protected $title;
57
58
    /**
59
     * @var string
60
     */
61
    protected $titleLogo;
62
63
    /**
64
     * @var array
65
     */
66
    protected $options;
67
68
    /**
69
     * @var PropertyAccessorInterface
70
     */
71
    protected $propertyAccessor;
72
73
    /**
74
     * @param ContainerInterface $container
75
     * @param string             $title
76
     * @param string             $logoTitle
77
     * @param array              $options
78
     */
79
    public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array(), PropertyAccessorInterface $propertyAccessor = null)
80
    {
81
        $this->container = $container;
82
        $this->title = $title;
83
        $this->titleLogo = $logoTitle;
84
        $this->options = $options;
85
        $this->propertyAccessor = $propertyAccessor;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getGroups()
92
    {
93
        $groups = $this->adminGroups;
94
95
        foreach ($this->adminGroups as $name => $adminGroup) {
96
            foreach ($adminGroup as $id => $options) {
97
                $groups[$name][$id] = $this->getInstance($id);
98
            }
99
        }
100
101
        return $groups;
102
    }
103
104
    /**
105
     * Returns whether an admin group exists or not.
106
     *
107
     * @param string $group
108
     *
109
     * @return bool
110
     */
111
    public function hasGroup($group)
112
    {
113
        return isset($this->adminGroups[$group]);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getDashboardGroups()
120
    {
121
        $groups = $this->adminGroups;
122
123
        foreach ($this->adminGroups as $name => $adminGroup) {
124
            if (isset($adminGroup['items'])) {
125
                foreach ($adminGroup['items'] as $key => $item) {
126
                    // Only Admin Group should be returned
127
                    if ('' != $item['admin']) {
128
                        $admin = $this->getInstance($item['admin']);
129
130
                        if ($admin->showIn(AbstractAdmin::CONTEXT_DASHBOARD)) {
131
                            $groups[$name]['items'][$key] = $admin;
132
                        } else {
133
                            unset($groups[$name]['items'][$key]);
134
                        }
135
                    } else {
136
                        unset($groups[$name]['items'][$key]);
137
                    }
138
                }
139
            }
140
141
            if (empty($groups[$name]['items'])) {
142
                unset($groups[$name]);
143
            }
144
        }
145
146
        return $groups;
147
    }
148
149
    /**
150
     * Returns all admins related to the given $group.
151
     *
152
     * @param string $group
153
     *
154
     * @return array
155
     *
156
     * @throws \InvalidArgumentException
157
     */
158
    public function getAdminsByGroup($group)
159
    {
160
        if (!isset($this->adminGroups[$group])) {
161
            throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
162
        }
163
164
        $admins = array();
165
166
        if (!isset($this->adminGroups[$group]['items'])) {
167
            return $admins;
168
        }
169
170
        foreach ($this->adminGroups[$group]['items'] as $item) {
171
            $admins[] = $this->getInstance($item['admin']);
172
        }
173
174
        return $admins;
175
    }
176
177
    /**
178
     * Return the admin related to the given $class.
179
     *
180
     * @param string $class
181
     *
182
     * @return \Sonata\AdminBundle\Admin\AdminInterface|null
183
     */
184
    public function getAdminByClass($class)
185
    {
186
        if (!$this->hasAdminByClass($class)) {
187
            return;
188
        }
189
190
        if (!is_array($this->adminClasses[$class])) {
191
            throw new \RuntimeException('Invalid format for the Pool::adminClass property');
192
        }
193
194
        if (count($this->adminClasses[$class]) > 1) {
195
            throw new \RuntimeException(sprintf(
196
                'Unable to find a valid admin for the class: %s, there are too many registered: %s',
197
                $class,
198
                implode(', ', $this->adminClasses[$class])
199
            ));
200
        }
201
202
        return $this->getInstance($this->adminClasses[$class][0]);
203
    }
204
205
    /**
206
     * @param string $class
207
     *
208
     * @return bool
209
     */
210
    public function hasAdminByClass($class)
211
    {
212
        return isset($this->adminClasses[$class]);
213
    }
214
215
    /**
216
     * Returns an admin class by its Admin code
217
     * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post.
218
     *
219
     * @param string $adminCode
220
     *
221
     * @return \Sonata\AdminBundle\Admin\AdminInterface|false|null
222
     */
223
    public function getAdminByAdminCode($adminCode)
224
    {
225
        $codes = explode('|', $adminCode);
226
227
        if (false === $codes) {
228
            return false;
229
        }
230
231
        $admin = $this->getInstance($codes[0]);
232
        array_shift($codes);
233
234
        if (empty($codes)) {
235
            return $admin;
236
        }
237
238
        foreach ($codes as $code) {
239
            if (!$admin->hasChild($code)) {
240
                return false;
241
            }
242
243
            $admin = $admin->getChild($code);
244
        }
245
246
        return $admin;
247
    }
248
249
    /**
250
     * Returns a new admin instance depends on the given code.
251
     *
252
     * @param string $id
253
     *
254
     * @return AdminInterface
255
     *
256
     * @throws \InvalidArgumentException
257
     */
258
    public function getInstance($id)
259
    {
260
        if (!in_array($id, $this->adminServiceIds)) {
261
            throw new \InvalidArgumentException(sprintf('Admin service "%s" not found in admin pool.', $id));
262
        }
263
264
        return $this->container->get($id);
265
    }
266
267
    /**
268
     * @return ContainerInterface|null
269
     */
270
    public function getContainer()
271
    {
272
        return $this->container;
273
    }
274
275
    /**
276
     * @param array $adminGroups
277
     */
278
    public function setAdminGroups(array $adminGroups)
279
    {
280
        $this->adminGroups = $adminGroups;
281
    }
282
283
    /**
284
     * @return array
285
     */
286
    public function getAdminGroups()
287
    {
288
        return $this->adminGroups;
289
    }
290
291
    /**
292
     * @param array $adminServiceIds
293
     */
294
    public function setAdminServiceIds(array $adminServiceIds)
295
    {
296
        $this->adminServiceIds = $adminServiceIds;
297
    }
298
299
    /**
300
     * @return array
301
     */
302
    public function getAdminServiceIds()
303
    {
304
        return $this->adminServiceIds;
305
    }
306
307
    /**
308
     * @param array $adminClasses
309
     */
310
    public function setAdminClasses(array $adminClasses)
311
    {
312
        $this->adminClasses = $adminClasses;
313
    }
314
315
    /**
316
     * @return array
317
     */
318
    public function getAdminClasses()
319
    {
320
        return $this->adminClasses;
321
    }
322
323
    /**
324
     * @param array $templates
325
     */
326
    public function setTemplates(array $templates)
327
    {
328
        $this->templates = $templates;
329
    }
330
331
    /**
332
     * @return array
333
     */
334
    public function getTemplates()
335
    {
336
        return $this->templates;
337
    }
338
339
    /**
340
     * @param string $name
341
     *
342
     * @return null|string
343
     */
344
    public function getTemplate($name)
345
    {
346
        if (isset($this->templates[$name])) {
347
            return $this->templates[$name];
348
        }
349
    }
350
351
    /**
352
     * @return string
353
     */
354
    public function getTitleLogo()
355
    {
356
        return $this->titleLogo;
357
    }
358
359
    /**
360
     * @return string
361
     */
362
    public function getTitle()
363
    {
364
        return $this->title;
365
    }
366
367
    /**
368
     * @param string $name
369
     * @param mixed  $default
370
     *
371
     * @return mixed
372
     */
373
    public function getOption($name, $default = null)
374
    {
375
        if (isset($this->options[$name])) {
376
            return $this->options[$name];
377
        }
378
379
        return $default;
380
    }
381
382
    public function getPropertyAccessor()
383
    {
384
        if (null === $this->propertyAccessor) {
385
            $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
386
        }
387
388
        return $this->propertyAccessor;
389
    }
390
}
391