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

Plugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 54
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFeatures() 0 2 1
A __construct() 0 2 1
A getPluginName() 0 2 1
A beforeMethod() 0 28 5
A initialize() 0 3 1
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\Calendar;
29
use OCP\IRequest;
30
use Sabre\DAV\Exception\NotFound;
31
use Sabre\DAV\Server;
32
use Sabre\DAV\ServerPlugin;
33
use Sabre\HTTP\RequestInterface;
34
use Sabre\HTTP\ResponseInterface;
35
use function array_slice;
36
use function implode;
37
38
/**
39
 * Conditional logic to bypass the calendar trashbin
40
 */
41
class Plugin extends ServerPlugin {
42
43
	/** @var bool */
44
	private $disableTrashbin;
45
46
	/** @var Server */
47
	private $server;
48
49
	public function __construct(IRequest $request) {
50
		$this->disableTrashbin = $request->getHeader('X-NC-CalDAV-No-Trashbin') === '1';
51
	}
52
53
	public function initialize(Server $server): void {
54
		$this->server = $server;
55
		$server->on('beforeMethod:*', [$this, 'beforeMethod']);
56
	}
57
58
	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

58
	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...
59
		if (!$this->disableTrashbin) {
60
			return;
61
		}
62
63
		$path = $request->getPath();
64
		$pathParts = explode('/', ltrim($path, '/'));
65
		if (\count($pathParts) < 3) {
66
			// We are looking for a path like calendars/username/calendarname
67
			return;
68
		}
69
70
		// $calendarPath will look like calendars/username/calendarname
71
		$calendarPath = implode(
72
			'/',
73
			array_slice($pathParts, 0, 3)
74
		);
75
		try {
76
			$calendar = $this->server->tree->getNodeForPath($calendarPath);
77
			if (!($calendar instanceof Calendar)) {
78
				// This is odd
79
				return;
80
			}
81
82
			/** @var Calendar $calendar */
83
			$calendar->disableTrashbin();
84
		} catch (NotFound $ex) {
85
			return;
86
		}
87
	}
88
89
	public function getFeatures(): array {
90
		return ['nc-calendar-trashbin'];
91
	}
92
93
	public function getPluginName(): string {
94
		return 'nc-calendar-trashbin';
95
	}
96
}
97