Completed
Push — master ( 3363e2...c5c11c )
by
unknown
05:35
created

Socket::initScanner()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.3955

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 11
cts 17
cp 0.6471
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 0
crap 3.3955
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