Completed
Push — master ( bebed6...b6e66d )
by Robin
02:58
created

NativeShare   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 11
dl 0
loc 298
ccs 0
cts 101
cp 0
rs 9.6
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getState() 0 9 3
A getName() 0 3 1
A buildUrl() 0 10 2
A dir() 0 17 4
A stat() 0 3 1
A getStat() 0 3 1
A mkdir() 0 3 1
A rmdir() 0 3 1
A del() 0 3 1
A rename() 0 3 1
A put() 0 10 2
B get() 0 27 6
A read() 0 5 1
A write() 0 5 1
A getAttribute() 0 3 1
A setAttribute() 0 7 3
A setMode() 0 3 1
A notify() 0 9 2
A __destruct() 0 3 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\Native;
9
10
use Icewind\SMB\AbstractShare;
11
use Icewind\SMB\Exception\DependencyException;
12
use Icewind\SMB\Exception\InvalidPathException;
13
use Icewind\SMB\Exception\InvalidResourceException;
14
use Icewind\SMB\INotifyHandler;
15
use Icewind\SMB\IServer;
16
use Icewind\SMB\Wrapped\Server;
17
use Icewind\SMB\Wrapped\Share;
18
19
class NativeShare extends AbstractShare {
20
	/**
21
	 * @var IServer $server
22
	 */
23
	private $server;
24
25
	/**
26
	 * @var string $name
27
	 */
28
	private $name;
29
30
	/**
31
	 * @var NativeState $state
32
	 */
33
	private $state;
34
35
	/**
36
	 * @param IServer $server
37
	 * @param string $name
38
	 */
39
	public function __construct($server, $name) {
40
		parent::__construct();
41
		$this->server = $server;
42
		$this->name = $name;
43
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50
	protected function getState() {
51
		if ($this->state and $this->state instanceof NativeState) {
52
			return $this->state;
53
		}
54
55
		$this->state = new NativeState();
56
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65
	public function getName() {
66
		return $this->name;
67
	}
68
69
	private function buildUrl($path) {
70
		$this->verifyPath($path);
71
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72
		if ($path) {
73
			$path = trim($path, '/');
74
			$url .= '/';
75
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76
		}
77
		return $url;
78
	}
79
80
	/**
81
	 * List the content of a remote folder
82
	 *
83
	 * @param string $path
84
	 * @return \Icewind\SMB\IFileInfo[]
85
	 *
86
	 * @throws \Icewind\SMB\Exception\NotFoundException
87
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
88
	 */
89
	public function dir($path) {
90
		$files = [];
91
92
		$dh = $this->getState()->opendir($this->buildUrl($path));
93
		while ($file = $this->getState()->readdir($dh)) {
94
			$name = $file['name'];
95
			if ($name !== '.' and $name !== '..') {
96
				$fullPath = $path . '/' . $name;
97
				$files [] = new NativeFileInfo($this, $fullPath, $name, function() use ($fullPath) {
98
					return $this->stat($fullPath);
99
				});
100
			}
101
		}
102
103
		$this->getState()->closedir($dh);
104
		return $files;
105
	}
106
107
	/**
108
	 * @param string $path
109
	 * @return \Icewind\SMB\IFileInfo
110
	 */
111
	public function stat($path) {
112
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
113
	}
114
115
	private function getStat($path) {
116
		return $this->getState()->stat($this->buildUrl($path));
117
	}
118
119
	/**
120
	 * Create a folder on the share
121
	 *
122
	 * @param string $path
123
	 * @return bool
124
	 *
125
	 * @throws \Icewind\SMB\Exception\NotFoundException
126
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
127
	 */
128
	public function mkdir($path) {
129
		return $this->getState()->mkdir($this->buildUrl($path));
130
	}
131
132
	/**
133
	 * Remove a folder on the share
134
	 *
135
	 * @param string $path
136
	 * @return bool
137
	 *
138
	 * @throws \Icewind\SMB\Exception\NotFoundException
139
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
140
	 */
141
	public function rmdir($path) {
142
		return $this->getState()->rmdir($this->buildUrl($path));
143
	}
144
145
	/**
146
	 * Delete a file on the share
147
	 *
148
	 * @param string $path
149
	 * @return bool
150
	 *
151
	 * @throws \Icewind\SMB\Exception\NotFoundException
152
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
153
	 */
154
	public function del($path) {
155
		return $this->getState()->unlink($this->buildUrl($path));
156
	}
157
158
	/**
159
	 * Rename a remote file
160
	 *
161
	 * @param string $from
162
	 * @param string $to
163
	 * @return bool
164
	 *
165
	 * @throws \Icewind\SMB\Exception\NotFoundException
166
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
167
	 */
168
	public function rename($from, $to) {
169
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
170
	}
171
172
	/**
173
	 * Upload a local file
174
	 *
175
	 * @param string $source local file
176
	 * @param string $target remove file
177
	 * @return bool
178
	 *
179
	 * @throws \Icewind\SMB\Exception\NotFoundException
180
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
181
	 */
182
	public function put($source, $target) {
183
		$sourceHandle = fopen($source, 'rb');
184
		$targetHandle = $this->getState()->create($this->buildUrl($target));
185
186
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
187
			$this->getState()->write($targetHandle, $data);
188
		}
189
		$this->getState()->close($targetHandle);
190
		return true;
191
	}
192
193
	/**
194
	 * Download a remote file
195
	 *
196
	 * @param string $source remove file
197
	 * @param string $target local file
198
	 * @return bool
199
	 *
200
	 * @throws \Icewind\SMB\Exception\NotFoundException
201
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
202
	 * @throws \Icewind\SMB\Exception\InvalidPathException
203
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
204
	 */
205
	public function get($source, $target) {
206
		if (!$target) {
207
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
208
		}
209
		$targetHandle = @fopen($target, 'wb');
210
		if (!$targetHandle) {
211
			$error = error_get_last();
212
			if (is_array($error)) {
213
				$reason = $error['message'];
214
			} else {
215
				$reason = 'Unknown error';
216
			}
217
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
218
		}
219
220
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
221
		if (!$sourceHandle) {
222
			fclose($targetHandle);
223
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
224
		}
225
226
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
227
			fwrite($targetHandle, $data);
228
		}
229
		$this->getState()->close($sourceHandle);
230
		return true;
231
	}
232
233
	/**
234
	 * Open a readable stream top a remote file
235
	 *
236
	 * @param string $source
237
	 * @return resource a read only stream with the contents of the remote file
238
	 *
239
	 * @throws \Icewind\SMB\Exception\NotFoundException
240
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
241
	 */
242
	public function read($source) {
243
		$url = $this->buildUrl($source);
244
		$handle = $this->getState()->open($url, 'r');
245
		return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
246
	}
247
248
	/**
249
	 * Open a readable stream top a remote file
250
	 *
251
	 * @param string $source
252
	 * @return resource a read only stream with the contents of the remote file
253
	 *
254
	 * @throws \Icewind\SMB\Exception\NotFoundException
255
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
256
	 */
257
	public function write($source) {
258
		$url = $this->buildUrl($source);
259
		$handle = $this->getState()->create($url);
260
		return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
261
	}
262
263
	/**
264
	 * Get extended attributes for the path
265
	 *
266
	 * @param string $path
267
	 * @param string $attribute attribute to get the info
268
	 * @return string the attribute value
269
	 */
270
	public function getAttribute($path, $attribute) {
271
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
272
	}
273
274
	/**
275
	 * Get extended attributes for the path
276
	 *
277
	 * @param string $path
278
	 * @param string $attribute attribute to get the info
279
	 * @param mixed $value
280
	 * @return string the attribute value
281
	 */
282
	public function setAttribute($path, $attribute, $value) {
283
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
284
			$value = '0x' . dechex($value);
285
		}
286
287
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
288
	}
289
290
	/**
291
	 * @param string $path
292
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
293
	 * @return mixed
294
	 */
295
	public function setMode($path, $mode) {
296
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
297
	}
298
299
	/**
300
	 * @param string $path
301
	 * @return INotifyHandler
302
	 */
303
	public function notify($path) {
304
		// php-smbclient does support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
305
		// so we use the smbclient based backend for this
306
		if (!Server::available($this->server->getSystem())) {
307
			throw new DependencyException('smbclient not found in path for notify command');
308
		}
309
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
310
		return $share->notify($path);
311
	}
312
313
	public function __destruct() {
314
		unset($this->state);
315
	}
316
}
317