Completed
Push — master ( aec9b6...c1a84f )
by Robin
04:02 queued 01:19
created

NativeShare::get()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.1215

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 20
cp 0.85
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6.1215
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 256
	public function __construct($server, $name) {
40 256
		parent::__construct();
41 256
		$this->server = $server;
42 256
		$this->name = $name;
43 256
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 256
	protected function getState() {
51 256
		if ($this->state and $this->state instanceof NativeState) {
52 256
			return $this->state;
53
		}
54
55 256
		$this->state = new NativeState();
56 256
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 256
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65 1
	public function getName() {
66 1
		return $this->name;
67
	}
68
69 256
	private function buildUrl($path) {
70 256
		$this->verifyPath($path);
71 256
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 256
		if ($path) {
73 256
			$path = trim($path, '/');
74 256
			$url .= '/';
75 256
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 256
		}
77 256
		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 256
	public function dir($path) {
90 256
		$files = [];
91
92 256
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 256
		while ($file = $this->getState()->readdir($dh)) {
94 256
			$name = $file['name'];
95 256
			if ($name !== '.' and $name !== '..') {
96 103
				$fullPath = $path . '/' . $name;
97 103
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
98 103
					return $this->getStat($fullPath);
99 103
				});
100 103
			}
101 256
		}
102
103 256
		$this->getState()->closedir($dh);
104 256
		return $files;
105
	}
106
107
	/**
108
	 * @param string $path
109
	 * @return \Icewind\SMB\IFileInfo
110
	 */
111 33
	public function stat($path) {
112 33
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
113
	}
114
115 115
	private function getStat($path) {
116 115
		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 256
	public function mkdir($path) {
129 256
		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 256
	public function rmdir($path) {
142 256
		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 134
	public function del($path) {
155 134
		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 28
	public function rename($from, $to) {
169 28
		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 113
	public function put($source, $target) {
183 113
		$sourceHandle = fopen($source, 'rb');
184 113
		$targetHandle = $this->getState()->create($this->buildUrl($target));
185
186 102
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
187 102
			$this->getState()->write($targetHandle, $data);
188 102
		}
189 102
		$this->getState()->close($targetHandle);
190 102
		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 44
	public function get($source, $target) {
206 44
		if (!$target) {
207 9
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
208
		}
209 35
		$targetHandle = @fopen($target, 'wb');
210 35
		if (!$targetHandle) {
211 1
			$error = error_get_last();
212 1
			if (is_array($error)) {
213 1
				$reason = $error['message'];
214 1
			} else {
215
				$reason = 'Unknown error';
216
			}
217 1
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
218
		}
219
220 34
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
221 32
		if (!$sourceHandle) {
222
			fclose($targetHandle);
223
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
224
		}
225
226 32
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
227 32
			fwrite($targetHandle, $data);
228 32
		}
229 32
		$this->getState()->close($sourceHandle);
230 32
		return true;
231
	}
232
233
	/**
234
	 * Open a readable stream to 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 29
	public function read($source) {
243 29
		$url = $this->buildUrl($source);
244 20
		$handle = $this->getState()->open($url, 'r');
245 20
		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 writeable stream to a remote file
250
	 * Note: This method will truncate the file to 0bytes first
251
	 *
252
	 * @param string $source
253
	 * @return resource a writeable stream
254
	 *
255
	 * @throws \Icewind\SMB\Exception\NotFoundException
256
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
257
	 */
258 29
	public function write($source) {
259 29
		$url = $this->buildUrl($source);
260 20
		$handle = $this->getState()->create($url);
261 20
		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...
262
	}
263
264
	/**
265
	 * Open a writeable stream and set the cursor to the end of the stream
266
	 *
267
	 * @param string $source
268
	 * @return resource a writeable stream
269
	 *
270
	 * @throws \Icewind\SMB\Exception\NotFoundException
271
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
272
	 */
273 1
	public function append($source) {
274 1
		$url = $this->buildUrl($source);
275 1
		$handle = $this->getState()->open($url, "a");
276 1
		return NativeWriteStream::wrap($this->getState(), $handle, "a", $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...
277
	}
278
279
	/**
280
	 * Get extended attributes for the path
281
	 *
282
	 * @param string $path
283
	 * @param string $attribute attribute to get the info
284
	 * @return string the attribute value
285
	 */
286 14
	public function getAttribute($path, $attribute) {
287 14
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
288
	}
289
290
	/**
291
	 * Set extended attributes for the given path
292
	 *
293
	 * @param string $path
294
	 * @param string $attribute attribute to get the info
295
	 * @param string|int $value
296
	 * @return mixed the attribute value
297
	 */
298 8
	public function setAttribute($path, $attribute, $value) {
299 8
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
300 8
			$value = '0x' . dechex($value);
301 8
		}
302
303 8
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
304
	}
305
306
	/**
307
	 * Set DOS comaptible node mode
308
	 *
309
	 * @param string $path
310
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
311
	 * @return mixed
312
	 */
313 8
	public function setMode($path, $mode) {
314 8
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
315
	}
316
317
	/**
318
	 * Start smb notify listener
319
	 * Note: This is a blocking call
320
	 *
321
	 * @param string $path
322
	 * @return INotifyHandler
323
	 */
324
	public function notify($path) {
325
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
326
		// so we use the smbclient based backend for this
327
		if (!Server::available($this->server->getSystem())) {
328
			throw new DependencyException('smbclient not found in path for notify command');
329
		}
330
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
331
		return $share->notify($path);
332
	}
333
334 256
	public function __destruct() {
335 256
		unset($this->state);
336 256
	}
337
}
338