Completed
Push — master ( fdaf09...72216c )
by Robin
04:18
created

NativeShare::del()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
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\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 510
	public function __construct($server, $name) {
40 510
		parent::__construct();
41 510
		$this->server = $server;
42 510
		$this->name = $name;
43 510
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 510
	protected function getState() {
51 510
		if ($this->state and $this->state instanceof NativeState) {
52 510
			return $this->state;
53
		}
54
55 510
		$this->state = new NativeState();
56 510
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 510
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65 2
	public function getName() {
66 2
		return $this->name;
67
	}
68
69 510
	private function buildUrl($path) {
70 510
		$this->verifyPath($path);
71 510
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 510
		if ($path) {
73 510
			$path = trim($path, '/');
74 510
			$url .= '/';
75 510
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 255
		}
77 510
		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 510
	public function dir($path) {
90 510
		$files = [];
91
92 510
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 510
		while ($file = $this->getState()->readdir($dh)) {
94 510
			$name = $file['name'];
95 510
			if ($name !== '.' and $name !== '..') {
96 204
				$fullPath = $path . '/' . $name;
97 204
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
98 204
					return $this->getStat($fullPath);
99 204
				});
100 102
			}
101 255
		}
102
103 510
		$this->getState()->closedir($dh);
104 510
		return $files;
105
	}
106
107
	/**
108
	 * @param string $path
109
	 * @return \Icewind\SMB\IFileInfo
110
	 */
111 66
	public function stat($path) {
112 66
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
113
	}
114
115 228
	private function getStat($path) {
116 228
		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 510
	public function mkdir($path) {
129 510
		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 510
	public function rmdir($path) {
142 510
		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 266
	public function del($path) {
155 266
		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 56
	public function rename($from, $to) {
169 56
		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 226
	public function put($source, $target) {
183 226
		$sourceHandle = fopen($source, 'rb');
184 226
		$targetHandle = $this->getState()->create($this->buildUrl($target));
185
186 204
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
187 204
			$this->getState()->write($targetHandle, $data);
188 102
		}
189 204
		$this->getState()->close($targetHandle);
190 204
		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 88
	public function get($source, $target) {
206 88
		if (!$target) {
207 18
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
208
		}
209 70
		$targetHandle = @fopen($target, 'wb');
210 70
		if (!$targetHandle) {
211 2
			$error = error_get_last();
212 2
			if (is_array($error)) {
213 2
				$reason = $error['message'];
214 1
			} else {
215
				$reason = 'Unknown error';
216
			}
217 2
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
218
		}
219
220 68
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
221 64
		if (!$sourceHandle) {
222
			fclose($targetHandle);
223
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
224
		}
225
226 64
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
227 64
			fwrite($targetHandle, $data);
228 32
		}
229 64
		$this->getState()->close($sourceHandle);
230 64
		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 58
	public function read($source) {
243 58
		$url = $this->buildUrl($source);
244 40
		$handle = $this->getState()->open($url, 'r');
245 40
		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 58
	public function write($source) {
258 58
		$url = $this->buildUrl($source);
259 40
		$handle = $this->getState()->create($url);
260 40
		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 28
	public function getAttribute($path, $attribute) {
271 28
		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 string|int $value
280
	 * @return mixed the attribute value
281
	 */
282 16
	public function setAttribute($path, $attribute, $value) {
283 16
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
284 16
			$value = '0x' . dechex($value);
285 8
		}
286
287 16
		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 16
	public function setMode($path, $mode) {
296 16
		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 510
	public function __destruct() {
314 510
		unset($this->state);
315 510
	}
316
}
317