Completed
Push — closes-181 ( fce50e...02104e )
by Victor
05:19
created

Daemon::shutdownScanner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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