Passed
Push — master ( dd0b96...52dd1e )
by Roeland
11:37 queued 11s
created

Registry::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 *
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OC\Support\CrashReport;
29
30
use Exception;
31
use OCP\AppFramework\QueryException;
32
use OCP\ILogger;
33
use OCP\IServerContainer;
34
use OCP\Support\CrashReport\ICollectBreadcrumbs;
35
use OCP\Support\CrashReport\IMessageReporter;
36
use OCP\Support\CrashReport\IRegistry;
37
use OCP\Support\CrashReport\IReporter;
38
use Throwable;
39
40
class Registry implements IRegistry {
0 ignored issues
show
Deprecated Code introduced by
The interface OCP\Support\CrashReport\IRegistry has been deprecated: used internally only ( Ignorable by Annotation )

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

40
class Registry implements /** @scrutinizer ignore-deprecated */ IRegistry {

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

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

Loading history...
41
42
	/** @var string[] */
43
	private $lazyReporters = [];
44
45
	/** @var IReporter[] */
46
	private $reporters = [];
47
48
	/** @var IServerContainer */
49
	private $serverContainer;
50
51
	/** @var ILogger */
52
	private $logger;
0 ignored issues
show
introduced by
The private property $logger is not used, and could be removed.
Loading history...
53
54
	public function __construct(IServerContainer $serverContainer) {
55
		$this->serverContainer = $serverContainer;
56
	}
57
58
	/**
59
	 * Register a reporter instance
60
	 *
61
	 * @param IReporter $reporter
62
	 */
63
	public function register(IReporter $reporter): void {
64
		$this->reporters[] = $reporter;
65
	}
66
67
	public function registerLazy(string $class): void {
68
		$this->lazyReporters[] = $class;
69
	}
70
71
	/**
72
	 * Delegate breadcrumb collection to all registered reporters
73
	 *
74
	 * @param string $message
75
	 * @param string $category
76
	 * @param array $context
77
	 *
78
	 * @since 15.0.0
79
	 */
80
	public function delegateBreadcrumb(string $message, string $category, array $context = []): void {
81
		$this->loadLazyProviders();
82
83
		foreach ($this->reporters as $reporter) {
84
			if ($reporter instanceof ICollectBreadcrumbs) {
85
				$reporter->collect($message, $category, $context);
86
			}
87
		}
88
	}
89
90
	/**
91
	 * Delegate crash reporting to all registered reporters
92
	 *
93
	 * @param Exception|Throwable $exception
94
	 * @param array $context
95
	 */
96
	public function delegateReport($exception, array $context = []): void {
97
		$this->loadLazyProviders();
98
99
		foreach ($this->reporters as $reporter) {
100
			$reporter->report($exception, $context);
101
		}
102
	}
103
104
	/**
105
	 * Delegate a message to all reporters that implement IMessageReporter
106
	 *
107
	 * @param string $message
108
	 * @param array $context
109
	 *
110
	 * @return void
111
	 */
112
	public function delegateMessage(string $message, array $context = []): void {
113
		$this->loadLazyProviders();
114
115
		foreach ($this->reporters as $reporter) {
116
			if ($reporter instanceof IMessageReporter) {
117
				$reporter->reportMessage($message, $context);
118
			}
119
		}
120
	}
121
122
	private function loadLazyProviders(): void {
123
		$classes = $this->lazyReporters;
124
		foreach ($classes as $class) {
125
			try {
126
				/** @var IReporter $reporter */
127
				$reporter = $this->serverContainer->query($class);
128
			} catch (QueryException $e) {
129
				/*
130
				 * There is a circular dependency between the logger and the registry, so
131
				 * we can not inject it. Thus the static call.
132
				 */
133
				\OC::$server->getLogger()->logException($e, [
134
					'message' => 'Could not load lazy crash reporter: ' . $e->getMessage(),
135
					'level' => ILogger::FATAL,
136
				]);
137
			}
138
			/**
139
			 * Try to register the loaded reporter. Theoretically it could be of a wrong
140
			 * type, so we might get a TypeError here that we should catch.
141
			 */
142
			try {
143
				$this->register($reporter);
144
			} catch (Throwable $e) {
145
				/*
146
				 * There is a circular dependency between the logger and the registry, so
147
				 * we can not inject it. Thus the static call.
148
				 */
149
				\OC::$server->getLogger()->logException($e, [
150
					'message' => 'Could not register lazy crash reporter: ' . $e->getMessage(),
151
					'level' => ILogger::FATAL,
152
				]);
153
			}
154
		}
155
		$this->lazyReporters = [];
156
	}
157
}
158