createStream()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
c 0
b 0
f 0
nc 7
nop 2
dl 0
loc 28
rs 8.4444
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
use One\Http\PumpStream;
6
use One\Http\Stream;
7
use Psr\Http\Message\StreamInterface;
8
9
/**
10
 * createUriFromString
11
 * @covers FactoryUri::create
12
 */
13
function createUriFromString(string $uri): \One\Uri
14
{
15
    return FactoryUri::create($uri);
16
}
17
18
/**
19
 * createuriFromServer
20
 * @covers FactoryUri::create
21
 */
22
function createUriFromServer(): \One\Uri
23
{
24
    return FactoryUri::createFromServer();
25
}
26
27
/**
28
 * createArticleFromArray
29
 * @covers FactoryArticle::create
30
 */
31
function createArticleFromArray(array $data): \One\Model\Article
32
{
33
    return FactoryArticle::create($data);
34
}
35
36
/**
37
 * createAttachmentPhoto
38
 * @covers FactoryPhoto::create
39
 */
40
function createAttachmentPhoto(string $url, string $ratio, string $description, string $information): \One\Model\Photo
41
{
42
    return FactoryPhoto::create(
43
        [
44
            'url' => $url,
45
            'ratio' => $ratio,
46
            'description' => $description,
47
            'information' => $information,
48
        ]
49
    );
50
}
51
52
/**
53
 * createAttachmentGallery
54
 * @covers FactoryGalery::create
55
 */
56
function createAttachmentGallery(String $body, Int $order, string $photo, String $source, String $lead = ''): \One\Model\Gallery
57
{
58
    return FactoryGallery::create(
59
        [
60
            'body' => $body,
61
            'order' => $order,
62
            'photo' => $photo,
63
            'source' => $source,
64
            'lead' => $lead,
65
        ]
66
    );
67
}
68
69
/**
70
 * Create a new stream based on the input type.
71
 *
72
 * Options is an associative array that can contain the following keys:
73
 * - metadata: Array of custom metadata.
74
 * - size: Size of the stream.
75
 *
76
 * @param mixed $resource Entity body data
77
 * @param array $options Additional options
78
 *
79
 * @throws \InvalidArgumentException if the $resource arg is not valid.
80
 */
81
function stream_for($resource = '', $options = []): StreamInterface
82
{
83
    if (is_scalar($resource)) {
84
        return openStream($resource, $options);
85
    }
86
87
    return createStream($resource, $options);
88
}
89
90
/**
91
 * Helper to create stream based on resource and options
92
 * @param mixed $resource
93
 * @param  array $options
94
 * @throws \InvalidArgumentException if the $resource arg is not valid.
95
 */
96
function createStream($resource, $options): StreamInterface
97
{
98
    switch (gettype($resource)) {
99
        case 'resource':
100
            return new Stream($resource, $options);
101
        case 'object':
102
            if ($resource instanceof StreamInterface) {
103
                return $resource;
104
            } elseif (method_exists($resource, '__toString')) {
105
                return stream_for((string) $resource, $options);
106
            }
107
            return new PumpStream(function () use ($resource) {
108
                if (! $resource->valid()) {
109
                    return false;
110
                }
111
                $result = $resource->current();
112
                $resource->next();
113
                return $result;
114
            }, $options);
115
        case 'NULL':
116
            return new Stream(fopen('php://temp', 'r+'), $options);
117
    }
118
119
    if (is_callable($resource)) {
120
        return new \One\Http\PumpStream($resource, $options);
121
    }
122
123
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
124
}
125
126
/**
127
 * Copy the contents of a stream into a string until the given number of
128
 * bytes have been read.
129
 *
130
 * @param StreamInterface $stream Stream to read
131
 * @param int             $maxLen Maximum number of bytes to read. Pass -1
132
 *                                to read the entire stream.
133
 * @throws \RuntimeException on error.
134
 */
135
function copy_to_string(StreamInterface $stream, int $maxLen = -1): string
136
{
137
    $buffer = '';
138
    if ($maxLen === -1) {
139
        while (! $stream->eof()) {
140
            $buf = $stream->read(1048576);
141
            // Using a loose equality here to match on '' and false.
142
            if ($buf === null) {
143
                break;
144
            }
145
            $buffer .= $buf;
146
        }
147
        return $buffer;
148
    }
149
150
    $len = 0;
151
    while (! $stream->eof() && $len < $maxLen) {
152
        $buf = $stream->read($maxLen - $len);
153
        // Using a loose equality here to match on '' and false.
154
        if ($buf === null) {
155
            break;
156
        }
157
        $buffer .= $buf;
158
        $len = strlen($buffer);
159
    }
160
    return $buffer;
161
}
162
163
/**
164
 * Open Stream when resource is a scalar type
165
 * @param mixed $resource
166
 * @param array $options
167
 */
168
function openStream($resource, $options): StreamInterface
169
{
170
    $stream = fopen('php://temp', 'r+');
171
    if ($resource !== '' && $stream !== false) {
172
        fwrite($stream, $resource);
173
        fseek($stream, 0);
174
    }
175
    return new Stream($stream, $options);
176
}
177