Completed
Push — master ( 64a4da...21cb6b )
by Thomas
07:39
created

RootCollection::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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