Completed
Push — master ( d0f8dc...76d05f )
by Victor
14s
created

Socket::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

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