Passed
Push — master ( db1dbb...0b4489 )
by Jean-Christophe
02:24 queued 01:18
created

ContentSecurityManager::clearCsp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Ubiquity\security\csp;
3
4
/**
5
 * Manage Content Security Policies.
6
 * Ubiquity\security\csp$ContentSecurityManager
7
 * This class is part of Ubiquity
8
 *
9
 * @author jc
10
 * @version 1.0.0
11
 *
12
 */
13
class ContentSecurityManager {
14
15
	private static NonceGenerator $nonceGenerator;
16
17
	private static array $csp = [];
18
19
	public static function start(string $nonceGeneratorClass = null) {
20
		$nonceGeneratorClass ??= NonceGenerator::class;
21
		self::$nonceGenerator = new $nonceGeneratorClass();
22
	}
23
24
	public static function getNonce(string $name) {
25
		return self::$nonceGenerator->getNonce($name);
26
	}
27
28
	public static function isStarted(): bool {
29
		return isset(self::$nonceGenerator);
30
	}
31
32
	public static function addCsp(?bool $reportOnly = null): ContentSecurity {
33
		return self::$csp[] = new ContentSecurity($reportOnly);
34
	}
35
36
	public static function clearCsp() {
37
		self::$csp = [];
38
	}
39
40
	public static function addHeadersToResponse(?bool $reportOnly = null): void {
41
		foreach (self::$csp as $csp) {
42
			$csp->addHeaderToResponse($reportOnly);
43
		}
44
	}
45
}
46
47