DataStream   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 2
dl 0
loc 139
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 2
A close() 0 8 3
A resource() 0 8 2
A string() 0 14 4
A object() 0 22 5
A streamWrapper() 0 19 3
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
     * Handler for resource input type
48
     *
49
     * @param mixed $input
50
     *
51
     * @throws StreamException
52
     *
53
     * @return resource
54
     */
55
    protected static function resource($input)
56
    {
57
        if (!is_resource($input)) {
58
            throw new StreamException('Invalid resource: unable to create stream.');
59
        }
60
61
        return $input;
62
    }
63
64
    /**
65
     * Handler for string input type
66
     *
67
     * @param string $input
68
     *
69
     * @throws StreamException
70
     *
71
     * @return resource
72
     */
73
    protected static function string(string $input)
74
    {
75
        if (!is_file($input)) {
76
            throw new StreamException('File does not exist: `' . $input . '`.');
77
        }
78
79
        $stream = extension_loaded('zlib') ? @gzopen($input, 'rb') : @fopen($input, 'rb');
80
81
        if ($stream === false) {
82
            throw new StreamException('Unable to open file for reading: `' . $input . '`.');
83
        }
84
85
        return $stream;
86
    }
87
88
    /**
89
     * Handler for object input type
90
     *
91
     * @param MessageInterface|StreamInterface $object
92
     *
93
     * @throws StreamException
94
     *
95
     * @return resource
96
     */
97
    protected static function object($object)
98
    {
99
        if (!($object instanceof MessageInterface || $object instanceof StreamInterface)) {
100
            throw new StreamException(
101
                'Unable to create stream from `'
102
                . get_class($object)
103
                . '`, must be one of `MessageInterface` or `StreamInterface`.'
104
            );
105
        }
106
107
        $object = $object instanceof MessageInterface ? $object->getBody() : $object;
108
109
        if (!$object instanceof StreamInterface) {
110
            throw new StreamException(
111
                'Unable to create a stream from `'
112
                . get_class($object)
113
                . '`, must be `StreamInterface`.'
114
            );
115
        }
116
117
        return static::streamWrapper($object);
118
    }
119
120
    /**
121
     * Stream content from the given stream wrapper
122
     *
123
     * @param StreamInterface $stream
124
     *
125
     * @throws StreamException
126
     *
127
     * @return resource
128
     */
129
    protected static function streamWrapper(StreamInterface $stream)
130
    {
131
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
132
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
133
        }
134
135
        $resource = @fopen(
136
            StreamWrapper::NAME . '://stream',
137
            'rb',
138
            false,
139
            stream_context_create([StreamWrapper::NAME => compact('stream')])
140
        );
141
142
        if ($resource === false) {
143
            throw new StreamException('Failed to open stream from `' . get_class($stream) . '`');
144
        }
145
146
        return $resource;
147
    }
148
}
149