Completed
Pull Request — master (#118)
by Victor
02:39
created

BackgroundScanner   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 61.53%

Importance

Changes 16
Bugs 7 Features 0
Metric Value
wmc 16
c 16
b 7
f 0
lcom 2
cbo 4
dl 0
loc 161
ccs 56
cts 91
cp 0.6153
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserFolder() 0 7 2
A initFilesystemForUser() 0 10 3
A check() 0 2 1
A __construct() 0 10 1
B run() 0 66 6
A getOwner() 0 11 3
1
<?php
2
/**
3
 * Copyright (c) 2012 Bart Visscher <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus;
10
11
use OC\Files\Filesystem;
12
use OC\Files\View;
13
use OCP\IL10N;
14
use OCP\IUser;
15
use OCP\Files\IRootFolder;
16
use OCP\IUserSession;
17
18
class BackgroundScanner {
19
20
	const BATCH_SIZE = 10;
21
22
	/** @var IRootFolder */
23
	protected $rootFolder;
24
25
	/** @var \OCP\Files\Folder[] */
26
	protected $userFolders;
27
28
	/**
29
	 * @var ScannerFactory
30
	 */
31
	private $scannerFactory;
32
33
	
34
	/**
35
	 * @var IL10N
36
	 */
37
	private $l10n;
38
39
	/** @var string */
40
	protected $currentFilesystemUser;
41
42
	/** @var \OCP\IUserSession */
43
	protected $userSession;
44
45
	/**
46
	 * A constructor
47
	 *
48
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
49
	 * @param IL10N $l10n
50
	 * @param IRootFolder $rootFolder
51
	 * @param IUserSession $userSession
52
	 */
53 1
	public function __construct(ScannerFactory $scannerFactory,
54
								IL10N $l10n,
55
								IRootFolder $rootFolder,
56
								IUserSession $userSession
57
	){
58 1
		$this->rootFolder = $rootFolder;
59 1
		$this->scannerFactory = $scannerFactory;
60 1
		$this->l10n = $l10n;
61 1
		$this->userSession = $userSession;
62 1
	}
63
	
64
	/**
65
	 * Background scanner main job
66
	 * @return null
67
	 */
68 1
	public function run(){
69
		// locate files that are not checked yet
70 1
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
71
		try {
72 1
			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
73 1
			$qb->select(['fc.fileid'])
74 1
				->from('filecache', 'fc')
75 1
				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
76 1
				->innerJoin(
77 1
					'fc',
78 1
					'storages',
79 1
					'ss',
80 1
					$qb->expr()->andX(
81 1
						$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
82 1
						$qb->expr()->orX(
83 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
84 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
85 1
						)
86 1
					)
87 1
				)
88 1
				->where(
89 1
					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
90 1
				)
91 1
				->andWhere(
92 1
					$qb->expr()->orX(
93 1
						$qb->expr()->isNull('fa.fileid'),
94 1
						$qb->expr()->gt('fc.mtime', 'fa.check_time')
95 1
					)
96 1
				)
97 1
				->andWhere(
98 1
					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
99 1
				)
100 1
				->andWhere(
101 1
					$qb->expr()->neq('fc.size', $qb->expr()->literal('0'))
102 1
				)
103 1
				->setMaxResults(self::BATCH_SIZE)
104
			;
105 1
			$result = $qb->execute();
106 1
		} catch(\Exception $e) {
107
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
108
			return;
109
		}
110
111
		try {
112 1
			while ($row = $result->fetch()) {
113 1
				$fileId = $row['fileid'];
114 1
				$owner = $this->getOwner($fileId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $owner is correct as $this->getOwner($fileId) (which targets OCA\Files_Antivirus\BackgroundScanner::getOwner()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
115 1
				if (!$owner){
116 1
					continue;
117
				}
118
				$this->initFilesystemForUser($owner);
119
				$view = new View('/');
120
121
				$path = $view->getPath($fileId);
122
				if (!is_null($path)) {
123
					$item = new Item($this->l10n, $view, $path, $fileId);
124
					$scanner = $this->scannerFactory->getScanner();
125
					$status = $scanner->scan($item);
126
					$status->dispatch($item, true);
127
				}
128
			}
129 1
		} catch (\Exception $e){
130
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
131
		}
132 1
		\OC_Util::tearDownFS();
133 1
	}
134
135 1
	protected function getOwner($fileId){
136 1
		$mountProviderCollection = \OC::$server->getMountProviderCollection();
137 1
		$mountCache = $mountProviderCollection->getMountCache();
138 1
		$mounts = $mountCache->getMountsForFileId($fileId);
139 1
		if (!empty($mounts)) {
140
			$user = $mounts[0]->getUser();
141
			if ($user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser 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...
142
				return $user;
143
			}
144
		}
145 1
	}
146
147
	/**
148
	 * @param \OCP\IUser $user
149
	 * @return \OCP\Files\Folder
150
	 */
151
	protected function getUserFolder(IUser $user) {
152
		if (!isset($this->userFolders[$user->getUID()])) {
153
			$userFolder = $this->rootFolder->getUserFolder($user->getUID());
154
			$this->userFolders[$user->getUID()] = $userFolder;
155
		}
156
		return $this->userFolders[$user->getUID()];
157
	}
158
159
	/**
160
	 * @param IUser $user
161
	 */
162
	protected function initFilesystemForUser(IUser $user) {
163
		if ($this->currentFilesystemUser !== $user->getUID()) {
164
			if ($this->currentFilesystemUser !== '') {
165
				Filesystem::tearDown();
166
			}
167
			Filesystem::init($user->getUID(), '/' . $user->getUID() . '/files');
168
			$this->userSession->setUser($user);
169
			$this->currentFilesystemUser = $user->getUID();
170
		}
171
	}
172
173
	/**
174
	 * @deprecated since  v8.0.0
175
	 */
176
	public static function check(){
177
	}
178
}
179