Completed
Push — master ( 0492d9...7c4761 )
by Nazar
04:01
created

Psr7_data_stream::stream_stat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
	Exception;
11
12
/**
13
 * Stream wrapper created in order to be used as virtual stream which actually represents PSR7 Psr\Http\Message\StreamInterface, but in form that can be used
14
 * in `fopen()`, this is all useful in order to avoid copying entire stream from PSR7 representation into regular PHP stream
15
 *
16
 * Usage: `fopen('request-psr7-data://', 'r')`
17
 */
18
class Psr7_data_stream {
19
	/**
20
	 * PSR7 request body stream is injected here
21
	 *
22
	 * @var \Psr\Http\Message\StreamInterface
23
	 */
24
	public static $stream;
25
	/**
26
	 * @var array
27
	 */
28
	protected $stat = [];
29
30
	function stream_open ($path, $mode) {
1 ignored issue
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
		if ($mode != 'r' && $mode != 'rb') {
32
			return false;
33
		}
34
		$stat     = &$this->stat;
35
		$stat[0]  = $stat['dev'] = 0;
36
		$stat[1]  = $stat['ino'] = 0;
37
		$stat[2]  = $stat['mode'] = 0;
38
		$stat[3]  = $stat['nlink'] = 0;
39
		$stat[4]  = $stat['uid'] = 0;
40
		$stat[5]  = $stat['gid'] = 0;
41
		$stat[6]  = $stat['rdev'] = 0;
42
		$stat[7]  = $stat['size'] = static::$stream->getSize();
43
		$time     = time();
44
		$stat[8]  = $stat['atime'] = $time;
45
		$stat[9]  = $stat['mtime'] = $time;
46
		$stat[10] = $stat['ctime'] = $time;
47
		$stat[11] = $stat['blksize'] = -1;
48
		$stat[12] = $stat['blocks'] = -1;
49
		return true;
50
	}
51
	/**
52
	 * @param int $length
53
	 *
54
	 * @return false|string
55
	 */
56
	function stream_read ($length) {
57
		try {
58
			return static::$stream->read($length);
59
		} catch (Exception $e) {
60
			return false;
61
		}
62
	}
63
	/**
64
	 * @return false|int
65
	 */
66
	function stream_tell () {
67
		try {
68
			return static::$stream->tell();
69
		} catch (Exception $e) {
70
			return false;
71
		}
72
	}
73
	/**
74
	 * @return bool
75
	 */
76
	function stream_eof () {
77
		return static::$stream->eof();
78
	}
79
	/**
80
	 * @param int $offset
81
	 * @param int $whence
82
	 *
83
	 * @return int
84
	 */
85
	function stream_seek ($offset, $whence = SEEK_SET) {
86
		try {
87
			return static::$stream->seek($offset, $whence);
88
		} catch (Exception $e) {
89
			return -1;
90
		}
91
	}
92
	/**
93
	 * @return array
94
	 */
95
	function stream_stat () {
96
		return $this->stat;
97
	}
98
}
99