Completed
Push — master ( 037efd...b5c692 )
by Robin
01:56 queued 11s
created

NativeStream::stream_close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
		stream_wrapper_register('nativesmb', $class);
56
		$context = stream_context_create([
57
			'nativesmb' => [
58
				'state'  => $state,
59
				'handle' => $smbStream,
60
				'url'    => $url
61
			]
62
		]);
63 78
		$fh = fopen('nativesmb://', $mode, false, $context);
64
		stream_wrapper_unregister('nativesmb');
65 78
		return $fh;
66
	}
67
68
	public function stream_close() {
69
		try {
70
			return $this->state->close($this->handle, $this->url);
71 38
		} catch (\Exception $e) {
72 38
			return false;
73
		}
74
	}
75 38
76 38
	public function stream_eof() {
77
		return $this->eof;
78
	}
79 78
80 78
	public function stream_flush() {
81 78
		return false;
82 78
	}
83 78
84 78
85
	public function stream_open($path, $mode, $options, &$opened_path) {
86
		$context = stream_context_get_options($this->context);
87 38
		if (!isset($context['nativesmb']) || !is_array($context['nativesmb'])) {
88 38
			throw new InvalidArgumentException("context not set");
89 38
		}
90 38
		$state = $context['nativesmb']['state'];
91
		if (!$state instanceof NativeState) {
92 38
			throw new InvalidArgumentException("invalid context set");
93
		}
94
		$this->state = $state;
95 4
		$handle = $context['nativesmb']['handle'];
96 4
		if (!is_resource($handle)) {
97
			throw new InvalidArgumentException("invalid context set");
98 4
		}
99
		$this->handle = $handle;
100
		$url = $context['nativesmb']['url'];
101
		if (!is_string($url)) {
102
			throw new InvalidArgumentException("invalid context set");
103
		}
104 36
		$this->url = $url;
105
		return true;
106 36
	}
107
108
	public function stream_read($count) {
109
		$result = $this->state->read($this->handle, $count, $this->url);
110
		if (strlen($result) < $count) {
111
			$this->eof = true;
112 4
		}
113 4
		return $result;
114
	}
115
116
	public function stream_seek($offset, $whence = SEEK_SET) {
117
		$this->eof = false;
118
		try {
119
			return $this->state->lseek($this->handle, $offset, $whence, $this->url) !== false;
120 2
		} catch (InvalidRequestException $e) {
121 2
			return false;
122
		}
123
	}
124 2
125 2
	/**
126
	 * @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...
127
	 */
128 2
	public function stream_stat() {
129 2
		try {
130
			return $this->state->stat($this->url);
131
		} catch (Exception $e) {
132
			return false;
133
		}
134
	}
135
136
	public function stream_tell() {
137
		return $this->state->lseek($this->handle, 0, SEEK_CUR, $this->url);
138
	}
139
140
	public function stream_write($data) {
141
		return $this->state->write($this->handle, $data, $this->url);
142
	}
143
144
	public function stream_truncate($size) {
145
		return $this->state->ftruncate($this->handle, $size, $this->url);
146
	}
147
148
	public function stream_set_option($option, $arg1, $arg2) {
149
		return false;
150
	}
151
152
	public function stream_lock($operation) {
153
		return false;
154
	}
155
}
156