Completed
Pull Request — master (#195)
by Victor
10:00 queued 08:41
created

ScannerFactory::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.5145

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 15
cts 23
cp 0.6522
rs 8.439
cc 6
eloc 25
nc 7
nop 2
crap 7.5145
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
	
16
	/**
17
	 * @var \OCA\Files_Antivirus\AppConfig
18
	 */
19
	protected $appConfig;
20
	
21
	/**
22
	 * @var ILogger;
23
	 */
24
	protected $logger;
25
	
26
	/**
27
	 * @var string
28
	 */
29
	protected $scannerClass;
30
	
31 13
	public function __construct(AppConfig $appConfig, ILogger $logger){
32 13
		$this->appConfig = $appConfig;
33 13
		$this->logger = $logger;
34
		try {
35 13
			$avMode = $appConfig->getAvMode();
36
			switch ($avMode) {
37 13
				case 'daemon':
38 7
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Daemon';
39 7
					break;
40 7
				case 'socket':
41 1
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Socket';
42 1
					break;
43 6
				case 'executable':
44 6
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Local';
45 6
					break;
46
				default:
47
					throw new InitException(
48
						sprintf(
49
							'Please check the settings at the admin page. Invalid mode: "%s"',
50 13
							$avMode
51
						)
52
					);
53
			}
54
		} catch (InitException $e) {
55
			// rethrow misconfiguration exception
56
			throw $e;
57
		} catch (\Exception $e) {
58
			$message = 	implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
59
			$this->logger->warning($message, ['app' => 'files_antivirus']);
60
		}
61 13
	}
62
	
63
	/**
64
	 * Produce a scanner instance 
65
	 * @return \OCA\Files_Antivirus\Scanner\AbstractScanner
66
	 */
67 9
	public function getScanner(){
68 9
		return new $this->scannerClass($this->appConfig, $this->logger);
69
	}
70
}
71