Completed
Pull Request — master (#22620)
by Joas
09:42
created

RootCollection::getChild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\DAV\Comments;
23
24
use OCP\Comments\CommentsEntityEvent;
25
use OCP\Comments\ICommentsManager;
26
use OCP\ILogger;
27
use OCP\IUserManager;
28
use OCP\IUserSession;
29
use Sabre\DAV\Exception\NotAuthenticated;
30
use Sabre\DAV\Exception\Forbidden;
31
use Sabre\DAV\Exception\NotFound;
32
use Sabre\DAV\ICollection;
33
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
34
35
class RootCollection implements ICollection {
36
37
	/** @var EntityTypeCollection[]|null */
38
	private $entityTypeCollections;
39
40
	/** @var ICommentsManager */
41
	protected $commentsManager;
42
43
	/** @var string */
44
	protected $name = 'comments';
45
46
	/** @var ILogger */
47
	protected $logger;
48
49
	/** @var IUserManager */
50
	protected $userManager;
51
52
	/** @var IUserSession */
53
	protected $userSession;
54
55
	/** @var EventDispatcherInterface */
56
	protected $dispatcher;
57
58
	/**
59
	 * @param ICommentsManager $commentsManager
60
	 * @param IUserManager $userManager
61
	 * @param IUserSession $userSession
62
	 * @param EventDispatcherInterface $dispatcher
63
	 * @param ILogger $logger
64
	 */
65
	public function __construct(
66
		ICommentsManager $commentsManager,
67
		IUserManager $userManager,
68
		IUserSession $userSession,
69
		EventDispatcherInterface $dispatcher,
70
		ILogger $logger)
71
	{
72
		$this->commentsManager = $commentsManager;
73
		$this->logger = $logger;
74
		$this->userManager = $userManager;
75
		$this->userSession = $userSession;
76
		$this->dispatcher = $dispatcher;
77
	}
78
79
	/**
80
	 * initializes the collection. At this point of time, we need the logged in
81
	 * user. Since it is not the case when the instance is created, we cannot
82
	 * have this in the constructor.
83
	 *
84
	 * @throws NotAuthenticated
85
	 */
86
	protected function initCollections() {
87
		if($this->entityTypeCollections !== null) {
88
			return;
89
		}
90
		$user = $this->userSession->getUser();
91
		if(is_null($user)) {
92
			throw new NotAuthenticated();
93
		}
94
95
		$event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY);
96
		$this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event);
97
98
		$this->entityTypeCollections = [];
99
		foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
100
			$this->entityTypeCollections[$entity] = new EntityTypeCollection(
101
				$entity,
102
				$this->commentsManager,
103
				$this->userManager,
104
				$this->userSession,
105
				$this->logger,
106
				$entityExistsFunction
107
			);
108
		}
109
	}
110
111
	/**
112
	 * Creates a new file in the directory
113
	 *
114
	 * @param string $name Name of the file
115
	 * @param resource|string $data Initial payload
116
	 * @return null|string
117
	 * @throws Forbidden
118
	 */
119
	function createFile($name, $data = null) {
120
		throw new Forbidden('Cannot create comments by id');
121
	}
122
123
	/**
124
	 * Creates a new subdirectory
125
	 *
126
	 * @param string $name
127
	 * @throws Forbidden
128
	 */
129
	function createDirectory($name) {
130
		throw new Forbidden('Permission denied to create collections');
131
	}
132
133
	/**
134
	 * Returns a specific child node, referenced by its name
135
	 *
136
	 * This method must throw Sabre\DAV\Exception\NotFound if the node does not
137
	 * exist.
138
	 *
139
	 * @param string $name
140
	 * @return \Sabre\DAV\INode
141
	 * @throws NotFound
142
	 */
143
	function getChild($name) {
144
		$this->initCollections();
145
		if(isset($this->entityTypeCollections[$name])) {
146
			return $this->entityTypeCollections[$name];
147
		}
148
		throw new NotFound('Entity type "' . $name . '" not found."');
149
	}
150
151
	/**
152
	 * Returns an array with all the child nodes
153
	 *
154
	 * @return \Sabre\DAV\INode[]
155
	 */
156
	function getChildren() {
157
		$this->initCollections();
158
		return $this->entityTypeCollections;
159
	}
160
161
	/**
162
	 * Checks if a child-node with the specified name exists
163
	 *
164
	 * @param string $name
165
	 * @return bool
166
	 */
167
	function childExists($name) {
168
		$this->initCollections();
169
		return isset($this->entityTypeCollections[$name]);
170
	}
171
172
	/**
173
	 * Deleted the current node
174
	 *
175
	 * @throws Forbidden
176
	 */
177
	function delete() {
178
		throw new Forbidden('Permission denied to delete this collection');
179
	}
180
181
	/**
182
	 * Returns the name of the node.
183
	 *
184
	 * This is used to generate the url.
185
	 *
186
	 * @return string
187
	 */
188
	function getName() {
189
		return $this->name;
190
	}
191
192
	/**
193
	 * Renames the node
194
	 *
195
	 * @param string $name The new name
196
	 * @throws Forbidden
197
	 */
198
	function setName($name) {
199
		throw new Forbidden('Permission denied to rename this collection');
200
	}
201
202
	/**
203
	 * Returns the last modification time, as a unix timestamp
204
	 *
205
	 * @return int
206
	 */
207
	function getLastModified() {
208
		return null;
209
	}
210
}
211