Completed
Push — master ( 61475a...8376f2 )
by Nazar
04:08
created

File_stream::stream_open()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 10
nc 4
nop 2
1
<?php
2
/**
3
 * @package   CleverStyle CMS
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
	function stream_open ($path, $mode) {
29
		if ($mode != 'r' && $mode != 'rb') {
30
			return false;
31
		}
32
		$files = Request::instance()->files;
33
		foreach (explode('/', explode(':///', $path)[1]) as $file_path) {
34
			if (!isset($files[$file_path])) {
35
				return false;
36
			}
37
			$files = $files[$file_path];
38
		}
39
		$this->stream = $files['stream'];
40
		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
	function stream_read ($length) {
48
		return fread($this->stream, $length);
49
	}
50
	/**
51
	 * @return int
52
	 */
53
	function stream_tell () {
54
		return ftell($this->stream);
55
	}
56
	/**
57
	 * @return bool
58
	 */
59
	function stream_eof () {
60
		return feof($this->stream);
61
	}
62
	/**
63
	 * @param int $offset
64
	 * @param int $whence
65
	 *
66
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
67
	 */
68
	function stream_seek ($offset, $whence = SEEK_SET) {
69
		return fseek($this->stream, $offset, $whence);
70
	}
71
	/**
72
	 * @return array
73
	 */
74
	function stream_stat () {
75
		return fstat($this->stream);
76
	}
77
}
78