Completed
Push — master ( b3a060...a91f17 )
by Maik
02:01
created

StandardOutputStream::flush()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 0
crap 3
1
<?php
2
/**
3
 * This file is part of the PHP Generics package.
4
 *
5
 * @package Generics
6
 */
7
namespace Generics\Streams;
8
9
use Generics\Streams\Interceptor\StreamInterceptor;
10
use Countable;
11
12
/**
13
 * This class provides a stream for standard output
14
 *
15
 * @author Maik Greubel <[email protected]>
16
 */
17
class StandardOutputStream implements OutputStream
18
{
19
20
    /**
21
     * Interceptor
22
     * 
23
     * @var StreamInterceptor
24
     */
25
    private $interceptor;
26
27
    /**
28
     * The standard out channel
29
     * 
30
     * @var resource
31
     */
32
    private $stdout;
33
34
    /**
35
     * Create a new instance of StandardOutputStream
36
     */
37 1
    public function __construct()
38
    {
39 1
        $this->open();
40 1
    }
41
42
    /**
43
     * Opens a new standard output channel
44
     */
45 1
    private function open()
46
    {
47 1
        $this->stdout = fopen("php://stdout", "w");
48 1
    }
49
50
    /**
51
     *
52
     * {@inheritdoc}
53
     * @see \Generics\Streams\Stream::isOpen()
54
     */
55 1
    public function isOpen(): bool
56
    {
57 1
        return is_resource($this->stdout);
58
    }
59
60
    /**
61
     *
62
     * {@inheritdoc}
63
     * @see \Generics\Streams\OutputStream::flush()
64
     */
65 1
    public function flush()
66
    {
67 1
        if ($this->isOpen()) {
68 1
            fflush($this->stdout);
69
        }
70 1
        if ($this->interceptor instanceof StreamInterceptor) {
71 1
            $this->interceptor->reset();
72
        }
73 1
    }
74
75
    /**
76
     *
77
     * {@inheritdoc}
78
     * @see \Generics\Streams\Stream::ready()
79
     */
80 1
    public function ready(): bool
81
    {
82 1
        return $this->isOpen();
83
    }
84
85
    /**
86
     *
87
     * {@inheritdoc}
88
     * @see Countable::count()
89
     */
90 1
    public function count()
91
    {
92 1
        return 0;
93
    }
94
95
    /**
96
     *
97
     * {@inheritdoc}
98
     * @see \Generics\Resettable::reset()
99
     */
100 1
    public function reset()
101
    {
102 1
        $this->close();
103 1
        $this->open();
104
        
105 1
        if ($this->interceptor instanceof StreamInterceptor) {
106 1
            $this->interceptor->reset();
107 1
            $this->setInterceptor($this->interceptor);
108
        }
109 1
    }
110
111
    /**
112
     *
113
     * {@inheritdoc}
114
     * @see \Generics\Streams\OutputStream::write()
115
     */
116 1
    public function write($buffer)
117
    {
118 1
        if ($this->isWriteable()) {
119 1
            fwrite($this->stdout, $buffer);
120
        }
121 1
    }
122
123
    /**
124
     *
125
     * {@inheritdoc}
126
     * @see \Generics\Streams\Stream::close()
127
     */
128 1
    public function close()
129
    {
130 1
        if ($this->isOpen()) {
131 1
            fclose($this->stdout);
132 1
            $this->stdout = null;
133
        }
134 1
    }
135
136
    /**
137
     *
138
     * {@inheritdoc}
139
     * @see \Generics\Streams\OutputStream::isWriteable()
140
     */
141 1
    public function isWriteable(): bool
142
    {
143 1
        return $this->isOpen();
144
    }
145
146
    /**
147
     * Apply a stream interceptor
148
     *
149
     * @param StreamInterceptor $interceptor
150
     */
151 1
    public function setInterceptor(StreamInterceptor $interceptor)
152
    {
153 1
        $this->interceptor = $interceptor;
154 1
        stream_filter_append($this->stdout, $interceptor->getFilterName());
155
    }
156
}