1
0

LockContext.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\Files\Lock;
  25. use OCP\Files\Node;
  26. /**
  27. * Structure to identify a specific lock context to request or
  28. * describe a lock with the affected node and ownership information
  29. *
  30. * This is used to match a lock/unlock request or file operation to existing locks
  31. *
  32. * @since 24.0.0
  33. */
  34. final class LockContext {
  35. private Node $node;
  36. private int $type;
  37. private string $owner;
  38. /**
  39. * @param Node $node Node that is owned by the lock
  40. * @param int $type Type of the lock owner
  41. * @param string $owner Unique identifier for the lock owner based on the type
  42. * @since 24.0.0
  43. */
  44. public function __construct(
  45. Node $node,
  46. int $type,
  47. string $owner
  48. ) {
  49. $this->node = $node;
  50. $this->type = $type;
  51. $this->owner = $owner;
  52. }
  53. /**
  54. * @since 24.0.0
  55. */
  56. public function getNode(): Node {
  57. return $this->node;
  58. }
  59. /**
  60. * @return int
  61. * @since 24.0.0
  62. */
  63. public function getType(): int {
  64. return $this->type;
  65. }
  66. /**
  67. * @return string user id / app id / lock token depending on the type
  68. * @since 24.0.0
  69. */
  70. public function getOwner(): string {
  71. return $this->owner;
  72. }
  73. /**
  74. * @since 24.0.0
  75. */
  76. public function __toString(): string {
  77. $typeString = 'unknown';
  78. if ($this->type === ILock::TYPE_USER) {
  79. $typeString = 'ILock::TYPE_USER';
  80. }
  81. if ($this->type === ILock::TYPE_APP) {
  82. $typeString = 'ILock::TYPE_APP';
  83. }
  84. if ($this->type === ILock::TYPE_TOKEN) {
  85. $typeString = 'ILock::TYPE_TOKEN';
  86. }
  87. return "$typeString $this->owner " . $this->getNode()->getId();
  88. }
  89. }