Passed
Branch master (c87ba8)
by Christian
16:02
created

WidgetRegistry::getBackendUser()   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\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Dashboard\Widgets\WidgetInterface;
25
26
/**
27
 * @internal
28
 */
29
class WidgetRegistry implements SingletonInterface
30
{
31
    /**
32
     * @var ContainerInterface
33
     */
34
    protected $container;
35
36
    /**
37
     * @var WidgetInterface[]
38
     */
39
    private $widgets = [];
40
41
    /**
42
     * @var array
43
     */
44
    private $widgetsPerWidgetGroup = [];
45
46
    public function __construct(ContainerInterface $container)
47
    {
48
        $this->container = $container;
49
    }
50
51
    public function getAvailableWidgets(): array
52
    {
53
        return $this->checkPermissionOfWidgets($this->widgets);
54
    }
55
56
    public function getAllWidgets(): array
57
    {
58
        return $this->widgets;
59
    }
60
61
    /**
62
     * @throws \InvalidArgumentException If requested identifier does not exist.
63
     */
64
    public function getAvailableWidget(string $identifier): WidgetInterface
65
    {
66
        if (array_key_exists($identifier, $this->getAvailableWidgets())) {
67
            return $this->container->get($this->widgets[$identifier]->getServiceName());
0 ignored issues
show
Bug introduced by
The method getServiceName() does not exist on TYPO3\CMS\Dashboard\Widgets\WidgetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            return $this->container->get($this->widgets[$identifier]->/** @scrutinizer ignore-call */ getServiceName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        }
69
70
        throw new \InvalidArgumentException('Requested widget "' . $identifier . '" does not exist.', 1584777201);
71
    }
72
73
    public function getAvailableWidgetsForWidgetGroup(string $widgetGroupIdentifier): array
74
    {
75
        if (!array_key_exists($widgetGroupIdentifier, $this->widgetsPerWidgetGroup)) {
76
            return [];
77
        }
78
        return $this->checkPermissionOfWidgets($this->widgetsPerWidgetGroup[$widgetGroupIdentifier]);
79
    }
80
81
    public function registerWidget(string $serviceName): void
82
    {
83
        $widgetConfiguration = $this->container->get($serviceName);
84
        $this->widgets[$widgetConfiguration->getIdentifier()] = $widgetConfiguration;
85
        foreach ($widgetConfiguration->getGroupNames() as $groupIdentifier) {
86
            $this->widgetsPerWidgetGroup = ArrayUtility::setValueByPath(
87
                $this->widgetsPerWidgetGroup,
88
                $groupIdentifier . '/' . $widgetConfiguration->getIdentifier(),
89
                $widgetConfiguration
90
            );
91
        }
92
    }
93
94
    protected function checkPermissionOfWidgets(array $widgets): array
95
    {
96
        return array_filter($widgets, function ($identifier) {
97
            return $this->getBackendUser()->check('available_widgets', $identifier);
98
        }, ARRAY_FILTER_USE_KEY);
99
    }
100
101
    public function widgetItemsProcFunc(array &$parameters): void
102
    {
103
        foreach ($this->widgets as $widget) {
104
            $parameters['items'][] = [
105
                $widget->getTitle(),
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on TYPO3\CMS\Dashboard\Widgets\WidgetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
                $widget->/** @scrutinizer ignore-call */ 
106
                         getTitle(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
                $widget->getIdentifier(),
0 ignored issues
show
Bug introduced by
The method getIdentifier() does not exist on TYPO3\CMS\Dashboard\Widgets\WidgetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
                $widget->/** @scrutinizer ignore-call */ 
107
                         getIdentifier(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
                $widget->getIconIdentifier()
0 ignored issues
show
Bug introduced by
The method getIconIdentifier() does not exist on TYPO3\CMS\Dashboard\Widgets\WidgetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
                $widget->/** @scrutinizer ignore-call */ 
108
                         getIconIdentifier()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
            ];
109
        }
110
    }
111
112
    protected function getBackendUser(): BackendUserAuthentication
113
    {
114
        return $GLOBALS['BE_USER'];
115
    }
116
}
117