Completed
Pull Request — master (#195)
by Victor
10:55 queued 09:43
created

ScannerFactory::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11.0182

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 13
cts 27
cp 0.4815
rs 8.439
c 0
b 0
f 0
cc 6
eloc 29
nc 7
nop 2
crap 11.0182
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 8
	public function __construct(AppConfig $appConfig, ILogger $logger){
32 8
		$this->appConfig = $appConfig;
33 8
		$this->logger = $logger;
34
		try {
35 8
			$avMode = $appConfig->getAvMode();
36
			switch ($avMode) {
37 8
				case 'daemon':
38 4
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Daemon';
39 4
					break;
40 5
				case 'socket':
41
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Socket';
42
					break;
43 5
				case 'executable':
44 5
					$this->scannerClass = 'OCA\Files_Antivirus\Scanner\Local';
45 5
					break;
46
				default:
47
					throw new InitException(
48
						sprintf(
49
							'Please check the settings at the admin page. Invalid mode: "%s"',
50
							$avMode
51
						)
52
					);
53 8
					break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
54
			}
55
		} catch (InitException $e) {
56
			// TODO: add a new config option to prevent upload on misconfiguration
57
			//  and check it here
58
			$this->logger->warning(
59
				sprintf(
60
					'Application is misconfigured. %s',
61
					$e->getMessage()
62
				)
63
			);
64
		} catch (\Exception $e) {
65
			$message = 	implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]);
66
			$this->logger->warning($message);
67
		}
68 8
	}
69
	
70
	/**
71
	 * Produce a scanner instance 
72
	 * @return \OCA\Files_Antivirus\Scanner\AbstractScanner
73
	 */
74 4
	public function getScanner(){
75 4
		return new $this->scannerClass($this->appConfig, $this->logger);
76
	}
77
}
78