Completed
Push — master ( 412925...57ea46 )
by Morris
18:29 queued 10s
created

TrashFolderFolder::getChild()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 3
loc 14
rs 9.2
c 0
b 0
f 0
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 TrashFolderFolder implements ICollection, ITrash {
32
33
	/** @var string */
34
	private $root;
35
36
	/** @var string */
37
	private $userId;
38
39
	/** @var FileInfo */
40
	private $data;
41
42
	/** @var string */
43
	private $location;
44
45 View Code Duplication
	public function __construct(string $root,
46
								string $userId,
47
								FileInfo $data,
48
								string $location) {
49
		$this->root = $root;
50
		$this->userId = $userId;
51
		$this->data = $data;
52
		$this->location = $location;
53
	}
54
55
	public function createFile($name, $data = null) {
56
		throw new Forbidden();
57
	}
58
59
	public function createDirectory($name) {
60
		throw new Forbidden();
61
	}
62
63
	public function getChild($name): ITrash {
64
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $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...
65
66
		foreach ($entries as $entry) {
67
			if ($entry->getName() === $name) {
68 View Code Duplication
				if ($entry->getType() === FileInfo::TYPE_FOLDER) {
69
					return new TrashFolderFolder($this->root . '/' . $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...
70
				}
71
				return new TrashFolderFile($this->root . '/' . $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
		}
74
75
		throw new NotFound();
76
	}
77
78
	public function getChildren(): array {
79
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $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...
80
81
		$children = array_map(function (FileInfo $entry) {
82 View Code Duplication
			if ($entry->getType() === FileInfo::TYPE_FOLDER) {
83
				return new TrashFolderFolder($this->root.'/'.$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...
84
			}
85
			return new TrashFolderFile($this->root.'/'.$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...
86
		}, $entries);
87
88
		return $children;
89
	}
90
91 View Code Duplication
	public function childExists($name): bool {
92
		$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->root . '/' . $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...
93
94
		foreach ($entries as $entry) {
95
			if ($entry->getName() === $name) {
96
				return true;
97
			}
98
		}
99
100
		return false;
101
	}
102
103
	public function delete() {
104
		\OCA\Files_Trashbin\Trashbin::delete($this->root . '/' . $this->getName(), $this->userId, null);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
105
	}
106
107
	public function getName(): string {
108
		return $this->data->getName();
109
110
	}
111
112
	public function setName($name) {
113
		throw new Forbidden();
114
	}
115
116
	public function getLastModified(): int {
117
		return $this->data->getMtime();
118
	}
119
120
	public function restore(): bool {
121
		return \OCA\Files_Trashbin\Trashbin::restore($this->root . '/' . $this->getName(), $this->data->getName(), null);
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
122
	}
123
124
	public function getFilename(): string {
125
		return $this->data->getName();
126
	}
127
128
	public function getOriginalLocation(): string {
129
		return $this->location . '/' . $this->getFilename();
130
	}
131
132
	public function getDeletionTime(): int {
133
		return $this->getLastModified();
134
	}
135
}
136