RequestId.php 929 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\AppFramework\Http;
  8. use OCP\IRequestId;
  9. use OCP\Security\ISecureRandom;
  10. class RequestId implements IRequestId {
  11. protected ISecureRandom $secureRandom;
  12. protected string $requestId;
  13. public function __construct(string $uniqueId,
  14. ISecureRandom $secureRandom) {
  15. $this->requestId = $uniqueId;
  16. $this->secureRandom = $secureRandom;
  17. }
  18. /**
  19. * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
  20. * If `mod_unique_id` is installed this value will be taken.
  21. * @return string
  22. */
  23. public function getId(): string {
  24. if (empty($this->requestId)) {
  25. $validChars = ISecureRandom::CHAR_ALPHANUMERIC;
  26. $this->requestId = $this->secureRandom->generate(20, $validChars);
  27. }
  28. return $this->requestId;
  29. }
  30. }