Passed
Push — master ( 9e596d...9f70c6 )
by Christoph
15:44 queued 10s
created

DeletedCalendarObjectsCollection::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2021 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2021 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\DAV\CalDAV\Trashbin;
27
28
use OCA\DAV\CalDAV\CalDavBackend;
29
use Sabre\CalDAV\ICalendarObjectContainer;
30
use Sabre\DAV\Exception\BadRequest;
31
use Sabre\DAV\Exception\Forbidden;
32
use Sabre\DAV\Exception\NotFound;
33
use Sabre\DAV\Exception\NotImplemented;
34
use function array_map;
35
use function implode;
36
use function preg_match;
37
38
class DeletedCalendarObjectsCollection implements ICalendarObjectContainer {
39
	public const NAME = 'objects';
40
41
	/** @var CalDavBackend */
42
	protected $caldavBackend;
43
44
	/** @var mixed[] */
45
	private $principalInfo;
46
47
	public function __construct(CalDavBackend $caldavBackend,
48
								array $principalInfo) {
49
		$this->caldavBackend = $caldavBackend;
50
		$this->principalInfo = $principalInfo;
51
	}
52
53
	/**
54
	 * @see \OCA\DAV\CalDAV\Trashbin\DeletedCalendarObjectsCollection::calendarQuery
55
	 */
56
	public function getChildren() {
57
		throw new NotImplemented();
58
	}
59
60
	public function getChild($name) {
61
		if (!preg_match("/(\d+)\\.ics/", $name, $matches)) {
62
			throw new NotFound();
63
		}
64
65
		$data = $this->caldavBackend->getCalendarObjectById(
66
			$this->principalInfo['uri'],
67
			(int) $matches[1],
68
		);
69
70
		// If the object hasn't been deleted yet then we don't want to find it here
71
		if ($data === null) {
72
			throw new NotFound();
73
		}
74
		if (!isset($data['deleted_at'])) {
75
			throw new BadRequest('The calendar object you\'re trying to restore is not marked as deleted');
76
		}
77
78
		return new DeletedCalendarObject(
79
			$this->getRelativeObjectPath($data),
80
			$data,
81
			$this->caldavBackend
82
		);
83
	}
84
85
	public function createFile($name, $data = null) {
86
		throw new Forbidden();
87
	}
88
89
	public function createDirectory($name) {
90
		throw new Forbidden();
91
	}
92
93
	public function childExists($name) {
94
		try {
95
			$this->getChild($name);
96
		} catch (NotFound $e) {
97
			return false;
98
		}
99
100
		return true;
101
	}
102
103
	public function delete() {
104
		throw new Forbidden();
105
	}
106
107
	public function getName(): string {
108
		return self::NAME;
109
	}
110
111
	public function setName($name) {
112
		throw new Forbidden();
113
	}
114
115
	public function getLastModified(): int {
116
		return 0;
117
	}
118
119
	public function calendarQuery(array $filters) {
120
		return array_map(function (array $calendarInfo) {
121
			return $this->getRelativeObjectPath($calendarInfo);
122
		}, $this->caldavBackend->getDeletedCalendarObjectsByPrincipal($this->principalInfo['uri']));
123
	}
124
125
	private function getRelativeObjectPath(array $calendarInfo): string {
126
		return implode(
127
			'.',
128
			[$calendarInfo['id'], 'ics'],
129
		);
130
	}
131
}
132