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

Socket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 40
rs 10
c 0
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 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