Completed
Pull Request — master (#195)
by Victor
19:38 queued 09:40
created

ScannerFactory::testConnection()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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