Completed
Pull Request — master (#80)
by Aydin
01:50
created

ResourceOutputStream::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 6
nop 1
1
<?php
2
3
namespace PhpSchool\CliMenu\IO;
4
5
use function is_resource;
6
use function get_resource_type;
7
use function stream_get_meta_data;
8
use function strpos;
9
10
/**
11
 * @author Aydin Hassan <[email protected]>
12
 */
13
class ResourceOutputStream implements OutputStream
14
{
15
    /**
16
     * @var resource
17
     */
18
    private $stream;
19
20 View Code Duplication
    public function __construct($stream = 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...
21
    {
22
        if ($stream === null) {
23
            $stream = STDOUT;
24
        }
25
26
        if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
27
            throw new \InvalidArgumentException('Expected a valid stream');
28
        }
29
30
        $meta = stream_get_meta_data($stream);
31
        if (strpos($meta['mode'], 'r') !== false && strpos($meta['mode'], '+') === false) {
32
            throw new \InvalidArgumentException('Expected a writable stream');
33
        }
34
35
        $this->stream = $stream;
36
    }
37
38
    public function write(string $buffer): void
39
    {
40
        fwrite($this->stream, $buffer);
41
    }
42
}
43