Passed
Push — master ( 052f09...8557c6 )
by Julius
15:44 queued 12s
created

Registry::hasReporters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016 Christoph Wurst <[email protected]>
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
namespace OC\Support\CrashReport;
28
29
use Exception;
30
use OCP\AppFramework\QueryException;
31
use OCP\IServerContainer;
32
use OCP\Support\CrashReport\ICollectBreadcrumbs;
33
use OCP\Support\CrashReport\IMessageReporter;
34
use OCP\Support\CrashReport\IRegistry;
35
use OCP\Support\CrashReport\IReporter;
36
use Psr\Log\LoggerInterface;
37
use Throwable;
38
use function array_shift;
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
	public function __construct(IServerContainer $serverContainer) {
52
		$this->serverContainer = $serverContainer;
53
	}
54
55
	/**
56
	 * Register a reporter instance
57
	 *
58
	 * @param IReporter $reporter
59
	 */
60
	public function register(IReporter $reporter): void {
61
		$this->reporters[] = $reporter;
62
	}
63
64
	public function registerLazy(string $class): void {
65
		$this->lazyReporters[] = $class;
66
	}
67
68
	/**
69
	 * Delegate breadcrumb collection to all registered reporters
70
	 *
71
	 * @param string $message
72
	 * @param string $category
73
	 * @param array $context
74
	 *
75
	 * @since 15.0.0
76
	 */
77
	public function delegateBreadcrumb(string $message, string $category, array $context = []): void {
78
		$this->loadLazyProviders();
79
80
		foreach ($this->reporters as $reporter) {
81
			if ($reporter instanceof ICollectBreadcrumbs) {
82
				$reporter->collect($message, $category, $context);
83
			}
84
		}
85
	}
86
87
	/**
88
	 * Delegate crash reporting to all registered reporters
89
	 *
90
	 * @param Exception|Throwable $exception
91
	 * @param array $context
92
	 */
93
	public function delegateReport($exception, array $context = []): void {
94
		$this->loadLazyProviders();
95
96
		foreach ($this->reporters as $reporter) {
97
			$reporter->report($exception, $context);
98
		}
99
	}
100
101
	/**
102
	 * Delegate a message to all reporters that implement IMessageReporter
103
	 *
104
	 * @param string $message
105
	 * @param array $context
106
	 *
107
	 * @return void
108
	 */
109
	public function delegateMessage(string $message, array $context = []): void {
110
		$this->loadLazyProviders();
111
112
		foreach ($this->reporters as $reporter) {
113
			if ($reporter instanceof IMessageReporter) {
114
				$reporter->reportMessage($message, $context);
115
			}
116
		}
117
	}
118
119
	private function loadLazyProviders(): void {
120
		while (($class = array_shift($this->lazyReporters)) !== null) {
121
			try {
122
				/** @var IReporter $reporter */
123
				$reporter = $this->serverContainer->query($class);
124
			} catch (QueryException $e) {
125
				/*
126
				 * There is a circular dependency between the logger and the registry, so
127
				 * we can not inject it. Thus the static call.
128
				 */
129
				\OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [
130
					'exception' => $e,
131
				]);
132
			}
133
			/**
134
			 * Try to register the loaded reporter. Theoretically it could be of a wrong
135
			 * type, so we might get a TypeError here that we should catch.
136
			 */
137
			try {
138
				$this->register($reporter);
139
			} catch (Throwable $e) {
140
				/*
141
				 * There is a circular dependency between the logger and the registry, so
142
				 * we can not inject it. Thus the static call.
143
				 */
144
				\OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [
145
					'exception' => $e,
146
				]);
147
			}
148
		}
149
	}
150
151
	public function hasReporters(): bool {
152
		return !empty($this->lazyReporters) || !empty($this->reporters);
153
	}
154
}
155