Passed
Push — master ( 0b4489...06c2c4 )
by Jean-Christophe
19:16 queued 18:03
created

NonceGenerator::getNonce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Ubiquity\security\csp;
3
4
use Ubiquity\utils\http\URequest;
5
6
class NonceGenerator {
7
8
	private array $nonces = [];
9
10
	private ?callable $onNonce;
11
12
	public function __construct(?callable $onNonce) {
13
		$this->onNonce = $onNonce;
14
	}
15
16
	protected function _generateNonce(string $name, ?int $value = null): string {
17
		$bytes = \random_bytes((int) ($value ?? 32));
18
		$nonce = \base64_encode($bytes);
19
		if (isset($this->onNonce) && ! URequest::isAjax()) {
20
			$this->{onNonce}($name, $nonce);
0 ignored issues
show
Bug introduced by
The constant Ubiquity\security\csp\onNonce was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
		}
22
		return $nonce;
23
	}
24
25
	public function getNonce(string $name) {
26
		return $this->nonces[$name] ??= self::_generateNonce($name, $value);
0 ignored issues
show
Bug Best Practice introduced by
The method Ubiquity\security\csp\No...rator::_generateNonce() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
		return $this->nonces[$name] ??= self::/** @scrutinizer ignore-call */ _generateNonce($name, $value);
Loading history...
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
27
	}
28
}
29
30