Passed
Push — master ( c5a2ca...9a196d )
by Roeland
12:57 queued 11s
created

PropfindCompressionPlugin::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020, Roeland Jago Douma <[email protected]>
6
 *
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\DAV\Connector\Sabre;
27
28
use Sabre\DAV\ServerPlugin;
29
use Sabre\HTTP\Request;
30
use Sabre\HTTP\Response;
31
32
class PropfindCompressionPlugin extends ServerPlugin {
33
34
	/**
35
	 * Reference to main server object
36
	 *
37
	 * @var Server
38
	 */
39
	private $server;
40
41
	/**
42
	 * This initializes the plugin.
43
	 *
44
	 * This function is called by \Sabre\DAV\Server, after
45
	 * addPlugin is called.
46
	 *
47
	 * This method should set up the required event subscriptions.
48
	 *
49
	 * @param \Sabre\DAV\Server $server
50
	 * @return void
51
	 */
52
	public function initialize(\Sabre\DAV\Server $server) {
53
		$this->server = $server;
0 ignored issues
show
Documentation Bug introduced by
$server is of type Sabre\DAV\Server, but the property $server was declared to be of type OCA\DAV\Connector\Sabre\Server. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
54
		$this->server->on('afterMethod:PROPFIND', [$this, 'compressResponse'], 100);
55
	}
56
57
	public function compressResponse(Request $request, Response $response) {
58
		$header = $request->getHeader('Accept-Encoding');
59
60
		if ($header === null) {
61
			return $response;
62
		}
63
64
		if (strpos($header, 'gzip') !== false) {
65
			$body = $response->getBody();
66
			if (is_string($body)) {
67
				$response->setHeader('Content-Encoding', 'gzip');
68
				$response->setBody(gzencode($body));
69
			}
70
		}
71
72
		return $response;
73
	}
74
}
75