Completed
Pull Request — master (#195)
by Victor
19:38 queued 09:40
created

Socket::initScanner()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
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
	public function __construct(AppConfig $config, ILogger $logger) {
28
		parent::__construct($config, $logger);
29
		$this->socket = $this->appConfig->getAvSocket();
30
		if ($this->socket === '') {
31
			throw new InitException(
32
				'Socket mode requires a path to the unix socket but it is empty.'
33
			);
34
		}
35
	}
36
37
	/**
38
	 * @throws InitException
39
	 */
40
	public function initScanner(){
41
		parent::initScanner();
42
		$this->writeHandle = @stream_socket_client(
43
			'unix://' . $this->socket, $errorCode, $errorMessage, 5
44
		);
45
		if (!$this->getWriteHandle()) {
46
			throw new InitException(
47
				sprintf(
48
					'Could not connect to socket "%s": %s (code %d)',
49
					$this->socket,
50
					$errorMessage,
51
					$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