Completed
Push — stable3.0 ( bd43cd...55b7d1 )
by Robin
08:57 queued 07:06
created

NativeShare   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 91.84%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 11
dl 0
loc 296
ccs 90
cts 98
cp 0.9184
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 14 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 8 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 255
	public function __construct($server, $name) {
40 255
		parent::__construct();
41 255
		$this->server = $server;
42 255
		$this->name = $name;
43 255
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 255
	protected function getState() {
51 255
		if ($this->state and $this->state instanceof NativeState) {
52 255
			return $this->state;
53
		}
54
55 255
		$this->state = new NativeState();
56 255
		$this->state->init($this->server->getAuth());
57 255
		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 255
	private function buildUrl($path) {
70 255
		$this->verifyPath($path);
71 255
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 255
		if ($path) {
73 255
			$path = trim($path, '/');
74 255
			$url .= '/';
75 255
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 255
		}
77 255
		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 255
	public function dir($path) {
90 255
		$files = array();
91
92 255
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 255
		while ($file = $this->getState()->readdir($dh)) {
94 255
			$name = $file['name'];
95 255
			if ($name !== '.' and $name !== '..') {
96 102
				$files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
97 102
			}
98 255
		}
99
100 255
		$this->getState()->closedir($dh);
101 255
		return $files;
102
	}
103
104
	/**
105
	 * @param string $path
106
	 * @return \Icewind\SMB\IFileInfo
107
	 */
108 33
	public function stat($path) {
109 33
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
110
	}
111
112 114
	public function getStat($path) {
113 114
		return $this->getState()->stat($this->buildUrl($path));
114
	}
115
116
	/**
117
	 * Create a folder on the share
118
	 *
119
	 * @param string $path
120
	 * @return bool
121
	 *
122
	 * @throws \Icewind\SMB\Exception\NotFoundException
123
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
124
	 */
125 255
	public function mkdir($path) {
126 255
		return $this->getState()->mkdir($this->buildUrl($path));
127
	}
128
129
	/**
130
	 * Remove a folder on the share
131
	 *
132
	 * @param string $path
133
	 * @return bool
134
	 *
135
	 * @throws \Icewind\SMB\Exception\NotFoundException
136
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
137
	 */
138 255
	public function rmdir($path) {
139 255
		return $this->getState()->rmdir($this->buildUrl($path));
140
	}
141
142
	/**
143
	 * Delete a file on the share
144
	 *
145
	 * @param string $path
146
	 * @return bool
147
	 *
148
	 * @throws \Icewind\SMB\Exception\NotFoundException
149
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
150
	 */
151 133
	public function del($path) {
152 133
		return $this->getState()->unlink($this->buildUrl($path));
153
	}
154
155
	/**
156
	 * Rename a remote file
157
	 *
158
	 * @param string $from
159
	 * @param string $to
160
	 * @return bool
161
	 *
162
	 * @throws \Icewind\SMB\Exception\NotFoundException
163
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
164
	 */
165 28
	public function rename($from, $to) {
166 28
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
167
	}
168
169
	/**
170
	 * Upload a local file
171
	 *
172
	 * @param string $source local file
173
	 * @param string $target remove file
174
	 * @return bool
175
	 *
176
	 * @throws \Icewind\SMB\Exception\NotFoundException
177
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
178
	 */
179 113
	public function put($source, $target) {
180 113
		$sourceHandle = fopen($source, 'rb');
181 113
		$targetHandle = $this->getState()->create($this->buildUrl($target));
182
183 102
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
184 102
			$this->getState()->write($targetHandle, $data);
185 102
		}
186 102
		$this->getState()->close($targetHandle);
187 102
		return true;
188
	}
189
190
	/**
191
	 * Download a remote file
192
	 *
193
	 * @param string $source remove file
194
	 * @param string $target local file
195
	 * @return bool
196
	 *
197
	 * @throws \Icewind\SMB\Exception\NotFoundException
198
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
199
	 * @throws \Icewind\SMB\Exception\InvalidPathException
200
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
201
	 */
202 44
	public function get($source, $target) {
203 44
		if (!$target) {
204 9
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
205
		}
206 35
		$targetHandle = @fopen($target, 'wb');
207 35
		if (!$targetHandle) {
208 1
			$error = error_get_last();
209 1
			if (is_array($error)) {
210 1
				$reason = $error['message'];
211 1
			} else {
212
				$reason = 'Unknown error';
213
			}
214 1
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
215
		}
216
217 34
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
218 32
		if (!$sourceHandle) {
219
			fclose($targetHandle);
220
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
221
		}
222
223 32
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
224 32
			fwrite($targetHandle, $data);
225 32
		}
226 32
		$this->getState()->close($sourceHandle);
227 32
		return true;
228
	}
229
230
	/**
231
	 * Open a readable stream top a remote file
232
	 *
233
	 * @param string $source
234
	 * @return resource a read only stream with the contents of the remote file
235
	 *
236
	 * @throws \Icewind\SMB\Exception\NotFoundException
237
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
238
	 */
239 29
	public function read($source) {
240 29
		$url = $this->buildUrl($source);
241 20
		$handle = $this->getState()->open($url, 'r');
242 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...
243
	}
244
245
	/**
246
	 * Open a readable stream top a remote file
247
	 *
248
	 * @param string $source
249
	 * @return resource a read only stream with the contents of the remote file
250
	 *
251
	 * @throws \Icewind\SMB\Exception\NotFoundException
252
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
253
	 */
254 29
	public function write($source) {
255 29
		$url = $this->buildUrl($source);
256 20
		$handle = $this->getState()->create($url);
257 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...
258
	}
259
260
	/**
261
	 * Get extended attributes for the path
262
	 *
263
	 * @param string $path
264
	 * @param string $attribute attribute to get the info
265
	 * @return string the attribute value
266
	 */
267 14
	public function getAttribute($path, $attribute) {
268 14
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
269
	}
270
271
	/**
272
	 * Get extended attributes for the path
273
	 *
274
	 * @param string $path
275
	 * @param string $attribute attribute to get the info
276
	 * @param mixed $value
277
	 * @return string the attribute value
278
	 */
279 8
	public function setAttribute($path, $attribute, $value) {
280
281 8
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
282 8
			$value = '0x' . dechex($value);
283 8
		}
284
285 8
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
286
	}
287
288
	/**
289
	 * @param string $path
290
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
291
	 * @return mixed
292
	 */
293 8
	public function setMode($path, $mode) {
294 8
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
295
	}
296
297
	/**
298
	 * @param string $path
299
	 * @return INotifyHandler
300
	 */
301
	public function notify($path) {
302
		// php-smbclient does support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
303
		// so we use the smbclient based backend for this
304
		if (!Server::available($this->server->getSystem())) {
305
			throw new DependencyException('smbclient not found in path for notify command');
306
		}
307
		$share = new Share($this->server, $this->getName());
308
		return $share->notify($path);
309
	}
310
311 255
	public function __destruct() {
312 255
		unset($this->state);
313 255
	}
314
}
315