RequestId.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\AppFramework\Http;
  24. use OCP\IRequestId;
  25. use OCP\Security\ISecureRandom;
  26. class RequestId implements IRequestId {
  27. protected ISecureRandom $secureRandom;
  28. protected string $requestId;
  29. public function __construct(string $uniqueId,
  30. ISecureRandom $secureRandom) {
  31. $this->requestId = $uniqueId;
  32. $this->secureRandom = $secureRandom;
  33. }
  34. /**
  35. * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging
  36. * If `mod_unique_id` is installed this value will be taken.
  37. * @return string
  38. */
  39. public function getId(): string {
  40. if (empty($this->requestId)) {
  41. $validChars = ISecureRandom::CHAR_ALPHANUMERIC;
  42. $this->requestId = $this->secureRandom->generate(20, $validChars);
  43. }
  44. return $this->requestId;
  45. }
  46. }