Stream::isReadable()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dazzle\Stream\Sync;
4
5
use Dazzle\Stream\StreamSeeker;
6
use Dazzle\Throwable\Exception\Runtime\ReadException;
7
use Dazzle\Throwable\Exception\Runtime\WriteException;
8
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException;
9
10
class Stream extends StreamSeeker implements StreamInterface
11
{
12
    /**
13
     * @var bool
14
     */
15
    protected $readable;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $writable;
21
22
    /**
23
     * @var int
24
     */
25
    protected $bufferSize;
26
27
    /**
28
     * @param resource $resource
29
     * @param bool $autoClose
30
     * @throws InvalidArgumentException
31
     */
32 38
    public function __construct($resource, $autoClose = true)
33
    {
34 38
        parent::__construct($resource, $autoClose);
35
36 36
        $this->readable = true;
37 36
        $this->writable = true;
38 36
        $this->bufferSize = 4096;
39 36
    }
40
41
    /**
42
     *
43
     */
44 29
    public function __destruct()
45
    {
46 29
        unset($this->readable);
47 29
        unset($this->writable);
48 29
        unset($this->bufferSize);
49
50 29
        parent::__destruct();
51 29
    }
52
53
    /**
54
     * @override
55
     * @inheritDoc
56
     */
57 2
    public function isReadable()
58
    {
59 2
        return $this->readable;
60
    }
61
62
    /**
63
     * @override
64
     * @inheritDoc
65
     */
66 2
    public function isWritable()
67
    {
68 2
        return $this->writable;
69
    }
70
71
    /**
72
     * @override
73
     * @inheritDoc
74
     */
75 1
    public function setBufferSize($bufferSize)
76
    {
77 1
        $this->bufferSize = $bufferSize;
78 1
    }
79
80
    /**
81
     * @override
82
     * @inheritDoc
83
     */
84 3
    public function getBufferSize()
85
    {
86 3
        return $this->bufferSize;
87
    }
88
89
    /**
90
     * @override
91
     * @inheritDoc
92
     */
93 2 View Code Duplication
    public function write($text = '')
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...
94
    {
95 2
        if (!$this->writable)
96
        {
97
            return $this->throwAndEmitException(
98
                new WriteException('Stream is no longer writable.')
99
            );
100
        }
101
102 2
        $sent = fwrite($this->resource, $text);
103
104 2
        if ($sent === false)
105
        {
106
            return $this->throwAndEmitException(
107
                new WriteException('Error occurred while writing to the stream resource.')
108
            );
109
        }
110
111 2
        $this->emit('drain', [ $this ]);
112 2
        $this->emit('finish', [ $this ]);
113
114 2
        return true;
115
    }
116
117
    /**
118
     * @override
119
     * @inheritDoc
120
     */
121 2 View Code Duplication
    public function read($length = null)
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...
122
    {
123 2
        if (!$this->readable)
124
        {
125
            return $this->throwAndEmitException(
126
                new ReadException('Stream is no longer readable.')
127
            );
128
        }
129
130 2
        if ($length === null)
131
        {
132 2
            $length = $this->bufferSize;
133
        }
134
135 2
        $ret = fread($this->resource, $length);
136
137 2
        if ($ret === false)
138
        {
139
            return $this->throwAndEmitException(
140
                new ReadException('Cannot read stream.')
141
            );
142
        }
143 2
        else if ($ret !== '')
144
        {
145 2
            $this->emit('data', [ $this, $ret ]);
146
147 2
            if (strlen($ret) < $length)
148
            {
149 2
                $this->emit('end', [ $this ]);
150
            }
151
        }
152
153 2
        return $ret;
154
    }
155
156
    /**
157
     * @override
158
     * @inheritDoc
159
     */
160 5 View Code Duplication
    public function close()
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...
161
    {
162 5
        if ($this->closing)
163
        {
164
            return;
165
        }
166
167 5
        $this->closing = true;
168 5
        $this->readable = false;
169 5
        $this->writable = false;
170
171 5
        $this->emit('close', [ $this ]);
172 5
        $this->handleClose();
173 5
        $this->emit('done', [ $this ]);
174 5
    }
175
}
176