NativeStream::stream_stat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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\Exception\Exception;
11
use Icewind\SMB\Exception\InvalidRequestException;
12
use Icewind\Streams\File;
13
use InvalidArgumentException;
14
15
abstract class NativeStream implements File {
16
	/**
17
	 * @var resource
18
	 * @psalm-suppress PropertyNotSetInConstructor
19
	 */
20
	public $context;
21
22
	/**
23
	 * @var NativeState
24
	 * @psalm-suppress PropertyNotSetInConstructor
25
	 */
26
	protected $state;
27
28
	/**
29
	 * @var resource
30
	 * @psalm-suppress PropertyNotSetInConstructor
31
	 */
32
	protected $handle;
33
34
	/**
35
	 * @var bool
36
	 */
37
	protected $eof = false;
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $url = '';
43
44
	/**
45
	 * Wrap a stream from libsmbclient-php into a regular php stream
46
	 *
47
	 * @param NativeState $state
48
	 * @param resource $smbStream
49
	 * @param string $mode
50
	 * @param string $url
51
	 * @param class-string<NativeStream> $class
0 ignored issues
show
Documentation introduced by
The doc-type class-string<NativeStream> could not be parsed: Unknown type name "class-string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
52
	 * @return resource
53
	 */
54
	protected static function wrapClass(NativeState $state, $smbStream, string $mode, string $url, string $class) {
55
		if (stream_wrapper_register('nativesmb', $class) === false) {
56
			throw new Exception("Failed to register stream wrapper");
57
		}
58
		$context = stream_context_create([
59
			'nativesmb' => [
60
				'state'  => $state,
61
				'handle' => $smbStream,
62
				'url'    => $url
63 78
			]
64
		]);
65 78
		$fh = fopen('nativesmb://', $mode, false, $context);
66
		if (stream_wrapper_unregister('nativesmb') === false) {
67
			throw new Exception("Failed to unregister stream wrapper");
68
		}
69
		return $fh;
70
	}
71 38
72 38
	public function stream_close() {
73
		try {
74
			return $this->state->close($this->handle, $this->url);
75 38
		} catch (\Exception $e) {
76 38
			return false;
77
		}
78
	}
79 78
80 78
	public function stream_eof() {
81 78
		return $this->eof;
82 78
	}
83 78
84 78
	public function stream_flush() {
85
		return false;
86
	}
87 38
88 38
89 38
	public function stream_open($path, $mode, $options, &$opened_path) {
90 38
		$context = stream_context_get_options($this->context);
91
		if (!isset($context['nativesmb']) || !is_array($context['nativesmb'])) {
92 38
			throw new InvalidArgumentException("context not set");
93
		}
94
		$state = $context['nativesmb']['state'];
95 4
		if (!$state instanceof NativeState) {
96 4
			throw new InvalidArgumentException("invalid context set");
97
		}
98 4
		$this->state = $state;
99
		$handle = $context['nativesmb']['handle'];
100
		if (!is_resource($handle)) {
101
			throw new InvalidArgumentException("invalid context set");
102
		}
103
		$this->handle = $handle;
104 36
		$url = $context['nativesmb']['url'];
105
		if (!is_string($url)) {
106 36
			throw new InvalidArgumentException("invalid context set");
107
		}
108
		$this->url = $url;
109
		return true;
110
	}
111
112 4
	public function stream_read($count) {
113 4
		$result = $this->state->read($this->handle, $count, $this->url);
114
		if (strlen($result) < $count) {
115
			$this->eof = true;
116
		}
117
		return $result;
118
	}
119
120 2
	public function stream_seek($offset, $whence = SEEK_SET) {
121 2
		$this->eof = false;
122
		try {
123
			return $this->state->lseek($this->handle, $offset, $whence, $this->url) !== false;
124 2
		} catch (InvalidRequestException $e) {
125 2
			return false;
126
		}
127
	}
128 2
129 2
	/**
130
	 * @return array{"mtime": int, "size": int, "mode": int}|false
0 ignored issues
show
Documentation introduced by
The doc-type array{"mtime": could not be parsed: Unknown type name "array{"mtime":" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
131
	 */
132
	public function stream_stat() {
133
		try {
134
			return $this->state->stat($this->url);
135
		} catch (Exception $e) {
136
			return false;
137
		}
138
	}
139
140
	public function stream_tell() {
141
		return $this->state->lseek($this->handle, 0, SEEK_CUR, $this->url);
142
	}
143
144
	public function stream_write($data) {
145
		return $this->state->write($this->handle, $data, $this->url);
146
	}
147
148
	public function stream_truncate($size) {
149
		return $this->state->ftruncate($this->handle, $size, $this->url);
150
	}
151
152
	public function stream_set_option($option, $arg1, $arg2) {
153
		return false;
154
	}
155
156
	public function stream_lock($operation) {
157
		return false;
158
	}
159
}
160