Completed
Pull Request — stable8.2 (#145)
by Victor
02:24
created

BackgroundScanner::run()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 7.0188

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 64
ccs 51
cts 55
cp 0.9273
rs 7.2058
cc 7
eloc 46
nc 13
nop 0
crap 7.0188

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 OCP\IUserManager;
12
use OCP\IL10N;
13
14
class BackgroundScanner {
15
16
	const BATCH_SIZE = 10;
17
18
	/** @var ScannerFactory */
19
	private $scannerFactory;
20
	
21
	/** @var IUserManager */
22
	private $userManager;
23
24
	/** @var IL10N */
25
	private $l10n;
26
27
	/** @var  AppConfig  */
28
	private $appConfig;
29
30
	/**
31
	 * A constructor
32
	 *
33
	 * @param \OCA\Files_Antivirus\ScannerFactory $scannerFactory
34
	 * @param AppConfig $appConfig
35
	 * @param IUserManager $userManager
36
	 * @param IL10N $l10n
37
	 */
38 1
	public function __construct(ScannerFactory $scannerFactory, AppConfig $appConfig, IUserManager $userManager, IL10N $l10n){
39 1
		$this->scannerFactory = $scannerFactory;
40 1
		$this->userManager = $userManager;
41 1
		$this->l10n = $l10n;
42 1
		$this->appConfig = $appConfig;
43 1
	}
44
	
45
	/**
46
	 * Background scanner main job
47
	 * @return null
48
	 */
49 1
	public function run(){
50 1
		if (!$this->initFS()) {
51
			return;
52
		}
53
		// locate files that are not checked yet
54 1
		$dirMimeTypeId = \OC::$server->getMimeTypeLoader()->getId('httpd/unix-directory');
55
		try {
56 1
			$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
57 1
			$qb->select(['fc.fileid'])
58 1
				->from('filecache', 'fc')
59 1
				->leftJoin('fc', 'files_antivirus', 'fa', $qb->expr()->eq('fa.fileid', 'fc.fileid'))
60 1
				->innerJoin(
61 1
					'fc',
62 1
					'storages',
63 1
					'ss',
64 1
					$qb->expr()->andX(
65 1
						$qb->expr()->eq('fc.storage', 'ss.numeric_id'),
66 1
						$qb->expr()->orX(
67 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('local::%')),
68 1
							$qb->expr()->like('ss.id', $qb->expr()->literal('home::%'))
69 1
						)
70 1
					)
71 1
				)
72 1
				->where(
73 1
					$qb->expr()->neq('fc.mimetype', $qb->expr()->literal($dirMimeTypeId))
74 1
				)
75 1
				->andWhere(
76 1
					$qb->expr()->orX(
77 1
						$qb->expr()->isNull('fa.fileid'),
78 1
						$qb->expr()->gt('fc.mtime', 'fa.check_time')
79 1
					)
80 1
				)
81 1
				->andWhere(
82 1
					$qb->expr()->like('fc.path', $qb->expr()->literal('files/%'))
83 1
				)
84 1
				->andWhere(
85 1
					$qb->expr()->neq('fc.size', $qb->expr()->literal('0'))
86 1
				)
87
			;
88 1
			$result = $qb->execute();
89 1
		} catch(\Exception $e) {
90
			\OC::$server->getLogger()->error( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
91
			return;
92
		}
93
94 1
		$view = new \OC\Files\View('');
95 1
		$cnt = 0;
96 1
		while (($row = $result->fetch()) && $cnt < self::BATCH_SIZE) {
97
			try {
98 1
				$path = $view->getPath($row['fileid']);
99 1
				if (!is_null($path)) {
100 1
					$item = new Item($this->l10n, $view, $path, $row['fileid']);
101 1
					$scanner = $this->scannerFactory->getScanner();
102 1
					$status = $scanner->scan($item);
103 1
					$status->dispatch($item, true);
104
 					// increased only for successfully scanned files
105 1
					$cnt = $cnt + 1;
106 1
				}
107 1
			} catch (\Exception $e){
108
				\OC::$server->getLogger()->debug( __METHOD__ . ', exception: ' . $e->getMessage(), ['app' => 'files_antivirus']);
109
			}
110 1
		}
111 1
		\OC_Util::tearDownFS();
112 1
	}
113
114
	/**
115
	 * A hack to access files and views. Better than before.
116
	 *
117
	 * @return bool
118
	 */
119 1
	protected function initFS(){
120
		//Need any valid user to mount FS
121 1
		$results = $this->userManager->search('', 2, 0);
122 1
		$anyUser = array_pop($results);
123 1
		if (is_null($anyUser)) {
124
			\OC::$server->getLogger()->error("Failed to setup file system", ['app' => 'files_antivirus']);
125
			return false;
126
		}
127 1
		\OC_Util::tearDownFS();
128 1
		\OC_Util::setupFS($anyUser->getUID());
129 1
		return true;
130
	}
131
132
	/**
133
	 * @deprecated 
134
	 */
135
	public static function check(){
136
		return \OCA\Files_Antivirus\Cron\Task::run();
137
	}
138
}
139