Passed
Pull Request — master (#44)
by Christoph
02:16
created

SentryReporterAdapter::buildSentryContext()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 1
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @author Christoph Wurst <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Sentry\Reporter;
25
26
use Exception;
27
use OCP\IConfig;
28
use OCP\ILogger;
29
use OCP\IUserSession;
30
use OCP\Support\CrashReport\ICollectBreadcrumbs;
0 ignored issues
show
Bug introduced by
The type OCP\Support\CrashReport\ICollectBreadcrumbs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use OCP\Support\CrashReport\IReporter;
32
use Raven_Client;
33
use Throwable;
34
35
class SentryReporterAdapter implements IReporter, ICollectBreadcrumbs {
36
37
	/** @var IUserSession */
38
	private $userSession;
39
40
	/** @var Raven_Client */
41
	private $client;
42
43
	/** @var array mapping of log levels */
44
	private $levels = [
45
		ILogger::DEBUG => 'debug',
46
		ILogger::INFO => 'info',
47
		ILogger::WARN => 'warning',
48
		ILogger::ERROR => 'error',
49
		ILogger::FATAL => 'fatal',
50
	];
51
52
	/** @var int */
53
	private $minimumLogLevel;
54
55
	/**
56
	 * @param Raven_Client $client
57
	 */
58
	public function __construct(Raven_Client $client, IUserSession $userSession, IConfig $config) {
59
		$this->client = $client;
60
		$this->userSession = $userSession;
61
		$this->minimumLogLevel = (int)$config->getSystemValue('sentry.minimum.log.level', ILogger::WARN);
62
	}
63
64
	private function buildSentryContext(array $context) {
65
		$sentryContext = [];
66
		$sentryContext['tags'] = [];
67
68
		if (isset($context['level'])) {
69
			$sentryContext['level'] = $this->levels[$context['level']];
70
		}
71
		if (isset($context['app'])) {
72
			$sentryContext['tags']['app'] = $context['app'];
73
		}
74
75
		$user = $this->userSession->getUser();
76
		if (!is_null($user)) {
77
			$sentryContext['user'] = [
78
				'id' => $user->getUID(),
79
			];
80
		}
81
		return $sentryContext;
82
	}
83
84
	public function collect(string $message, string $category, array $context = []) {
85
		$sentryContext = $this->buildSentryContext($context);
86
87
		$sentryContext['message'] = $message;
88
		$sentryContext['category'] = $category;
89
90
		$this->client->breadcrumbs->record($sentryContext);
91
	}
92
93
	/**
94
	 * Report an (unhandled) exception to Sentry
95
	 *
96
	 * @param Exception|Throwable $exception
97
	 * @param array $context
98
	 */
99
	public function report($exception, array $context = []) {
100
		if (isset($context['level'])
101
			&& $context['level'] < $this->minimumLogLevel) {
102
			// TODO: report as breadcrumb instead?
103
			return;
104
		}
105
106
		$sentryContext = $this->buildSentryContext($context);
107
108
		$this->client->captureException($exception, $sentryContext);
109
	}
110
111
}
112