Completed
Pull Request — master (#195)
by Victor
17:32 queued 07:34
created

Socket::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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