Completed
Push — fix-179 ( 161381 )
by Victor
08:27
created

AntivirusPlugin::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * ownCloud - Files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus\Dav;
15
16
use OCA\DAV\Upload\FutureFile;
17
use OCP\IUserSession;
18
use Sabre\DAV\Server;
19
use Sabre\DAV\ServerPlugin;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\EventDispatcher\GenericEvent;
22
23
/**
24
 * Sabre plugin for the the antivirus
25
 */
26
class AntivirusPlugin extends ServerPlugin {
27
	const NS_OWNCLOUD = 'http://owncloud.org/ns';
28
29
	/**
30
	 * @var \Sabre\DAV\Server $server
31
	 */
32
	private $server;
33
34
	/**
35
	 * @var EventDispatcherInterface
36
	 */
37
	private $dispatcher;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param IUserSession $userSession
43
	 */
44
	public function __construct(
45
		IUserSession $userSession, EventDispatcherInterface $dispatcher
46
	) {
47
		$this->userSession = $userSession;
48
		$this->dispatcher = $dispatcher;
49
	}
50
51
	/**
52
	 * This initializes the plugin.
53
	 *
54
	 * This function is called by Sabre\DAV\Server, after
55
	 * addPlugin is called.
56
	 *
57
	 * This method should set up the required event subscriptions.
58
	 *
59
	 * @param Server $server
60
	 *
61
	 * @return void
62
	 */
63
	function initialize(Server $server) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
		$this->server = $server;
65
		$this->server->on('beforeMove', [$this, 'beforeMove'], 1);
66
	}
67
68
69
	/**
70
	 * @param string $source
71
	 * @param string $destination
72
	 *
73
	 * @return bool|void
74
	 * @throws \Sabre\DAV\Exception\NotFound
75
	 */
76
	public function beforeMove($source, $destination) {
77
		$sourceNode = $this->server->tree->getNodeForPath($source);
78
		if (!$sourceNode instanceof FutureFile) {
0 ignored issues
show
Bug introduced by
The class OCA\DAV\Upload\FutureFile does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79
			return;
80
		}
81
		$finalSize = $sourceNode->getSize();
82
		$this->dispatcher->dispatch(
83
			'\OCA\Files_Antivirus\Dav\AntivirusPlugin::beforeMove',
84
			new GenericEvent(
85
				null,
86
				[
87
					'path' => $destination,
88
					'finalSize' => $finalSize
89
				]
90
			)
91
		);
92
		return true;
93
	}
94
}
95