Passed
Push — master ( 681437...be6475 )
by Morris
12:27
created

InitialStateService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeLazyStateCallbacks() 0 7 3
A provideLazyInitialState() 0 5 2
A __construct() 0 2 1
A provideInitialState() 0 11 5
A getInitialStates() 0 10 3
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC;
26
27
use Closure;
28
use OCP\IInitialStateService;
29
use OCP\ILogger;
30
31
class InitialStateService implements IInitialStateService {
32
33
	/** @var ILogger */
34
	private $logger;
35
36
	/** @var string[][] */
37
	private $states = [];
38
39
	/** @var Closure[][] */
40
	private $lazyStates = [];
41
42
	public function __construct(ILogger $logger) {
43
		$this->logger = $logger;
44
	}
45
46
	public function provideInitialState(string $appName, string $key, $data): void {
47
		// Scalars and JsonSerializable are fine
48
		if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
49
			if (!isset($this->states[$appName])) {
50
				$this->states[$appName] = [];
51
			}
52
			$this->states[$appName][$key] = json_encode($data);
53
			return;
54
		}
55
56
		$this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
57
	}
58
59
	public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
60
		if (!isset($this->lazyStates[$appName])) {
61
			$this->lazyStates[$appName] = [];
62
		}
63
		$this->lazyStates[$appName][$key] = $closure;
64
	}
65
66
	/**
67
	 * Invoke all callbacks to populate the `states` property
68
	 */
69
	private function invokeLazyStateCallbacks(): void {
70
		foreach ($this->lazyStates as $app => $lazyStates) {
71
			foreach ($lazyStates as $key => $lazyState) {
72
				$this->provideInitialState($app, $key, $lazyState());
73
			}
74
		}
75
		$this->lazyStates = [];
76
	}
77
78
	public function getInitialStates(): array {
79
		$this->invokeLazyStateCallbacks();
80
81
		$appStates = [];
82
		foreach ($this->states as $app => $states) {
83
			foreach ($states as $key => $value) {
84
				$appStates["$app-$key"] = $value;
85
			}
86
		}
87
		return $appStates;
88
	}
89
90
}
91