Passed
Push — master ( 25fe32...0fb74a )
by Morris
13:02 queued 10s
created

Plugin::isCachingEnabledForThisRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2018 Georg Ehrke <[email protected]>
5
 *
6
 * @author Georg Ehrke <[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\DAV\CalDAV\WebcalCaching;
25
26
use OCA\DAV\CalDAV\CalendarHome;
27
use OCP\IRequest;
28
use Sabre\DAV\Exception\NotFound;
29
use Sabre\DAV\Server;
30
use Sabre\DAV\ServerPlugin;
31
use Sabre\HTTP\RequestInterface;
32
use Sabre\HTTP\ResponseInterface;
33
34
class Plugin extends ServerPlugin {
35
36
	/**
37
	 * list of regular expressions for calendar user agents,
38
	 * that do not support subscriptions on their own
39
	 *
40
	 * @var string[]
41
	 */
42
	const ENABLE_FOR_CLIENTS = [];
43
44
	/**
45
	 * @var bool
46
	 */
47
	private $enabled=false;
48
49
	/**
50
	 * @var Server
51
	 */
52
	private $server;
53
54
	/**
55
	 * Plugin constructor.
56
	 *
57
	 * @param IRequest $request
58
	 */
59
	public function __construct(IRequest $request) {
60
		if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) {
61
			$this->enabled = true;
62
		}
63
64
		$magicHeader = $request->getHeader('X-NC-CalDAV-Webcal-Caching');
65
		if ($magicHeader === 'On') {
66
			$this->enabled = true;
67
		}
68
	}
69
70
	/**
71
	 * This initializes the plugin.
72
	 *
73
	 * This function is called by Sabre\DAV\Server, after
74
	 * addPlugin is called.
75
	 *
76
	 * This method should set up the required event subscriptions.
77
	 *
78
	 * @param Server $server
79
	 */
80
	public function initialize(Server $server) {
81
		$this->server = $server;
82
		$server->on('beforeMethod', [$this, 'beforeMethod']);
83
	}
84
85
	/**
86
	 * @param RequestInterface $request
87
	 * @param ResponseInterface $response
88
	 */
89
	public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
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

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

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...
90
		if (!$this->enabled) {
91
			return;
92
		}
93
94
		$path = $request->getPath();
95
		$pathParts = explode('/', ltrim($path, '/'));
96
		if (\count($pathParts) < 2) {
97
			return;
98
		}
99
100
		// $calendarHomePath will look like: calendars/username
101
		$calendarHomePath = $pathParts[0] . '/' . $pathParts[1];
102
		try {
103
			$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
104
			if (!($calendarHome instanceof CalendarHome)) {
105
				//how did we end up here?
106
				return;
107
			}
108
109
			$calendarHome->enableCachedSubscriptionsForThisRequest();
110
		} catch(NotFound $ex) {
111
			return;
112
		}
113
	}
114
115
	/**
116
	 * @return bool
117
	 */
118
	public function isCachingEnabledForThisRequest():bool {
119
		return $this->enabled;
120
	}
121
122
	/**
123
	 * This method should return a list of server-features.
124
	 *
125
	 * This is for example 'versioning' and is added to the DAV: header
126
	 * in an OPTIONS response.
127
	 *
128
	 * @return string[]
129
	 */
130
	public function getFeatures():array {
131
		return ['nc-calendar-webcal-cache'];
132
	}
133
134
	/**
135
	 * Returns a plugin name.
136
	 *
137
	 * Using this name other plugins will be able to access other plugins
138
	 * using Sabre\DAV\Server::getPlugin
139
	 *
140
	 * @return string
141
	 */
142
	public function getPluginName():string {
143
		return 'nc-calendar-webcal-cache';
144
	}
145
}
146