Completed
Push — master ( c37ee6...eda387 )
by Robin
03:47
created

NotifyHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 *
7
 */
8
9
namespace Icewind\SMB\Wrapped;
10
11
12
use Icewind\SMB\Change;
13
use Icewind\SMB\Exception\Exception;
14
use Icewind\SMB\Exception\RevisionMismatchException;
15
use Icewind\SMB\INotifyHandler;
16
17
class NotifyHandler implements INotifyHandler {
18
	/**
19
	 * @var Connection
20
	 */
21
	private $connection;
22
23
	/**
24
	 * @var string
25
	 */
26
	private $path;
27
28
	private $listening = true;
29
30
	// see error.h
31
	const EXCEPTION_MAP = [
32
		ErrorCodes::RevisionMismatch => RevisionMismatchException::class,
33
	];
34
35
	/**
36
	 * @param Connection $connection
37
	 * @param string $path
38
	 */
39
	public function __construct(Connection $connection, $path) {
40
		$this->connection = $connection;
41
		$this->path = $path;
42
	}
43
44
	/**
45
	 * Get all changes detected since the start of the notify process or the last call to getChanges
46
	 *
47
	 * @return Change[]
48
	 */
49
	public function getChanges() {
50
		if (!$this->listening) {
51
			return [];
52
		}
53
		stream_set_blocking($this->connection->getOutputStream(), 0);
54
		$lines = [];
55
		while (($line = $this->connection->readLine())) {
56
			$this->checkForError($line);
57
			$lines[] = $line;
58
		}
59
		stream_set_blocking($this->connection->getOutputStream(), 1);
60
		return array_values(array_filter(array_map([$this, 'parseChangeLine'], $lines)));
61
	}
62
63
	/**
64
	 * Listen actively to all incoming changes
65
	 *
66
	 * Note that this is a blocking process and will cause the process to block forever if not explicitly terminated
67
	 *
68
	 * @param callable $callback
69
	 */
70
	public function listen($callback) {
71
		if ($this->listening) {
72
			$this->connection->read(function ($line) use ($callback) {
73
				$this->checkForError($line);
74
				$change = $this->parseChangeLine($line);
75
				if ($change) {
76
					return $callback($change);
77
				}
78
			});
79
		}
80
	}
81
82
	private function parseChangeLine($line) {
83
		$code = (int)substr($line, 0, 4);
84
		if ($code === 0) {
85
			return null;
86
		}
87
		$subPath = str_replace('\\', '/', substr($line, 5));
88
		if ($this->path === '') {
89
			return new Change($code, $subPath);
90
		} else {
91
			return new Change($code, $this->path . '/' . $subPath);
92
		}
93
	}
94
95
	private function checkForError($line) {
96
		if (substr($line, 0, 16) === 'notify returned ') {
97
			$error = substr($line, 16);
98
			throw Exception::fromMap(array_merge(self::EXCEPTION_MAP, Parser::EXCEPTION_MAP), $error, 'Notify is not supported with the used smb version');
99
		}
100
	}
101
102
	public function stop() {
103
		$this->listening = false;
104
		$this->connection->close();
105
	}
106
107
	public function __destruct() {
108
		$this->stop();
109
	}
110
}
111