Completed
Push — master ( 22c8c1...ac0588 )
by Nazar
07:03
created

File_stream::stream_seek()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
use
10
	cs\Request;
11
12
/**
13
 * Stream wrapper created in order to be used as virtual stream which actually represents uploaded file, which is only available in form of stream, but we
14
 * sometimes need to handle it like regular file
15
 *
16
 * Usage:
17
 * * `fopen('request-file:///file', 'r')`
18
 * * `fopen('request-file:///file/0', 'r')`
19
 *
20
 * `/file` basically represents `cs\Request::files['file']` and `/file/0` `cs\Request::files['file'][0]`
21
 */
22
class File_stream {
23
	/**
24
	 * @var resource
25
	 */
26
	protected $stream;
27
28 2
	function stream_open ($path, $mode) {
29 2
		if ($mode != 'r' && $mode != 'rb') {
30
			return false;
31
		}
32 2
		$files = Request::instance()->files;
33 2
		foreach (explode('/', explode(':///', $path)[1]) as $file_path) {
34 2
			if (!isset($files[$file_path])) {
35
				return false;
36
			}
37 2
			$files = $files[$file_path];
38
		}
39 2
		$this->stream = $files['stream'];
40 2
		return true;
41
	}
42
	/**
43
	 * @param int $length
44
	 *
45
	 * @return false|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
46
	 */
47 2
	function stream_read ($length) {
48 2
		return fread($this->stream, $length);
49
	}
50
	/**
51
	 * @return false|int
52
	 */
53
	function stream_tell () {
54
		return ftell($this->stream);
55
	}
56
	/**
57
	 * @return bool
58
	 */
59 2
	function stream_eof () {
60 2
		return feof($this->stream);
61
	}
62
	/**
63
	 * @param int $offset
64
	 * @param int $whence
65
	 *
66
	 * @return int
67
	 */
68
	function stream_seek ($offset, $whence = SEEK_SET) {
69
		return fseek($this->stream, $offset, $whence);
70
	}
71
	/**
72
	 * @return array
73
	 */
74 2
	function stream_stat () {
75 2
		return fstat($this->stream);
76
	}
77
}
78