Passed
Push — master ( b6209d...0acd4b )
by Joas
15:45 queued 14s
created

RequestId::getId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2022, Joas Schilling <[email protected]>
6
 *
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OC\AppFramework\Http;
25
26
use OCP\IRequestId;
27
use OCP\Security\ISecureRandom;
28
29
class RequestId implements IRequestId {
30
	protected ISecureRandom $secureRandom;
31
	protected string $requestId;
32
33
	public function __construct(string $uniqueId,
34
								ISecureRandom $secureRandom) {
35
		$this->requestId = $uniqueId;
36
		$this->secureRandom = $secureRandom;
37
	}
38
39
	/**
40
	 * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
41
	 * If `mod_unique_id` is installed this value will be taken.
42
	 * @return string
43
	 */
44
	public function getId(): string {
45
		if (empty($this->requestId)) {
46
			$validChars = ISecureRandom::CHAR_ALPHANUMERIC;
47
			$this->requestId = $this->secureRandom->generate(20, $validChars);
48
		}
49
50
		return $this->requestId;
51
	}
52
}
53