Passed
Push — master ( 346770...2398d1 )
by Roeland
10:40 queued 35s
created

Resource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
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
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OC\Collaboration\Resources;
24
25
26
use OCP\Collaboration\Resources\ICollection;
27
use OCP\Collaboration\Resources\IManager;
28
use OCP\Collaboration\Resources\IResource;
29
use OCP\IDBConnection;
30
use OCP\IUser;
31
32
class Resource implements IResource {
33
34
	/** @var IManager */
35
	protected $manager;
36
37
	/** @var IDBConnection */
38
	protected $connection;
39
40
	/** @var string */
41
	protected $type;
42
43
	/** @var string */
44
	protected $id;
45
46
	/** @var IUser|null */
47
	protected $userForAccess;
48
49
	/** @var bool|null */
50
	protected $access;
51
52
	/** @var string|null */
53
	protected $name;
54
55
	/** @var string|null */
56
	protected $iconClass;
57
58
	/** @var string|null */
59
	protected $link;
60
61
	public function __construct(
62
		IManager $manager,
63
		IDBConnection $connection,
64
		string $type,
65
		string $id,
66
		?IUser $userForAccess = null,
67
		?bool $access = null
68
	) {
69
		$this->manager = $manager;
70
		$this->connection = $connection;
71
		$this->type = $type;
72
		$this->id = $id;
73
		$this->userForAccess = $userForAccess;
74
		$this->access = $access;
75
	}
76
77
	/**
78
	 * @return string
79
	 * @since 16.0.0
80
	 */
81
	public function getType(): string {
82
		return $this->type;
83
	}
84
85
	/**
86
	 * @return string
87
	 * @since 16.0.0
88
	 */
89
	public function getId(): string {
90
		return $this->id;
91
	}
92
93
	/**
94
	 * @return string
95
	 * @since 16.0.0
96
	 */
97
	public function getName(): string {
98
		if ($this->name === null) {
99
			$this->name = $this->manager->getName($this);
100
		}
101
102
		return $this->name;
103
	}
104
105
	/**
106
	 * @return string
107
	 * @since 16.0.0
108
	 */
109
	public function getIconClass(): string {
110
		if ($this->iconClass === null) {
111
			$this->iconClass = $this->manager->getIconClass($this);
112
		}
113
114
		return $this->iconClass;
115
	}
116
117
	public function getLink(): string {
118
		if ($this->link === null) {
119
			$this->link = $this->manager->getLink($this);
120
		}
121
122
		return $this->link;
123
	}
124
125
	/**
126
	 * Can a user/guest access the resource
127
	 *
128
	 * @param IUser|null $user
129
	 * @return bool
130
	 * @since 16.0.0
131
	 */
132
	public function canAccess(?IUser $user): bool {
133
		if ($user instanceof IUser) {
134
			return $this->canUserAccess($user);
135
		}
136
		return $this->canGuestAccess();
137
	}
138
139
	protected function canUserAccess(IUser $user): bool {
140
		if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
141
			return $this->access;
142
		}
143
144
		$access = $this->manager->canAccessResource($this, $user);
145
		if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
146
			$this->access = $access;
147
		}
148
		return $access;
149
	}
150
151
	protected function canGuestAccess(): bool {
152
		if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
153
			return $this->access;
154
		}
155
156
		$access = $this->manager->canAccessResource($this, null);
157
		if (!$this->userForAccess instanceof IUser) {
158
			$this->access = $access;
159
		}
160
		return $access;
161
	}
162
163
	/**
164
	 * @return ICollection[]
165
	 * @since 16.0.0
166
	 */
167
	public function getCollections(): array {
168
		$collections = [];
169
170
		$query = $this->connection->getQueryBuilder();
171
172
		$query->select('collection_id')
173
			->from('collres_resources')
174
			->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
175
			->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));
176
177
		$result = $query->execute();
178
		while ($row = $result->fetch()) {
179
			$collections[] = $this->manager->getCollection((int) $row['collection_id']);
180
		}
181
		$result->closeCursor();
182
183
		return $collections;
184
	}
185
}
186