Completed
Push — master ( 78b032...4251cd )
by Robin
01:40
created

NativeReadStream::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\StringBuffer;
11
12
/**
13
 * Stream optimized for read only usage
14
 */
15
class NativeReadStream extends NativeStream {
16
	const CHUNK_SIZE = 1048576; // 1MB chunks
17
18
	/** @var StringBuffer */
19
	private $readBuffer;
20
21
	public function __construct() {
22
		$this->readBuffer = new StringBuffer();
23
	}
24 40
25 40
	/** @var int */
26
	private $pos = 0;
27 40
28
	public function stream_open($path, $mode, $options, &$opened_path) {
29
		return parent::stream_open($path, $mode, $options, $opened_path);
30
	}
31
32
	/**
33
	 * Wrap a stream from libsmbclient-php into a regular php stream
34
	 *
35
	 * @param NativeState $state
36
	 * @param resource $smbStream
37
	 * @param string $mode
38
	 * @param string $url
39 40
	 * @return resource
40 40
	 */
41 40
	public static function wrap($state, $smbStream, $mode, $url) {
42
		stream_wrapper_register('nativesmb', NativeReadStream::class);
43 40
		$context = stream_context_create([
44 40
			'nativesmb' => [
45 40
				'state'  => $state,
46
				'handle' => $smbStream,
47
				'url'    => $url
48 40
			]
49 40
		]);
50 40
		$fh = fopen('nativesmb://', $mode, false, $context);
51
		stream_wrapper_unregister('nativesmb');
52
		return $fh;
53 38
	}
54
55
	public function stream_read($count) {
56
		// php reads 8192 bytes at once
57 38
		// however due to network latency etc, it's faster to read in larger chunks
58 38
		// and buffer the result
59 38
		if (!parent::stream_eof() && $this->readBuffer->remaining() < $count) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (stream_eof() instead of stream_read()). Are you sure this is correct? If so, you might want to change this to $this->stream_eof().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
60 38
			$chunk = parent::stream_read(self::CHUNK_SIZE);
61 38
			if ($chunk === false) {
62 38
				return false;
63 38
			}
64
			$this->readBuffer->push($chunk);
65
		}
66 38
67 38
		$result = $this->readBuffer->read($count);
68
69 38
		$read = strlen($result);
70 38
		$this->pos += $read;
71
72 38
		return $result;
73
	}
74
75 2 View Code Duplication
	public function stream_seek($offset, $whence = SEEK_SET) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 2
		$result = parent::stream_seek($offset, $whence);
77 2
		if ($result) {
78 2
			$this->readBuffer->clear();
79 2
			$pos = parent::stream_tell();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (stream_tell() instead of stream_seek()). Are you sure this is correct? If so, you might want to change this to $this->stream_tell().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
80 2
			if ($pos === false) {
81
				return false;
82 2
			}
83
			$this->pos = $pos;
84
		}
85 38
		return $result;
86 38
	}
87
88
	public function stream_eof() {
89 2
		return $this->readBuffer->remaining() <= 0 && parent::stream_eof();
90 2
	}
91
92
	public function stream_tell() {
93
		return $this->pos;
94
	}
95
96
	public function stream_write($data) {
97
		return false;
98
	}
99
100
	public function stream_truncate($size) {
101
		return false;
102
	}
103
}
104