StreamWrapper::stream_eof()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class StreamWrapper
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class StreamWrapper
14
{
15
    /**
16
     * @var resource
17
     */
18
    public $context;
19
20
    /**
21
     * @var StreamInterface
22
     */
23
    private $stream;
24
25
    /**
26
     * @var string r, r+, or w
27
     */
28
    private $mode;
29
30
    /**
31
     * Returns a resource representing the stream.
32
     *
33
     * @param StreamInterface $stream The stream to get a resource for
34
     *
35
     * @return resource
36
     * @throws \InvalidArgumentException if stream is not readable or writable
37
     */
38 4
    public static function getResource(StreamInterface $stream)
39
    {
40 4
        self::register();
41
42 4
        if ($stream->isReadable()) {
43 2
            $mode = $stream->isWritable() ? 'r+' : 'r';
44 2
        } elseif ($stream->isWritable()) {
45 1
            $mode = 'w';
46
        } else {
47 1
            throw new \InvalidArgumentException('The stream must be readable, '
48 1
                . 'writable, or both.');
49
        }
50
51 3
        return fopen('thttp://stream', $mode, null, stream_context_create([
52 3
            'thttp' => ['stream' => $stream]
53
        ]));
54
    }
55
56
    /**
57
     * Registers the stream wrapper if needed
58
     */
59 4
    public static function register()
60
    {
61 4
        if (!in_array('thttp', stream_get_wrappers())) {
62 1
            stream_wrapper_register('thttp', __CLASS__);
63
        }
64 4
    }
65
66 4
    public function stream_open($path, $mode, $options, &$opened_path)
0 ignored issues
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...
Unused Code introduced by
The parameter $options 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...
Unused Code introduced by
The parameter $opened_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...
67
    {
68 4
        $options = stream_context_get_options($this->context);
69
70 4
        if (false === isset($options['thttp']['stream'])) {
71 1
            return false;
72
        }
73
74 3
        $this->mode = $mode;
75 3
        $this->stream = $options['thttp']['stream'];
76
77 3
        return true;
78
    }
79
80 2
    public function stream_read(int $count)
81
    {
82 2
        return $this->stream->read($count);
83
    }
84
85 1
    public function stream_write($data)
86
    {
87 1
        return (int) $this->stream->write($data);
88
    }
89
90 2
    public function stream_tell()
91
    {
92 2
        return $this->stream->tell();
93
    }
94
95 2
    public function stream_eof()
96
    {
97 2
        return $this->stream->eof();
98
    }
99
100 2
    public function stream_seek($offset, $whence)
101
    {
102 2
        $this->stream->seek($offset, $whence);
103
104 2
        return true;
105
    }
106
107 1
    public function stream_stat()
108
    {
109 1
        static $modeMap = [
110
            'r'  => 33060,
111
            'r+' => 33206,
112
            'w'  => 33188
113
        ];
114
115
        return [
116 1
            'dev'     => 0,
117 1
            'ino'     => 0,
118 1
            'mode'    => $modeMap[$this->mode],
119 1
            'nlink'   => 0,
120 1
            'uid'     => 0,
121 1
            'gid'     => 0,
122 1
            'rdev'    => 0,
123 1
            'size'    => $this->stream->getSize() ?: 0,
124 1
            'atime'   => 0,
125 1
            'mtime'   => 0,
126 1
            'ctime'   => 0,
127 1
            'blksize' => 0,
128 1
            'blocks'  => 0
129
        ];
130
    }
131
}
132