Completed
Push — master ( 5693b6...af7f32 )
by Robin
01:54
created

RetryWrapper::stream_read()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2016 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\Streams;
9
10
/**
11
 * Wrapper that retries reads/writes to remote streams that dont deliver/recieve all requested data at once
12
 */
13
class RetryWrapper extends Wrapper {
14
15
	/**
16
	 * Wraps a stream with the provided callbacks
17
	 *
18
	 * @param resource $source
19
	 * @return resource
20
	 */
21 View Code Duplication
	public static function wrap($source) {
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...
22
		$context = stream_context_create(array(
23
			'retry' => array(
24
				'source' => $source
25
			)
26
		));
27
		return Wrapper::wrapSource($source, $context, 'retry', '\Icewind\Streams\RetryWrapper');
28
	}
29
30
	protected function open() {
31
		$this->loadContext('retry');
32
		return true;
33
	}
34
35
	public function dir_opendir($path, $options) {
36
		return false;
37
	}
38
39
	public function stream_open($path, $mode, $options, &$opened_path) {
40
		return $this->open();
41
	}
42
43 View Code Duplication
	public function stream_read($count) {
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...
44
		$result = parent::stream_read($count);
45
46
		$bytesReceived = strlen($result);
47
		while ($bytesReceived < $count && !$this->stream_eof()) {
48
			$result .= parent::stream_read($count - $bytesReceived);
49
			$bytesReceived = strlen($result);
50
		}
51
52
		return $result;
53
	}
54
55 View Code Duplication
	public function stream_write($data) {
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...
56
		$bytesToSend = strlen($data);
57
		$result = parent::stream_write($data);
58
59
		while ($result < $bytesToSend && !$this->stream_eof()) {
60
			$dataLeft = substr($data, $result);
61
			$result += parent::stream_write($dataLeft);
62
		}
63
64
		return $result;
65
	}
66
}
67