Completed
Push — notifyhandler ( ebc1c0...c1afd7 )
by Robin
04:14
created

NativeState::testResult()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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