Passed
Pull Request — master (#56)
by Charis
01:50
created

copy_to_string()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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