Daemon   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 65
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 4
A initScanner() 0 15 2
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 2015-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus\Scanner;
15
16
use OCA\Files_Antivirus\AppConfig;
17
use OCP\ILogger;
18
19
/**
20
 * Class Daemon
21
 *
22
 * @package OCA\Files_Antivirus\Scanner
23
 */
24
class Daemon extends External {
25
26
	/**
27
	 * @var string
28
	 */
29
	private $avHost;
30
31
	/**
32
	 * @var int
33
	 */
34
	private $avPort;
35
36
	/**
37
	 * Daemon constructor.
38
	 *
39
	 * @param AppConfig $config
40
	 * @param ILogger $logger
41
	 *
42
	 * @throws InitException
43
	 */
44 7
	public function __construct(AppConfig $config, ILogger $logger) {
45 7
		parent::__construct($config, $logger);
46
47 7
		$this->avHost = $this->appConfig->getAvHost();
48 7
		$this->avPort = $this->appConfig->getAvPort();
49
		$checks = [
50 7
			'hostname' => $this->avHost,
51 7
			'port' => $this->avPort
52
		];
53 7
		$errors = [];
54 7
		foreach ($checks as $key => $check) {
55 7
			if ($check === '') {
56 2
				$errors[] = \sprintf(
57 2
					'Daemon mode requires a %s but it is empty.',
58 7
					$key
59
				);
60
			}
61
		}
62
63 7
		if (\count($errors) > 0) {
64 2
			throw new InitException(
65 2
				'The app is not configured properly. ' . \implode(' ', $errors)
66
			);
67
		}
68 5
	}
69
70
	/**
71
	 * @throws InitException
72
	 */
73 5
	public function initScanner() {
74 5
		parent::initScanner();
75 5
		$this->writeHandle = @\fsockopen($this->avHost, $this->avPort);
76 5
		if (!$this->getWriteHandle()) {
77 1
			throw new InitException(
78 1
				\sprintf(
79 1
					'Could not connect to host "%s" on port %d',
80 1
					$this->avHost,
81 1
					$this->avPort
82
				)
83
			);
84
		}
85
		// request scan from the daemon
86 4
		@\fwrite($this->getWriteHandle(), "nINSTREAM\n");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87 4
	}
88
}
89