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

Socket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 51
ccs 16
cts 24
cp 0.6667
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B initScanner() 0 25 3
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