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

EntityCollection::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 2
b 0
f 0
nc 3
nop 6
dl 0
loc 21
rs 9.0534
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\ICommentsManager;
25
use OCP\Comments\NotFoundException;
26
use OCP\ILogger;
27
use OCP\IUserManager;
28
use OCP\IUserSession;
29
use Sabre\DAV\Exception\NotFound;
30
use Sabre\DAV\IProperties;
31
use Sabre\DAV\PropPatch;
32
33
/**
34
 * Class EntityCollection
35
 *
36
 * this represents a specific holder of comments, identified by an entity type
37
 * (class member $name) and an entity id (class member $id).
38
 *
39
 * @package OCA\DAV\Comments
40
 */
41
class EntityCollection extends RootCollection implements IProperties {
42
	const PROPERTY_NAME_READ_MARKER  = '{http://owncloud.org/ns}readMarker';
43
44
	/** @var  string */
45
	protected $id;
46
47
	/** @var  ILogger */
48
	protected $logger;
49
50
	/**
51
	 * @param string $id
52
	 * @param string $name
53
	 * @param ICommentsManager $commentsManager
54
	 * @param IUserManager $userManager
55
	 * @param IUserSession $userSession
56
	 * @param ILogger $logger
57
	 */
58
	public function __construct(
59
		$id,
60
		$name,
61
		ICommentsManager $commentsManager,
62
		IUserManager $userManager,
63
		IUserSession $userSession,
64
		ILogger $logger
65
	) {
66
		foreach(['id', 'name'] as $property) {
67
			$$property = trim($$property);
68
			if(empty($$property) || !is_string($$property)) {
69
				throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string');
70
			}
71
		}
72
		$this->id = $id;
73
		$this->name = $name;
74
		$this->commentsManager = $commentsManager;
75
		$this->logger = $logger;
76
		$this->userManager = $userManager;
77
		$this->userSession = $userSession;
78
	}
79
80
	/**
81
	 * returns the ID of this entity
82
	 *
83
	 * @return string
84
	 */
85
	public function getId() {
86
		return $this->id;
87
	}
88
89
	/**
90
	 * Returns a specific child node, referenced by its name
91
	 *
92
	 * This method must throw Sabre\DAV\Exception\NotFound if the node does not
93
	 * exist.
94
	 *
95
	 * @param string $name
96
	 * @return \Sabre\DAV\INode
97
	 * @throws NotFound
98
	 */
99 View Code Duplication
	function getChild($name) {
100
		try {
101
			$comment = $this->commentsManager->get($name);
102
			return new CommentNode(
103
				$this->commentsManager,
104
				$comment,
105
				$this->userManager,
106
				$this->userSession,
107
				$this->logger
108
			);
109
		} catch (NotFoundException $e) {
110
			throw new NotFound();
111
		}
112
	}
113
114
	/**
115
	 * Returns an array with all the child nodes
116
	 *
117
	 * @return \Sabre\DAV\INode[]
118
	 */
119
	function getChildren() {
120
		return $this->findChildren();
121
	}
122
123
	/**
124
	 * Returns an array of comment nodes. Result can be influenced by offset,
125
	 * limit and date time parameters.
126
	 *
127
	 * @param int $limit
128
	 * @param int $offset
129
	 * @param \DateTime|null $datetime
130
	 * @return CommentNode[]
131
	 */
132
	function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) {
133
		$comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
134
		$result = [];
135
		foreach($comments as $comment) {
136
			$result[] = new CommentNode(
137
				$this->commentsManager,
138
				$comment,
139
				$this->userManager,
140
				$this->userSession,
141
				$this->logger
142
			);
143
		}
144
		return $result;
145
	}
146
147
	/**
148
	 * Checks if a child-node with the specified name exists
149
	 *
150
	 * @param string $name
151
	 * @return bool
152
	 */
153
	function childExists($name) {
154
		try {
155
			$this->commentsManager->get($name);
156
			return true;
157
		} catch (NotFoundException $e) {
158
			return false;
159
		}
160
	}
161
162
	/**
163
	 * Sets the read marker to the specified date for the logged in user
164
	 *
165
	 * @param \DateTime $value
166
	 * @return bool
167
	 */
168
	public function setReadMarker($value) {
169
		$dateTime = new \DateTime($value);
170
		$user = $this->userSession->getUser();
171
		$this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userSession->getUser() on line 170 can be null; however, OCP\Comments\ICommentsManager::setReadMark() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
172
		return true;
173
	}
174
175
	/**
176
	 * @inheritdoc
177
	 */
178
	function propPatch(PropPatch $propPatch) {
179
		$propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
180
	}
181
182
	/**
183
	 * @inheritdoc
184
	 */
185
	function getProperties($properties) {
186
		$marker = null;
187
		$user = $this->userSession->getUser();
188
		if(!is_null($user)) {
189
			$marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
190
		}
191
		return [self::PROPERTY_NAME_READ_MARKER => $marker];
192
	}
193
}
194
195