Completed
Push — master ( 7ed063...7400f3 )
by Andrea Marco
05:20 queued 12s
created

DataStreaming   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 103
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A streamData() 0 9 2
A streamResource() 0 7 2
A streamObject() 0 11 3
A streamString() 0 9 3
A streamWrapper() 0 17 3
1
<?php
2
3
namespace Cerbero\JsonObjects;
4
5
use Psr\Http\Message\MessageInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * The data streaming.
10
 *
11
 */
12
class DataStreaming
13
{
14
    /**
15
     * Stream content from the given data.
16
     *
17
     * @param mixed $data
18
     * @return resource
19
     *
20
     * @throws JsonObjectsException
21
     */
22 36
    public function streamData($data)
23
    {
24 36
        $method = 'stream' . ucfirst(gettype($data));
25
26 36
        if (method_exists($this, $method)) {
27 30
            return $this->{$method}($data);
28
        }
29
30 6
        throw new JsonObjectsException('Unable to create a stream from the given data.');
31
    }
32
33
    /**
34
     * Stream content from the given resource
35
     *
36
     * @param mixed $resource
37
     * @return resource
38
     *
39
     * @throws JsonObjectsException
40
     */
41 12
    public function streamResource($resource)
42
    {
43 12
        if (is_resource($resource)) {
44 9
            return $resource;
45
        }
46
47 3
        throw new JsonObjectsException('Unable to create a stream from an invalid resource.');
48
    }
49
50
    /**
51
     * Stream content from the given string
52
     *
53
     * @param string $string
54
     * @return resource
55
     *
56
     * @throws JsonObjectsException
57
     */
58 33
    public function streamString(string $string)
59
    {
60 33
        $stream = extension_loaded('zlib') ? @gzopen($string, 'rb') : @fopen($string, 'rb');
61
62 33
        if ($stream === false) {
63 3
            throw new JsonObjectsException("Failed to open stream from: {$string}");
64
        }
65
66 30
        return $stream;
67
    }
68
69
    /**
70
     * Stream content from the given object
71
     *
72
     * @param object $object
73
     * @return resource
74
     *
75
     * @throws JsonObjectsException
76
     */
77 12
    public function streamObject(object $object)
78
    {
79 12
        if ($object instanceof MessageInterface) {
80 3
            $object = $object->getBody();
81
        }
82
83 12
        if ($object instanceof StreamInterface) {
84 9
            return $this->streamWrapper($object);
85
        }
86
87 3
        throw new JsonObjectsException('Unable to create a stream from ' . get_class($object));
88
    }
89
90
    /**
91
     * Stream content from the given stream wrapper
92
     *
93
     * @param \Psr\Http\Message\StreamInterface $stream
94
     * @return resource
95
     *
96
     * @throws JsonObjectsException
97
     */
98 9
    public function streamWrapper(StreamInterface $stream)
99
    {
100
        // Register the stream wrapper if not already registered
101 9
        if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) {
102 3
            stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class);
103
        }
104
105
        // Retrieve a handler of the opened stream
106 9
        $resource = @fopen(StreamWrapper::NAME . '://stream', 'rb', false, stream_context_create([
107 9
            StreamWrapper::NAME => compact('stream'),
108
        ]));
109
110 9
        if ($resource === false) {
111 3
            throw new JsonObjectsException('Failed to open stream from ' . get_class($stream));
112
        }
113
114 6
        return $resource;
115
    }
116
}
117