Passed
Push — master ( acba3d...0e2f31 )
by Roeland
35:10 queued 21:35
created

Manager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A registerWidget() 0 6 2
A lazyRegisterWidget() 0 2 1
A getWidgets() 0 3 1
B loadLazyPanels() 0 52 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OC\Dashboard;
28
29
use InvalidArgumentException;
30
use OCP\AppFramework\QueryException;
31
use OCP\Dashboard\IManager;
32
use OCP\Dashboard\IWidget;
33
use OCP\ILogger;
34
use OCP\IServerContainer;
35
use Throwable;
36
37
class Manager implements IManager {
38
39
	/** @var array */
40
	private $lazyWidgets = [];
41
42
	/** @var IWidget[] */
43
	private $widgets = [];
44
45
	/** @var IServerContainer */
46
	private $serverContainer;
47
48
	public function __construct(IServerContainer $serverContainer) {
49
		$this->serverContainer = $serverContainer;
50
	}
51
52
	private function registerWidget(IWidget $widget): void {
53
		if (array_key_exists($widget->getId(), $this->widgets)) {
54
			throw new InvalidArgumentException('Dashboard widget with this id has already been registered');
55
		}
56
57
		$this->widgets[$widget->getId()] = $widget;
58
	}
59
60
	public function lazyRegisterWidget(string $widgetClass): void {
61
		$this->lazyWidgets[] = $widgetClass;
62
	}
63
64
	public function loadLazyPanels(): void {
65
		$classes = $this->lazyWidgets;
66
		foreach ($classes as $class) {
67
			try {
68
				/** @var IWidget $widget */
69
				$widget = $this->serverContainer->query($class);
70
			} catch (QueryException $e) {
71
				/*
72
				 * There is a circular dependency between the logger and the registry, so
73
				 * we can not inject it. Thus the static call.
74
				 */
75
				\OC::$server->getLogger()->logException($e, [
76
					'message' => 'Could not load lazy dashbaord widget: ' . $e->getMessage(),
77
					'level' => ILogger::FATAL,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::FATAL has been deprecated: 20.0.0 ( Ignorable by Annotation )

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

77
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::FATAL,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
78
				]);
79
			}
80
			/**
81
			 * Try to register the loaded reporter. Theoretically it could be of a wrong
82
			 * type, so we might get a TypeError here that we should catch.
83
			 */
84
			try {
85
				$this->registerWidget($widget);
86
			} catch (Throwable $e) {
87
				/*
88
				 * There is a circular dependency between the logger and the registry, so
89
				 * we can not inject it. Thus the static call.
90
				 */
91
				\OC::$server->getLogger()->logException($e, [
92
					'message' => 'Could not register lazy dashboard widget: ' . $e->getMessage(),
93
					'level' => ILogger::FATAL,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::FATAL has been deprecated: 20.0.0 ( Ignorable by Annotation )

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

93
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::FATAL,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
94
				]);
95
			}
96
97
			try {
98
				$startTime = microtime(true);
99
				$widget->load();
100
				$endTime = microtime(true);
101
				$duration = $endTime - $startTime;
102
				if ($duration > 1) {
103
					\OC::$server->getLogger()->error('Dashboard widget {widget} took {duration} seconds to load.', [
104
						'widget' => $widget->getId(),
105
						'duration' => round($duration, 2),
106
					]);
107
				}
108
			} catch (Throwable $e) {
109
				\OC::$server->getLogger()->logException($e, [
110
					'message' => 'Error during dashboard widget loading: ' . $e->getMessage(),
111
					'level' => ILogger::FATAL,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::FATAL has been deprecated: 20.0.0 ( Ignorable by Annotation )

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

111
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::FATAL,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
112
				]);
113
			}
114
		}
115
		$this->lazyWidgets = [];
116
	}
117
118
	public function getWidgets(): array {
119
		$this->loadLazyPanels();
120
		return $this->widgets;
121
	}
122
}
123