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 */
24namespace OCA\DAV\Comments;
25
26use OCP\Comments\ICommentsManager;
27use OCP\ILogger;
28use OCP\IUserManager;
29use OCP\IUserSession;
30use Sabre\DAV\Exception\MethodNotAllowed;
31use Sabre\DAV\Exception\NotFound;
32
33/**
34 * Class EntityTypeCollection
35 *
36 * This is collection on the type of things a user can leave comments on, for
37 * example: 'files'.
38 *
39 * Its children are instances of EntityCollection (representing a specific
40 * object, for example the file by id).
41 *
42 * @package OCA\DAV\Comments
43 */
44class EntityTypeCollection extends RootCollection {
45
46	/** @var ILogger */
47	protected $logger;
48
49	/** @var IUserManager */
50	protected $userManager;
51
52	/** @var \Closure */
53	protected $childExistsFunction;
54
55	/**
56	 * @param string $name
57	 * @param ICommentsManager $commentsManager
58	 * @param IUserManager $userManager
59	 * @param IUserSession $userSession
60	 * @param ILogger $logger
61	 * @param \Closure $childExistsFunction
62	 */
63	public function __construct(
64		$name,
65		ICommentsManager $commentsManager,
66		IUserManager $userManager,
67		IUserSession $userSession,
68		ILogger $logger,
69		\Closure $childExistsFunction
70	) {
71		$name = trim($name);
72		if (empty($name) || !is_string($name)) {
73			throw new \InvalidArgumentException('"name" parameter must be non-empty string');
74		}
75		$this->name = $name;
76		$this->commentsManager = $commentsManager;
77		$this->logger = $logger;
78		$this->userManager = $userManager;
79		$this->userSession = $userSession;
80		$this->childExistsFunction = $childExistsFunction;
81	}
82
83	/**
84	 * Returns a specific child node, referenced by its name
85	 *
86	 * This method must throw Sabre\DAV\Exception\NotFound if the node does not
87	 * exist.
88	 *
89	 * @param string $name
90	 * @return \Sabre\DAV\INode
91	 * @throws NotFound
92	 */
93	public function getChild($name) {
94		if (!$this->childExists($name)) {
95			throw new NotFound('Entity does not exist or is not available');
96		}
97		return new EntityCollection(
98			$name,
99			$this->name,
100			$this->commentsManager,
101			$this->userManager,
102			$this->userSession,
103			$this->logger
104		);
105	}
106
107	/**
108	 * Returns an array with all the child nodes
109	 *
110	 * @return \Sabre\DAV\INode[]
111	 * @throws MethodNotAllowed
112	 */
113	public function getChildren() {
114		throw new MethodNotAllowed('No permission to list folder contents');
115	}
116
117	/**
118	 * Checks if a child-node with the specified name exists
119	 *
120	 * @param string $name
121	 * @return bool
122	 */
123	public function childExists($name) {
124		return call_user_func($this->childExistsFunction, $name);
125	}
126}
127