Completed
Push — master ( 74b39a...45a732 )
by Thomas
04:27
created

Socket::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0932
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 2017-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 Socket
21
 *
22
 * @package OCA\Files_Antivirus\Scanner
23
 */
24
class Socket extends External {
25
26
	/**
27
	 * @var string
28
	 */
29
	private $socket;
30
31
	/**
32
	 * Socket constructor.
33
	 *
34
	 * @param AppConfig $config
35
	 * @param ILogger $logger
36
	 *
37
	 * @throws InitException
38
	 */
39 1
	public function __construct(AppConfig $config, ILogger $logger) {
40 1
		parent::__construct($config, $logger);
41 1
		$this->socket = $this->appConfig->getAvSocket();
42 1
		if ($this->socket === '') {
43
			throw new InitException(
44
				'Socket mode requires a path to the unix socket but it is empty.'
45
			);
46
		}
47 1
	}
48
49
	/**
50
	 * @throws InitException
51
	 */
52 1
	public function initScanner() {
53 1
		parent::initScanner();
54 1
		$this->writeHandle = @\stream_socket_client(
55 1
			'unix://' . $this->socket, $errorCode, $errorMessage, 5
56 1
		);
57 1
		if (!$this->getWriteHandle()) {
58 1
			throw new InitException(
59 1
				\sprintf(
60 1
					'Could not connect to socket "%s": %s (code %d)',
61 1
					$this->socket,
62 1
					$errorMessage,
63
					$errorCode
64 1
				)
65 1
			);
66
		}
67
68
		if (@\fwrite($this->getWriteHandle(), "nINSTREAM\n") === false) {
69
			throw new InitException(
70
				\sprintf(
71
					'Writing to socket "%s" failed',
72
					$this->socket
73
				)
74
			);
75
		}
76
	}
77
}
78