Completed
Push — master ( 3e5138...d2cc48 )
by Morris
22:18
created

TrashFolder::getFileId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2018, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Files_Trashbin\Sabre;
25
26
use OCP\Files\FileInfo;
27
use Sabre\DAV\Exception\Forbidden;
28
use Sabre\DAV\Exception\NotFound;
29
use Sabre\DAV\ICollection;
30
31
class TrashFolder implements ICollection, ITrash {
32
	/** @var string */
33
	private $userId;
34
35
	/** @var FileInfo */
36
	private $data;
37
38
	public function __construct(string $root, string $userId, FileInfo $data) {
39
		$this->userId = $userId;
40
		$this->data = $data;
41
	}
42
43
	public function createFile($name, $data = null) {
44
		throw new Forbidden();
45
	}
46
47
	public function createDirectory($name) {
48
		throw new Forbidden();
49
	}
50
51
	public function getChild($name): ITrash {
52
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53
54
		foreach ($entries as $entry) {
55
			if ($entry->getName() === $name) {
56 View Code Duplication
				if ($entry->getType() === FileInfo::TYPE_FOLDER) {
57
					return new TrashFolderFolder($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
58
				}
59
				return new TrashFolderFile($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
60
			}
61
		}
62
63
		throw new NotFound();
64
	}
65
66
	public function getChildren(): array {
67
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
68
69
		$children = array_map(function (FileInfo $entry) {
70 View Code Duplication
			if ($entry->getType() === FileInfo::TYPE_FOLDER) {
71
				return new TrashFolderFolder($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
			}
73
			return new TrashFolderFile($this->getName(), $this->userId, $entry, $this->getOriginalLocation());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
74
		}, $entries);
75
76
		return $children;
77
	}
78
79
	public function childExists($name): bool {
80
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
81
82
		foreach ($entries as $entry) {
83
			if ($entry->getName() === $name) {
84
				return true;
85
			}
86
		}
87
88
		return false;
89
	}
90
91
	public function delete() {
92
		\OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
93
	}
94
95
	public function getName(): string {
96
		return $this->data->getName() . '.d' . $this->getLastModified();
97
	}
98
99
	public function setName($name) {
100
		throw new Forbidden();
101
	}
102
103
	public function getLastModified(): int {
104
		return $this->data->getMtime();
105
	}
106
107
	public function restore(): bool {
108
		return \OCA\Files_Trashbin\Trashbin::restore($this->getName(), $this->data->getName(), $this->getLastModified());
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
109
	}
110
111
	public function getFilename(): string {
112
		return $this->data->getName();
113
	}
114
115
	public function getOriginalLocation(): string {
116
		return $this->data['extraData'];
117
	}
118
119
	public function getDeletionTime(): int {
120
		return $this->getLastModified();
121
	}
122
123
	public function getSize(): int {
124
		return $this->data->getSize();
125
	}
126
127
	public function getFileId(): int {
128
		return $this->data->getId();
129
	}
130
}
131