Passed
Pull Request — psr-implementation (#45)
by kenny
01:31
created

stream_for()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
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
 *
12
 * @param string $uri
13
 * @return \Psr\Http\Message\UriInterface
14
 */
15
function createUriFromString($uri)
16
{
17
    $parts    = parse_url($uri);
18
    $scheme   = isset($parts['scheme']) ? $parts['scheme'] : '';
19
    $user     = isset($parts['user']) ? $parts['user'] : '';
20
    $pass     = isset($parts['pass']) ? $parts['pass'] : '';
21
    $host     = isset($parts['host']) ? $parts['host'] : '';
22
    $port     = isset($parts['port']) ? $parts['port'] : null;
23
    $path     = isset($parts['path']) ? $parts['path'] : '';
24
    $query    = isset($parts['query']) ? $parts['query'] : '';
25
    $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
26
    return new Uri(
27
        $scheme,
28
        $host,
29
        $port,
30
        $path,
31
        $query,
32
        $fragment,
33
        $user,
34
        $pass
35
    );
36
}
37
38
/**
39
 * createuriFromServer
40
 *
41
 * @return \Psr\Http\Message\UriInterface
42
 */
43
44
function createuriFromServer()
45
{
46
    $scheme   = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
47
    $host     = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
48
    $port     = empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
49
    $path     = (string) parse_url('http://www.example.com/' . $_SERVER['REQUEST_URI'], PHP_URL_PATH);
50
    $query    = empty($_SERVER['QUERY_STRING']) ? parse_url('http://example.com' . $_SERVER['REQUEST_URI'], PHP_URL_QUERY) : $_SERVER['QUERY_STRING'];
51
    $fragment = '';
52
    $user     = !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
53
    $password = !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
54
    if (empty($user) && empty($password) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
55
        list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
56
    }
57
58
    return new Uri(
59
        $scheme,
60
        $host,
61
        $port,
62
        $path,
63
        $query,
64
        $fragment,
65
        $user,
66
        $password
67
    );
68
}
69
70
/**
71
 * Create a new stream based on the input type.
72
 *
73
 * Options is an associative array that can contain the following keys:
74
 * - metadata: Array of custom metadata.
75
 * - size: Size of the stream.
76
 *
77
 * @param resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data
78
 * @param array                                                                  $options  Additional options
79
 *
80
 * @return StreamInterface
81
 * @throws \InvalidArgumentException if the $resource arg is not valid.
82
 */
83
function stream_for($resource = '', array $options = [])
84
{
85
    if (is_scalar($resource)) {
86
        return openStream($resource, $options);
87
    }
88
    
89
    return createStream($resource);
90
}
91
92
function createStream($resource)
93
{
94
    switch (gettype($resource)) {
95
        case 'resource':
96
            return new Stream($resource, $options);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $options seems to be never defined.
Loading history...
97
        case 'object':
98
            if ($resource instanceof StreamInterface) {
99
                return $resource;
100
            } elseif (method_exists($resource, '__toString')) {
101
                return stream_for((string) $resource, $options);
102
            }
103
            return new PumpStream(function () use ($resource) {
104
                if (!$resource->valid()) {
105
                    return false;
106
                }
107
                $result = $resource->current();
108
                $resource->next();
109
                return $result;
110
            }, $options);
111
        case 'NULL':
112
            return new Stream(fopen('php://temp', 'r+'), $options);
113
    }
114
115
    if (is_callable($resource) && !is_null($resource)) {
116
        return new \One\Http\PumpStream($resource, $options);
117
    }
118
119
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
120
}
121
122
function openStream($resource, $options)
123
{
124
    $stream = fopen('php://temp', 'r+');
125
    if ($resource !== '' && $stream !== false) {
126
        fwrite($stream, $resource);
127
        fseek($stream, 0);
128
    }
129
    return new Stream($stream, $options);
130
}
131