ScopeContext.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Helper;
  8. use OCP\WorkflowEngine\IManager;
  9. class ScopeContext {
  10. /** @var int */
  11. private $scope;
  12. /** @var string */
  13. private $scopeId;
  14. /** @var string */
  15. private $hash;
  16. public function __construct(int $scope, ?string $scopeId = null) {
  17. $this->scope = $this->evaluateScope($scope);
  18. $this->scopeId = $this->evaluateScopeId($scopeId);
  19. }
  20. private function evaluateScope(int $scope): int {
  21. if (in_array($scope, [IManager::SCOPE_ADMIN, IManager::SCOPE_USER], true)) {
  22. return $scope;
  23. }
  24. throw new \InvalidArgumentException('Invalid scope');
  25. }
  26. private function evaluateScopeId(?string $scopeId = null): string {
  27. if ($this->scope === IManager::SCOPE_USER
  28. && trim((string)$scopeId) === '') {
  29. throw new \InvalidArgumentException('user scope requires a user id');
  30. }
  31. return trim((string)$scopeId);
  32. }
  33. /**
  34. * @return int
  35. */
  36. public function getScope(): int {
  37. return $this->scope;
  38. }
  39. /**
  40. * @return string
  41. */
  42. public function getScopeId(): string {
  43. return $this->scopeId;
  44. }
  45. public function getHash(): string {
  46. if ($this->hash === null) {
  47. $this->hash = \hash('sha256', $this->getScope() . '::' . $this->getScopeId());
  48. }
  49. return $this->hash;
  50. }
  51. }