Completed
Push — notifyhandler ( 1e7809...ebc1c0 )
by Robin
03:34
created

NativeState::handleError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2014 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
namespace Icewind\SMB;
9
10
use Icewind\SMB\Exception\Exception;
11
12
/**
13
 * Low level wrapper for libsmbclient-php for error handling
14
 */
15
class NativeState {
16
	/**
17
	 * @var resource
18
	 */
19
	protected $state;
20
21
	protected $handlerSet = false;
22
23
	protected $connected = false;
24
25
	// todo replace with static once <5.6 support is dropped
26
	// see error.h
27
	private static $exceptionMap = [
28
		1   => '\Icewind\SMB\Exception\ForbiddenException',
29
		2   => '\Icewind\SMB\Exception\NotFoundException',
30
		13  => '\Icewind\SMB\Exception\ForbiddenException',
31
		17  => '\Icewind\SMB\Exception\AlreadyExistsException',
32
		20  => '\Icewind\SMB\Exception\InvalidTypeException',
33
		21  => '\Icewind\SMB\Exception\InvalidTypeException',
34
		39  => '\Icewind\SMB\Exception\NotEmptyException',
35
		110 => '\Icewind\SMB\Exception\TimedOutException',
36
		111 => '\Icewind\SMB\Exception\ConnectionRefusedException',
37
		112 => '\Icewind\SMB\Exception\HostDownException',
38
		113 => '\Icewind\SMB\Exception\NoRouteToHostException'
39
	];
40
41 508
	protected function handleError($path) {
42 508
		$error = smbclient_state_errno($this->state);
43 508
		throw Exception::fromMap(self::$exceptionMap, $error, $path);
44
	}
45
46 508
	protected function testResult($result, $uri) {
47 508
		if ($result === false or $result === null) {
48
			// smb://host/share/path
49 508
			if (is_string($uri)) {
50 36
				list(, , , , $path) = explode('/', $uri, 5);
51 36
				$path = '/' . $path;
52 36
			} else {
53 508
				$path = null;
54
			}
55 508
			$this->handleError($path);
56
		}
57 508
	}
58
59
	/**
60
	 * @param string $workGroup
61
	 * @param string $user
62
	 * @param string $password
63
	 * @return bool
64
	 */
65 508
	public function init($workGroup, $user, $password) {
66 508
		if ($this->connected) {
67 508
			return true;
68
		}
69 508
		$this->state = smbclient_state_new();
70 508
		$result = @smbclient_state_init($this->state, $workGroup, $user, $password);
71
72 508
		$this->testResult($result, '');
73 508
		$this->connected = true;
74 508
		return $result;
75
	}
76
77
	/**
78
	 * @param string $uri
79
	 * @return resource
80
	 */
81 508
	public function opendir($uri) {
82 508
		$result = @smbclient_opendir($this->state, $uri);
83
84 508
		$this->testResult($result, $uri);
85 508
		return $result;
86
	}
87
88
	/**
89
	 * @param resource $dir
90
	 * @return array
91
	 */
92 508
	public function readdir($dir) {
93 508
		$result = @smbclient_readdir($this->state, $dir);
94
95 508
		$this->testResult($result, $dir);
96 508
		return $result;
97
	}
98
99
	/**
100
	 * @param $dir
101
	 * @return bool
102
	 */
103
	public function closedir($dir) {
104
		$result = smbclient_closedir($this->state, $dir);
105
106
		$this->testResult($result, $dir);
107
		return $result;
108
	}
109
110
	/**
111
	 * @param string $old
112
	 * @param string $new
113
	 * @return bool
114
	 */
115 36
	public function rename($old, $new) {
116 36
		$result = @smbclient_rename($this->state, $old, $this->state, $new);
117
118 36
		$this->testResult($result, $new);
119 32
		return $result;
120
	}
121
122
	/**
123
	 * @param string $uri
124
	 * @return bool
125
	 */
126 86
	public function unlink($uri) {
127 86
		$result = @smbclient_unlink($this->state, $uri);
128
129 86
		$this->testResult($result, $uri);
130 82
		return $result;
131
	}
132
133
	/**
134
	 * @param string $uri
135
	 * @param int $mask
136
	 * @return bool
137
	 */
138 508
	public function mkdir($uri, $mask = 0777) {
139 508
		$result = @smbclient_mkdir($this->state, $uri, $mask);
140
141 508
		$this->testResult($result, $uri);
142 508
		return $result;
143
	}
144
145
	/**
146
	 * @param string $uri
147
	 * @return bool
148
	 */
149 26
	public function rmdir($uri) {
150 26
		$result = @smbclient_rmdir($this->state, $uri);
151
152 26
		$this->testResult($result, $uri);
153 16
		return $result;
154
	}
155
156
	/**
157
	 * @param string $uri
158
	 * @return array
159
	 */
160 84
	public function stat($uri) {
161 84
		$result = @smbclient_stat($this->state, $uri);
162
163 84
		$this->testResult($result, $uri);
164 82
		return $result;
165
	}
166
167
	/**
168
	 * @param resource $file
169
	 * @return array
170
	 */
171
	public function fstat($file) {
172
		$result = @smbclient_fstat($this->state, $file);
173
174
		$this->testResult($result, $file);
175
		return $result;
176
	}
177
178
	/**
179
	 * @param string $uri
180
	 * @param string $mode
181
	 * @param int $mask
182
	 * @return resource
183
	 */
184 108
	public function open($uri, $mode, $mask = 0666) {
185 108
		$result = @smbclient_open($this->state, $uri, $mode, $mask);
186
187 108
		$this->testResult($result, $uri);
188 104
		return $result;
189
	}
190
191
	/**
192
	 * @param string $uri
193
	 * @param int $mask
194
	 * @return resource
195
	 */
196 248
	public function create($uri, $mask = 0666) {
197 248
		$result = @smbclient_creat($this->state, $uri, $mask);
198
199 248
		$this->testResult($result, $uri);
200 244
		return $result;
201
	}
202
203
	/**
204
	 * @param resource $file
205
	 * @param int $bytes
206
	 * @return string
207
	 */
208 102
	public function read($file, $bytes) {
209 102
		$result = @smbclient_read($this->state, $file, $bytes);
210
211 102
		$this->testResult($result, $file);
212 102
		return $result;
213
	}
214
215
	/**
216
	 * @param resource $file
217
	 * @param string $data
218
	 * @param int $length
219
	 * @return int
220
	 */
221 240
	public function write($file, $data, $length = null) {
222 240
		$result = @smbclient_write($this->state, $file, $data, $length);
223
224 240
		$this->testResult($result, $file);
225 240
		return $result;
226
	}
227
228
	/**
229
	 * @param resource $file
230
	 * @param int $offset
231
	 * @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
232
	 * @return int | bool new file offset as measured from the start of the file on success, false on failure.
233
	 */
234 2
	public function lseek($file, $offset, $whence = SEEK_SET) {
235 2
		$result = @smbclient_lseek($this->state, $file, $offset, $whence);
236
237 2
		$this->testResult($result, $file);
238 2
		return $result;
239
	}
240
241
	/**
242
	 * @param resource $file
243
	 * @param int $size
244
	 * @return bool
245
	 */
246 2
	public function ftruncate($file, $size) {
247 2
		$result = @smbclient_ftruncate($this->state, $file, $size);
248
249 2
		$this->testResult($result, $file);
250 2
		return $result;
251
	}
252
253 244
	public function close($file) {
254 244
		$result = @smbclient_close($this->state, $file);
255
256 244
		$this->testResult($result, $file);
257 244
		return $result;
258
	}
259
260
	/**
261
	 * @param string $uri
262
	 * @param string $key
263
	 * @return string
264
	 */
265 26
	public function getxattr($uri, $key) {
266 26
		$result = @smbclient_getxattr($this->state, $uri, $key);
267
268 26
		$this->testResult($result, $uri);
269 26
		return $result;
270
	}
271
272
	/**
273
	 * @param string $uri
274
	 * @param string $key
275
	 * @param string $value
276
	 * @param int $flags
277
	 * @return mixed
278
	 */
279 16
	public function setxattr($uri, $key, $value, $flags = 0) {
280 16
		$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
281
282 16
		$this->testResult($result, $uri);
283 16
		return $result;
284
	}
285
286 496
	public function __destruct() {
287 496
		if ($this->connected) {
288 496
			smbclient_state_free($this->state);
289 496
		}
290 496
	}
291
}
292