Passed
Push — master ( 7e8ddc...b908db )
by Christoph
14:53 queued 12s
created

Plugin::propFind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
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 Closure;
29
use OCA\DAV\CalDAV\Calendar;
30
use OCP\IRequest;
31
use Sabre\DAV\Exception\NotFound;
32
use Sabre\DAV\INode;
33
use Sabre\DAV\PropFind;
34
use Sabre\DAV\Server;
35
use Sabre\DAV\ServerPlugin;
36
use Sabre\HTTP\RequestInterface;
37
use Sabre\HTTP\ResponseInterface;
38
use function array_slice;
39
use function implode;
40
41
class Plugin extends ServerPlugin {
42
	public const PROPERTY_DELETED_AT = '{http://nextcloud.com/ns}deleted-at';
43
44
	/** @var bool */
45
	private $disableTrashbin;
46
47
	/** @var Server */
48
	private $server;
49
50
	public function __construct(IRequest $request) {
51
		$this->disableTrashbin = $request->getHeader('X-NC-CalDAV-No-Trashbin') === '1';
52
	}
53
54
	public function initialize(Server $server): void {
55
		$this->server = $server;
56
		$server->on('beforeMethod:*', [$this, 'beforeMethod']);
57
		$server->on('propFind', Closure::fromCallable([$this, 'propFind']));
58
	}
59
60
	public function beforeMethod(RequestInterface $request, ResponseInterface $response): void {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
	public function beforeMethod(RequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
		if (!$this->disableTrashbin) {
62
			return;
63
		}
64
65
		$path = $request->getPath();
66
		$pathParts = explode('/', ltrim($path, '/'));
67
		if (\count($pathParts) < 3) {
68
			// We are looking for a path like calendars/username/calendarname
69
			return;
70
		}
71
72
		// $calendarPath will look like calendars/username/calendarname
73
		$calendarPath = implode(
74
			'/',
75
			array_slice($pathParts, 0, 3)
76
		);
77
		try {
78
			$calendar = $this->server->tree->getNodeForPath($calendarPath);
79
			if (!($calendar instanceof Calendar)) {
80
				// This is odd
81
				return;
82
			}
83
84
			/** @var Calendar $calendar */
85
			$calendar->disableTrashbin();
86
		} catch (NotFound $ex) {
87
			return;
88
		}
89
	}
90
91
	private function propFind(
92
		PropFind $propFind,
93
		INode $node): void {
94
		if ($node instanceof DeletedCalendarObject) {
95
			$propFind->handle(self::PROPERTY_DELETED_AT, function () use ($node) {
96
				return $node->getDeletedAt();
97
			});
98
		}
99
	}
100
101
	public function getFeatures(): array {
102
		return ['nc-calendar-trashbin'];
103
	}
104
105
	public function getPluginName(): string {
106
		return 'nc-calendar-trashbin';
107
	}
108
}
109