Passed
Push — master ( 24ec4a...f04f93 )
by Roeland
13:26 queued 11s
created

Manager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getPanels() 0 3 1
A registerPanel() 0 6 2
A loadLazyPanels() 0 43 5
A lazyRegisterPanel() 0 2 1
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\IPanel;
33
use OCP\ILogger;
34
use OCP\IServerContainer;
35
use Throwable;
36
37
class Manager implements IManager {
38
39
	/** @var array */
40
	private $lazyPanels = [];
41
42
	/** @var IPanel[] */
43
	private $panels = [];
44
45
	/** @var IServerContainer */
46
	private $serverContainer;
47
48
	public function __construct(IServerContainer $serverContainer) {
49
		$this->serverContainer = $serverContainer;
50
	}
51
52
	private function registerPanel(IPanel $panel): void {
53
		if (array_key_exists($panel->getId(), $this->panels)) {
54
			throw new InvalidArgumentException('Dashboard panel with this id has already been registered');
55
		}
56
57
		$this->panels[$panel->getId()] = $panel;
58
	}
59
60
	public function lazyRegisterPanel(string $panelClass): void {
61
		$this->lazyPanels[] = $panelClass;
62
	}
63
64
	public function loadLazyPanels(): void {
65
		$classes = $this->lazyPanels;
66
		foreach ($classes as $class) {
67
			try {
68
				/** @var IPanel $panel */
69
				$panel = $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 panel: ' . $e->getMessage(),
77
					'level' => ILogger::FATAL,
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->registerPanel($panel);
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 panel: ' . $e->getMessage(),
93
					'level' => ILogger::FATAL,
94
				]);
95
			}
96
97
			try {
98
				$panel->load();
99
			} catch (Throwable $e) {
100
				\OC::$server->getLogger()->logException($e, [
101
					'message' => 'Error during dashboard panel loading: ' . $e->getMessage(),
102
					'level' => ILogger::FATAL,
103
				]);
104
			}
105
		}
106
		$this->lazyPanels = [];
107
	}
108
109
	public function getPanels(): array {
110
		$this->loadLazyPanels();
111
		return $this->panels;
112
	}
113
}
114