Completed
Push — master ( 6889d8...82f961 )
by Robin
04:17 queued 10s
created

NativeState   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 288
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 86.99%

Importance

Changes 11
Bugs 0 Features 2
Metric Value
wmc 39
c 11
b 0
f 2
lcom 1
cbo 10
dl 0
loc 288
ccs 107
cts 123
cp 0.8699
rs 8.2857

22 Methods

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