EntityTypeCollection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  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 OCA\DAV\Comments;
  25. use OCP\Comments\ICommentsManager;
  26. use OCP\IUserManager;
  27. use OCP\IUserSession;
  28. use Psr\Log\LoggerInterface;
  29. use Sabre\DAV\Exception\MethodNotAllowed;
  30. use Sabre\DAV\Exception\NotFound;
  31. /**
  32. * Class EntityTypeCollection
  33. *
  34. * This is collection on the type of things a user can leave comments on, for
  35. * example: 'files'.
  36. *
  37. * Its children are instances of EntityCollection (representing a specific
  38. * object, for example the file by id).
  39. *
  40. * @package OCA\DAV\Comments
  41. */
  42. class EntityTypeCollection extends RootCollection {
  43. protected LoggerInterface $logger;
  44. protected IUserManager $userManager;
  45. /** @var \Closure */
  46. protected $childExistsFunction;
  47. public function __construct(
  48. string $name,
  49. ICommentsManager $commentsManager,
  50. IUserManager $userManager,
  51. IUserSession $userSession,
  52. LoggerInterface $logger,
  53. \Closure $childExistsFunction
  54. ) {
  55. $name = trim($name);
  56. if (empty($name)) {
  57. throw new \InvalidArgumentException('"name" parameter must be non-empty string');
  58. }
  59. $this->name = $name;
  60. $this->commentsManager = $commentsManager;
  61. $this->logger = $logger;
  62. $this->userManager = $userManager;
  63. $this->userSession = $userSession;
  64. $this->childExistsFunction = $childExistsFunction;
  65. }
  66. /**
  67. * Returns a specific child node, referenced by its name
  68. *
  69. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  70. * exist.
  71. *
  72. * @param string $name
  73. * @return \Sabre\DAV\INode
  74. * @throws NotFound
  75. */
  76. public function getChild($name) {
  77. if (!$this->childExists($name)) {
  78. throw new NotFound('Entity does not exist or is not available');
  79. }
  80. return new EntityCollection(
  81. $name,
  82. $this->name,
  83. $this->commentsManager,
  84. $this->userManager,
  85. $this->userSession,
  86. $this->logger
  87. );
  88. }
  89. /**
  90. * Returns an array with all the child nodes
  91. *
  92. * @return \Sabre\DAV\INode[]
  93. * @throws MethodNotAllowed
  94. */
  95. public function getChildren() {
  96. throw new MethodNotAllowed('No permission to list folder contents');
  97. }
  98. /**
  99. * Checks if a child-node with the specified name exists
  100. *
  101. * @param string $name
  102. * @return bool
  103. */
  104. public function childExists($name) {
  105. return call_user_func($this->childExistsFunction, $name);
  106. }
  107. }