Passed
Push — master ( e324c9...3ac048 )
by Roeland
19:27 queued 12s
created

Application::injectJavascript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 17
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
7
 *
8
 * @author Alexey Pyltsyn <[email protected]>
9
 * @author Janis Köhr <[email protected]>
10
 * @author John Molakvoæ (skjnldsv) <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCA\Accessibility\AppInfo;
31
32
use OCA\Accessibility\Service\JSDataService;
33
use OCP\AppFramework\App;
34
use OCP\AppFramework\Bootstrap\IBootContext;
35
use OCP\AppFramework\Bootstrap\IBootstrap;
36
use OCP\AppFramework\Bootstrap\IRegistrationContext;
37
use OCP\AppFramework\IAppContainer;
38
use OCP\IConfig;
39
use OCP\IInitialStateService;
40
use OCP\IURLGenerator;
41
use OCP\IUserSession;
42
use function count;
43
use function implode;
44
use function md5;
45
46
class Application extends App implements IBootstrap {
47
48
	/** @var string */
49
	public const APP_ID = 'accessibility';
50
51
	public function __construct() {
52
		parent::__construct(self::APP_ID);
53
	}
54
55
	public function register(IRegistrationContext $context): void {
56
	}
57
58
	public function boot(IBootContext $context): void {
59
		$this->injectCss(
60
			$context->getAppContainer()->query(IUserSession::class),
61
			$context->getAppContainer()->query(IConfig::class),
62
			$context->getAppContainer()->query(IURLGenerator::class)
63
		);
64
65
		$this->registerInitialState($context->getAppContainer());
66
	}
67
68
	private function injectCss(IUserSession $userSession,
69
							   IConfig $config,
70
							   IURLGenerator $urlGenerator) {
71
		// Inject the fake css on all pages if enabled and user is logged
72
		$loggedUser = $userSession->getUser();
73
		if ($loggedUser !== null) {
74
			$userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID);
75
			// we want to check if any theme or font is enabled.
76
			if (count($userValues) > 0) {
77
				$hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues)));
78
				$linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]);
79
				\OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
80
			}
81
		}
82
	}
83
84
	private function registerInitialState(IAppContainer $container) {
85
		/** @var IInitialStateService $initialState */
86
		$initialState = $container->query(IInitialStateService::class);
87
88
		$initialState->provideLazyInitialState(self::APP_ID, 'data', function () use ($container) {
89
			/** @var JSDataService $data */
90
			$data = $container->query(JSDataService::class);
91
			return $data;
92
		});
93
	}
94
}
95