ScannerFactory::getScannerClass()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 16
cp 0.75
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4.25
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 2014-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus;
15
16
use OCA\Files_Antivirus\Scanner\InitException;
17
use \OCP\ILogger;
18
19
class ScannerFactory {
20
	// We split it in two parts in order to prevent reports from av scanners
21
	const EICAR_PART_1 = 'X5O!P%@AP[4\PZX54(P^)7CC)7}$';
22
	const EICAR_PART_2 = 'EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*';
23
	
24
	/**
25
	 * @var \OCA\Files_Antivirus\AppConfig
26
	 */
27
	protected $appConfig;
28
	
29
	/**
30
	 * @var ILogger;
31
	 */
32
	protected $logger;
33
	
34
	/**
35
	 * @var string
36
	 */
37
	protected $scannerClass;
38
	
39 15
	public function __construct(AppConfig $appConfig, ILogger $logger) {
40 15
		$this->appConfig = $appConfig;
41 15
		$this->logger = $logger;
42
		try {
43 15
			$this->getScannerClass();
44
		} catch (InitException $e) {
45
			// rethrow misconfiguration exception
46
			throw $e;
47
		} catch (\Exception $e) {
48
			$message = 	\implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
49
			$this->logger->warning($message, ['app' => 'files_antivirus']);
50
		}
51 15
	}
52
53
	/**
54
	 * @throws InitException
55
	 */
56 15
	protected function getScannerClass() {
57 15
		switch ($this->appConfig->getAvMode()) {
58 15
			case 'daemon':
59 9
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Daemon';
60 9
				break;
61 7
			case 'socket':
62 1
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Socket';
63 1
				break;
64 6
			case 'executable':
65 6
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Local';
66 6
				break;
67
			default:
68
				throw new InitException(
69
					\sprintf(
70
						'Please check the settings at the admin page. Invalid mode: "%s"',
71
						$this->appConfig->getAvMode()
72
					)
73
				);
74
		}
75 15
	}
76
	
77
	/**
78
	 * Produce a scanner instance
79
	 *
80
	 * @return \OCA\Files_Antivirus\Scanner\AbstractScanner
81
	 */
82 8
	public function getScanner() {
83 8
		return new $this->scannerClass($this->appConfig, $this->logger);
84
	}
85
86
	/**
87
	 * @param AppConfig $appConfig
88
	 *
89
	 * @return bool
90
	 */
91
	public function testConnection(AppConfig $appConfig) {
92
		$this->appConfig = $appConfig;
93
		$this->getScannerClass();
94
		try {
95
			$scanner = $this->getScanner();
96
			$item = new Content(self::EICAR_PART_1 . self::EICAR_PART_2, 4096);
97
			$status = $scanner->scan($item);
98
			return $status->getNumericStatus() === Status::SCANRESULT_INFECTED;
99
		} catch (\Exception $e) {
100
			$this->logger->warning($e->getMessage(), ['app' => 'files_antivirus']);
101
			return false;
102
		}
103
	}
104
}
105