Passed
Push — master ( c43fa0...9560e0 )
by
unknown
25:03 queued 11:18
created

Dashboard::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Dashboard;
19
20
use Psr\Container\ContainerInterface;
21
use TYPO3\CMS\Core\Localization\LanguageService;
22
use TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface;
23
24
/**
25
 * @internal
26
 */
27
class Dashboard
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $identifier;
33
34
    /**
35
     * @var string
36
     */
37
    protected $title;
38
39
    /**
40
     * @var array<string,array<string,string>>
41
     */
42
    protected $widgetConfig;
43
44
    /**
45
     * @var WidgetRegistry
46
     */
47
    protected $widgetRegistry;
48
49
    /**
50
     * @var ContainerInterface
51
     */
52
    protected $container;
53
54
    /**
55
     * @var array<string,WidgetConfigurationInterface>
56
     */
57
    protected $widgets = [];
58
59
    /**
60
     * @var array<string,array>
61
     */
62
    protected $widgetOptions = [];
63
64
    public function __construct(
65
        string $identifier,
66
        string $title,
67
        array $widgetConfig,
68
        WidgetRegistry $widgetRegistry,
69
        ContainerInterface $container
70
    ) {
71
        $this->identifier = $identifier;
72
        $this->title = $title;
73
        $this->widgetConfig = $widgetConfig;
74
        $this->widgetRegistry = $widgetRegistry;
75
        $this->container = $container;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getIdentifier(): string
82
    {
83
        return $this->identifier;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getTitle(): string
90
    {
91
        return $this->getLanguageService()->sL($this->title) ?: $this->title;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getWidgetConfig(): array
98
    {
99
        return $this->widgetConfig;
100
    }
101
102
    /**
103
     * @return array<string,WidgetConfigurationInterface>
104
     */
105
    public function getWidgets(): array
106
    {
107
        return $this->widgets;
108
    }
109
110
    /**
111
     * This will return a list of all widgets of the current dashboard object. It will only include available
112
     * widgets and will add the initialized object of the widget itself
113
     */
114
    public function initializeWidgets(): void
115
    {
116
        $availableWidgets = $this->widgetRegistry->getAvailableWidgets();
117
        foreach ($this->widgetConfig as $hash => $widgetConfig) {
118
            if (array_key_exists($widgetConfig['identifier'], $availableWidgets)) {
119
                $this->widgets[$hash] = $availableWidgets[$widgetConfig['identifier']];
120
121
                $widgetObject = $this->widgetRegistry->getAvailableWidget($widgetConfig['identifier']);
122
                if (method_exists($widgetObject, 'getOptions')) {
123
                    $this->widgetOptions[$hash] = $widgetObject->getOptions();
124
                }
125
            }
126
        }
127
    }
128
129
    /**
130
     * @return LanguageService
131
     */
132
    protected function getLanguageService(): LanguageService
133
    {
134
        return $GLOBALS['LANG'];
135
    }
136
137
    /**
138
     * @return array<string,array>
139
     */
140
    public function getWidgetOptions(): array
141
    {
142
        return $this->widgetOptions;
143
    }
144
}
145