Completed
Push — master ( 5fa6d3...7e7cc5 )
by
unknown
03:10
created

Socket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A initScanner() 0 25 3
1
<?php
2
/**
3
 * ownCloud - Files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2017-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus\Scanner;
15
16
use OCA\Files_Antivirus\AppConfig;
17
use OCP\ILogger;
18
19
/**
20
 * Class Socket
21
 *
22
 * @package OCA\Files_Antivirus\Scanner
23
 */
24
class Socket extends External {
25
26
	/**
27
	 * @var string
28
	 */
29
	private $socket;
30
31
	/**
32
	 * Socket constructor.
33
	 *
34
	 * @param AppConfig $config
35
	 * @param ILogger $logger
36
	 *
37
	 * @throws InitException
38
	 */
39 1
	public function __construct(AppConfig $config, ILogger $logger) {
40 1
		parent::__construct($config, $logger);
41 1
		$this->socket = $this->appConfig->getAvSocket();
42 1
		if ($this->socket === '') {
43
			throw new InitException(
44
				'Socket mode requires a path to the unix socket but it is empty.'
45
			);
46
		}
47 1
	}
48
49
	/**
50
	 * @throws InitException
51
	 */
52 1
	public function initScanner() {
53 1
		parent::initScanner();
54 1
		$this->writeHandle = @\stream_socket_client(
55 1
			'unix://' . $this->socket, $errorCode, $errorMessage, 5
56
		);
57 1
		if (!$this->getWriteHandle()) {
58 1
			throw new InitException(
59 1
				\sprintf(
60 1
					'Could not connect to socket "%s": %s (code %d)',
61 1
					$this->socket,
62 1
					$errorMessage,
63 1
					$errorCode
64
				)
65
			);
66
		}
67
68
		if (@\fwrite($this->getWriteHandle(), "nINSTREAM\n") === false) {
69
			throw new InitException(
70
				\sprintf(
71
					'Writing to socket "%s" failed',
72
					$this->socket
73
				)
74
			);
75
		}
76
	}
77
}
78