Completed
Push — stable3.0 ( 04db6f...bd43cd )
by Robin
03:57
created

NotifyHandler::getChanges()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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 20
	public function __construct(Connection $connection, $path) {
40 20
		$this->connection = $connection;
41 20
		$this->path = $path;
42 20
	}
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 16
	public function getChanges() {
50 16
		if (!$this->listening) {
51 4
			return [];
52
		}
53 12
		stream_set_blocking($this->connection->getOutputStream(), 0);
54 12
		$lines = [];
55 12
		while (($line = $this->connection->readLine())) {
56 12
			$this->checkForError($line);
57 12
			$lines[] = $line;
58 3
		}
59 12
		stream_set_blocking($this->connection->getOutputStream(), 1);
60 12
		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 8
	public function listen($callback) {
71 8
		if ($this->listening) {
72 8
			$this->connection->read(function ($line) use ($callback) {
73 8
				$this->checkForError($line);
74 8
				$change = $this->parseChangeLine($line);
75 8
				if ($change) {
76 8
					return $callback($change);
77
				}
78 8
			});
79 2
		}
80 8
	}
81
82 16
	private function parseChangeLine($line) {
83 16
		$code = (int)substr($line, 0, 4);
84 16
		if ($code === 0) {
85 12
			return null;
86
		}
87 16
		$subPath = str_replace('\\', '/', substr($line, 5));
88 16
		if ($this->path === '') {
89 12
			return new Change($code, $subPath);
90
		} else {
91 4
			return new Change($code, $this->path . '/' . $subPath);
92
		}
93
	}
94
95 16
	private function checkForError($line) {
96 16
		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 16
	}
101
102 20
	public function stop() {
103 20
		$this->listening = false;
104 20
		$this->connection->close();
105 20
	}
106
107 20
	public function __destruct() {
108 20
		$this->stop();
109 20
	}
110
}
111