Completed
Push — master ( 33b511...2595f8 )
by
unknown
10s
created

ScannerFactory::testConnection()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
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
	 * @var string
31
	 */
32
	protected $scannerClass;
33
	
34 15
	public function __construct(AppConfig $appConfig, ILogger $logger) {
35 15
		$this->appConfig = $appConfig;
36 15
		$this->logger = $logger;
37
		try {
38 15
			$this->getScannerClass();
39
		} catch (InitException $e) {
40
			// rethrow misconfiguration exception
41
			throw $e;
42
		} catch (\Exception $e) {
43
			$message = 	implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
44
			$this->logger->warning($message, ['app' => 'files_antivirus']);
45
		}
46 15
	}
47
48
	/**
49
	 * @throws InitException
50
	 */
51 15
	protected function getScannerClass() {
52 15
		switch ($this->appConfig->getAvMode()) {
53 15
			case 'daemon':
54 9
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Daemon';
55 9
				break;
56 7
			case 'socket':
57 1
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Socket';
58 1
				break;
59 6
			case 'executable':
60 6
				$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Local';
61 6
				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 15
	}
71
	
72
	/**
73
	 * Produce a scanner instance 
74
	 * @return \OCA\Files_Antivirus\Scanner\AbstractScanner
75
	 */
76 8
	public function getScanner() {
77 8
		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
		$this->getScannerClass();
87
		try {
88
			$scanner = $this->getScanner();
89
			$item = new Content(self::EICAR_PART_1 . self::EICAR_PART_2, 4096);
90
			$status = $scanner->scan($item);
91
			return $status->getNumericStatus() === Status::SCANRESULT_INFECTED;
92
		} catch (\Exception $e) {
93
			$this->logger->warning($e->getMessage(), ['app' => 'files_antivirus']);
94
			return false;
95
		}
96
	}
97
}
98