Completed
Pull Request — master (#195)
by Victor
03:10
created

Daemon::initScanner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Copyright (c) 2017 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
10
namespace OCA\Files_Antivirus\Scanner;
11
12
use OCA\Files_Antivirus\AppConfig;
13
use OCP\ILogger;
14
15
class Daemon extends External {
16
17
	/** @var string  */
18
	private $avHost;
19
20
	/** @var int  */
21
	private $avPort;
22
23
	/**
24
	 * Daemon constructor.
25
	 *
26
	 * @param AppConfig $config
27
	 * @param ILogger $logger
28
	 * @throws InitException
29
	 */
30 5
	public function __construct(AppConfig $config, ILogger $logger) {
31 5
		parent::__construct($config, $logger);
32
33 5
		$this->avHost = $this->appConfig->getAvHost();
34 5
		$this->avPort = $this->appConfig->getAvPort();
35
		$checks = [
36 5
			'hostname' => $this->avHost,
37 5
			'port' => $this->avPort
38 5
		];
39 5
		$errors = [];
40 5
		foreach ($checks as $key => $check) {
41 5
			if ($check === '') {
42 2
				$errors[] = sprintf(
43 2
					'Daemon mode requires a %s but it is empty.',
44
					$key
45 2
				);
46 2
			}
47 5
		}
48
49 5
		if (count($errors) > 0) {
50 2
			throw new InitException(
51 2
				'The app is not configured properly. ' . implode(' ', $errors)
52 2
			);
53
		}
54 3
	}
55
56
	/**
57
	 * @throws InitException
58
	 */
59 3
	public function initScanner(){
60 3
		parent::initScanner();
61 3
		$this->writeHandle = @fsockopen($this->avHost, $this->avPort);
62 3
		if (!$this->getWriteHandle()) {
63 1
			throw new InitException(
64 1
				sprintf(
65 1
					'Could not connect to host "%s" on port %d', $this->avHost, $this->avPort
66 1
				)
67 1
			);
68
		}
69
		// request scan from the daemon
70 2
		@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...
71 2
	}
72
}
73