Completed
Push — master ( a963a2...128b96 )
by Roeland
13s queued 11s
created

Application::setInitialState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
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\AppInfo;
25
26
use OC;
27
use OCA\Sentry\Reporter\SentryReporterBreadcrumbAdapter;
0 ignored issues
show
Bug introduced by
The type OCA\Sentry\Reporter\Sent...porterBreadcrumbAdapter 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...
28
use OCA\Sentry\Reporter\SentryReporterAdapter;
29
use OCP\AppFramework\App;
30
use OCP\AppFramework\Http\ContentSecurityPolicy;
31
use OCP\IConfig;
32
use OCP\IInitialStateService;
0 ignored issues
show
Bug introduced by
The type OCP\IInitialStateService 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...
33
use OCP\Security\IContentSecurityPolicyManager;
34
use OCP\Support\CrashReport\IRegistry;
35
use Raven_Client;
36
use Raven_ErrorHandler;
37
38
class Application extends App {
39
40
	/**
41
	 * @param array $urlParams
42
	 */
43
	public function __construct($urlParams = []) {
44
		parent::__construct('sentry', $urlParams);
45
46
		$container = $this->getContainer();
47
48
		/* @var $config IConfig */
49
		$config = $container->query(IConfig::class);
50
		$dsn = $config->getSystemValue('sentry.dsn', null);
51
		if (!is_null($dsn)) {
52
			$this->registerClient($dsn);
53
		}
54
		$publicDsn = $config->getSystemValue('sentry.public-dsn', null);
55
		$this->setInitialState($publicDsn);
56
		if (!is_null($publicDsn)) {
57
			$this->addCsp($publicDsn);
58
		}
59
	}
60
61
	/**
62
	 * @param string $dsn
63
	 */
64
	private function registerClient($dsn) {
65
		$container = $this->getContainer();
66
		/* @var $config IConfig */
67
		$config = $container->query(IConfig::class);
68
69
		$client = new Raven_Client($dsn);
70
		$client->setRelease($config->getSystemValue('version', '0.0.0'));
71
		$container->registerService(Raven_Client::class, function () use ($client) {
72
			return $client;
73
		});
74
75
		/* @var $registry IRegistry */
76
		$registry = $container->query(IRegistry::class);
77
		$reporter = $container->query(SentryReporterAdapter::class);
78
		$registry->register($reporter);
79
80
		$this->registerErrorHandlers($client);
81
	}
82
83
	private function registerErrorHandlers(Raven_Client $client) {
84
		$errorHandler = new Raven_ErrorHandler($client);
85
		$errorHandler->registerExceptionHandler();
86
		$errorHandler->registerErrorHandler();
87
		$errorHandler->registerShutdownFunction();
88
	}
89
90
	private function addCsp($publicDsn) {
91
		$parsedUrl = parse_url($publicDsn);
92
		if (!isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) {
93
			// Misconfigured setup -> ignore
94
			return;
95
		}
96
97
		$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
98
		$csp = new ContentSecurityPolicy();
99
		$csp->addAllowedConnectDomain($domain);
100
		$cspManager = OC::$server->query(IContentSecurityPolicyManager::class);
101
		$cspManager->addDefaultPolicy($csp);
102
	}
103
104
	private function setInitialState(?string $dsn) {
105
		$container = $this->getContainer();
106
107
		/** @var IInitialStateService $stateService */
108
		$stateService = $container->query(IInitialStateService::class);
109
110
		$stateService->provideLazyInitialState('sentry', 'dsn', function () use ($dsn) {
111
			return [
112
				'dsn' => $dsn
113
			];
114
		});
115
	}
116
117
}
118