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

ResourceProvider::__construct()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Files\Collaboration\Resources;
23
24
25
use OCP\Collaboration\Resources\IProvider;
26
use OCP\Collaboration\Resources\IResource;
27
use OCP\Collaboration\Resources\ResourceException;
28
use OCP\Files\IRootFolder;
29
use OCP\Files\Node;
30
use OCP\IURLGenerator;
31
use OCP\IUser;
32
33
class ResourceProvider implements IProvider {
34
35
	const RESOURCE_TYPE = 'files';
36
37
	/** @var IRootFolder */
38
	protected $rootFolder;
39
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
43
	/** @var array */
44
	protected $nodes = [];
45
46
	public function __construct(IRootFolder $rootFolder, IURLGenerator $urlGenerator) {
47
		$this->rootFolder = $rootFolder;
48
		$this->urlGenerator = $urlGenerator;
49
	}
50
51
	private function getNode(IResource $resource): ?Node {
52
		if (isset($this->nodes[(int) $resource->getId()])) {
53
			return $this->nodes[(int) $resource->getId()];
54
		}
55
		$nodes = $this->rootFolder->getById((int) $resource->getId());
56
		if (!empty($nodes)) {
57
			$this->nodes[(int) $resource->getId()] = array_shift($nodes);
58
			return $this->nodes[(int) $resource->getId()];
59
		}
60
		return null;
61
	}
62
63
	/**
64
	 * Get the display name of a resource
65
	 *
66
	 * @param IResource $resource
67
	 * @return string
68
	 * @since 15.0.0
69
	 */
70
	public function getName(IResource $resource): string {
71
		if (isset($this->nodes[(int) $resource->getId()])) {
72
			return $this->nodes[(int) $resource->getId()]->getPath();
73
		}
74
		$node = $this->getNode($resource);
75
		if ($node) {
76
			return $node->getName();
77
		}
78
		return '';
79
	}
80
81
	/**
82
	 * Can a user/guest access the collection
83
	 *
84
	 * @param IResource $resource
85
	 * @param IUser $user
86
	 * @return bool
87
	 * @since 15.0.0
88
	 */
89
	public function canAccessResource(IResource $resource, IUser $user = null): bool {
90
		if (!$user instanceof IUser) {
91
			return false;
92
		}
93
94
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
95
		$nodes = $userFolder->getById((int) $resource->getId());
96
97
		if (!empty($nodes)) {
98
			$this->nodes[(int) $resource->getId()] = array_shift($nodes);
99
			return true;
100
		}
101
102
		return false;
103
	}
104
105
	/**
106
	 * Get the icon class of a resource
107
	 *
108
	 * @param IResource $resource
109
	 * @return string
110
	 * @since 15.0.0
111
	 */
112
	public function getIconClass(IResource $resource): string {
113
		$node = $this->getNode($resource);
114
		if ($node && $node->getMimetype() === 'httpd/unix-directory') {
115
			return 'icon-files-dark';
116
		}
117
		return 'icon-filetype-file';
118
	}
119
120
	/**
121
	 * Get the resource type of the provider
122
	 *
123
	 * @return string
124
	 * @since 15.0.0
125
	 */
126
	public function getType(): string {
127
		return self::RESOURCE_TYPE;
128
	}
129
130
	/**
131
	 * Get the link to a resource
132
	 *
133
	 * @param IResource $resource
134
	 * @return string
135
	 * @since 15.0.0
136
	 */
137
	public function getLink(IResource $resource): string {
138
		return $this->urlGenerator->linkToRoute('files.viewcontroller.showFile', ['fileid' => $resource->getId()]);
139
	}
140
}
141