Passed
Push — master ( 6578a9...ac4ff6 )
by Christoph
16:15 queued 12s
created

DeletedCalendarObject::put()   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 1
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 OCA\DAV\CalDAV\IRestorable;
30
use Sabre\CalDAV\ICalendarObject;
31
use Sabre\DAV\Exception\Forbidden;
32
use Sabre\DAVACL\ACLTrait;
33
use Sabre\DAVACL\IACL;
34
35
class DeletedCalendarObject implements IACL, ICalendarObject, IRestorable {
36
	use ACLTrait;
37
38
	/** @var string */
39
	private $name;
40
41
	/** @var mixed[] */
42
	private $objectData;
43
44
	/** @var string */
45
	private $principalUri;
46
47
	/** @var CalDavBackend */
48
	private $calDavBackend;
49
50
	public function __construct(string $name,
51
								array $objectData,
52
								string $principalUri,
53
								CalDavBackend $calDavBackend) {
54
		$this->name = $name;
55
		$this->objectData = $objectData;
56
		$this->calDavBackend = $calDavBackend;
57
		$this->principalUri = $principalUri;
58
	}
59
60
	public function delete() {
61
		$this->calDavBackend->deleteCalendarObject(
62
			$this->objectData['calendarid'],
63
			$this->objectData['uri'],
64
			CalDavBackend::CALENDAR_TYPE_CALENDAR,
65
			true
66
		);
67
	}
68
69
	public function getName() {
70
		return $this->name;
71
	}
72
73
	public function setName($name) {
74
		throw new Forbidden();
75
	}
76
77
	public function getLastModified() {
78
		return 0;
79
	}
80
81
	public function put($data) {
82
		throw new Forbidden();
83
	}
84
85
	public function get() {
86
		return $this->objectData['calendardata'];
87
	}
88
89
	public function getContentType() {
90
		$mime = 'text/calendar; charset=utf-8';
91
		if (isset($this->objectData['component']) && $this->objectData['component']) {
92
			$mime .= '; component='.$this->objectData['component'];
93
		}
94
95
		return $mime;
96
	}
97
98
	public function getETag() {
99
		return $this->objectData['etag'];
100
	}
101
102
	public function getSize() {
103
		return (int) $this->objectData['size'];
104
	}
105
106
	public function restore(): void {
107
		$this->calDavBackend->restoreCalendarObject($this->objectData);
108
	}
109
110
	public function getDeletedAt(): ?int {
111
		return $this->objectData['deleted_at'] ? (int) $this->objectData['deleted_at'] : null;
112
	}
113
114
	public function getCalendarUri(): string {
115
		return $this->objectData['calendaruri'];
116
	}
117
118
	public function getACL(): array {
119
		return [
120
			[
121
				'privilege' => '{DAV:}read', // For queries
122
				'principal' => $this->getOwner(),
123
				'protected' => true,
124
			],
125
			[
126
				'privilege' => '{DAV:}unbind', // For moving and deletion
127
				'principal' => '{DAV:}owner',
128
				'protected' => true,
129
			],
130
		];
131
	}
132
133
	public function getOwner() {
134
		return $this->principalUri;
135
	}
136
}
137