Passed
Push — master ( 80a4ed...26ec76 )
by Robin
73:34 queued 71:57
created

NotifyHandler::checkForError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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