Completed
Push — master ( 2ce414...a1c75f )
by Max
13s queued 11s
created

DataStream::close()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JsonCollectionParser\Stream;
6
7
use Psr\Http\Message\MessageInterface;
8
use Psr\Http\Message\StreamInterface;
9
10
class DataStream
11
{
12
    /**
13
     * @param mixed $input
14
     *
15
     * @throws StreamException
16
     *
17
     * @return resource
18
     */
19
    public static function get($input)
20
    {
21
        $type = gettype($input);
22
23
        if (method_exists(static::class, $type)) {
24
            return static::{$type}($input);
25
        }
26
27
        throw new StreamException(
28
            'Unable to create a stream from given input, must be one of [`object`, `resource`, `string`].'
29
        );
30
    }
31
32
    /**
33
     * @param resource $stream
34
     *
35
     * @return bool|null
36
     */
37
    public static function close($stream): ?bool
38
    {
39
        if (!is_resource($stream)) {
40
            return null;
41
        }
42
43
        return extension_loaded('zlib') ? gzclose($stream) : fclose($stream);
44
    }
45
46
    /**
47
     * @param mixed $input
48
     *
49
     * @throws StreamException
50
     *
51
     * @return resource
52
     */
53
    protected static function resource($input)
54
    {
55
        if (!is_resource($input)) {
56
            throw new StreamException('Invalid resource: unable to create stream.');
57
        }
58
59
        return $input;
60
    }
61
62
    /**
63
     * @param string $input
64
     *
65
     * @throws StreamException
66
     *
67
     * @return resource
68
     */
69
    protected static function string(string $input)
70
    {
71
        if (!is_file($input)) {
72
            throw new StreamException('File does not exist: `' . $input . '`.');
73
        }
74
75
        $stream = extension_loaded('zlib') ? @gzopen($input, 'rb') : @fopen($input, 'rb');
76
77
        if ($stream === false) {
78
            throw new StreamException('Unable to open file for reading: `' . $input . '`.');
79
        }
80
81
        return $stream;
82
    }
83
84
    /**
85
     * @param MessageInterface|StreamInterface $object
86
     *
87
     * @throws StreamException
88
     *
89
     * @return resource
90
     */
91
    protected static function object($object)
92
    {
93
        if (!($object instanceof MessageInterface || $object instanceof StreamInterface)) {
94
            throw new StreamException(
95
                'Unable to create stream from `'
96
                . get_class($object)
97
                . '`, must be one of `MessageInterface` or `StreamInterface`.'
98
            );
99
        }
100
101
        $object = $object instanceof MessageInterface ? $object->getBody() : $object;
102
103
        if (!$object instanceof StreamInterface) {
104
            throw new StreamException(
105
                'Unable to create a stream from `'
106
                . get_class($object)
107
                . '`, must be `StreamInterface`.'
108
            );
109
        }
110
111
        return static::streamWrapper($object);
112
    }
113
114
    /**
115
     * Stream content from the given stream wrapper
116
     *
117
     * @param StreamInterface $stream
118
     *
119
     * @throws StreamException
120
     *
121
     * @return resource
122
     */
123
    protected static function streamWrapper(StreamInterface $stream)
124
    {
125
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
126
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
127
        }
128
129
        $resource = @fopen(
130
            StreamWrapper::NAME . '://stream',
131
            'rb',
132
            false,
133
            stream_context_create([StreamWrapper::NAME => compact('stream')])
134
        );
135
136
        if ($resource === false) {
137
            throw new StreamException('Failed to open stream from `' . get_class($stream) . '`');
138
        }
139
140
        return $resource;
141
    }
142
}
143