Completed
Push — master ( 356d79...201886 )
by Changwan
03:54
created

functions.php ➔ iterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Http
3
{
4
5
    use Wandu\Http\Factory\ResponseFactory;
6
7
    /**
8
     * @return \Wandu\Http\Factory\ResponseFactory
9
     */
10
    function response()
11
    {
12 46
        if (!isset(ResponseFactory::$instance)) {
13 1
            ResponseFactory::$instance = new ResponseFactory();
14
        }
15 46
        return ResponseFactory::$instance;
16
    }
17
18
    /**
19
     * @reference https://gist.github.com/Mulkave/65daabb82752f9b9a0dd
20
     * @param string $url
21
     * @return array|boolean
22
     */
23
    function parseUrl($url)
24
    {
25
        $parts = parse_url(preg_replace_callback('/[^:\/@?&=#]+/u', function ($matches) {
26 54
            return urlencode($matches[0]);
27 70
        }, $url));
28 70
        if ($parts === false) {
29
            return false;
30
        }
31 70
        foreach($parts as $name => $value) {
32 70
            $parts[$name] = ($name === 'port') ? $value : urldecode($value);
33
        }
34 70
        return $parts;
35
    }
36
}
37
38
namespace Wandu\Http\Response
39
{
40
41
    use Closure;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
42
    use Generator;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
43
    use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
44
    use Traversable;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
45
    use Wandu\Http\Exception\BadRequestException;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
46
    use function Wandu\Http\response;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
47
48
    /**
49
     * @param string $content
50
     * @param int $status
51
     * @param array $headers
52
     * @return \Psr\Http\Message\ResponseInterface
53
     */
54
    function create($content = null, $status = 200, array $headers = [])
55
    {
56 2
        return response()->create($content, $status, $headers);
0 ignored issues
show
Bug introduced by
It seems like $content defined by parameter $content on line 54 can also be of type string; however, Wandu\Http\Factory\ResponseFactory::create() does only seem to accept null|object<Psr\Http\Message\StreamInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
    }
58
59
    /**
60
     * @param \Closure $area
61
     * @param int $status
62
     * @param array $headers
63
     * @return \Psr\Http\Message\ResponseInterface
64
     */
65
    function capture(Closure $area, $status = 200, array $headers = [])
66
    {
67
        return response()->capture($area, $status, $headers);
68
    }
69
70
    /**
71
     * @param  string|array  $data
72
     * @param  int  $status
73
     * @param  array  $headers
74
     * @return \Psr\Http\Message\ResponseInterface
75
     */
76
    function json($data = [], $status = 200, array $headers = [])
77
    {
78
        return response()->json($data, $status, $headers);
79
    }
80
81
    /**
82
     * @param  string  $file
83
     * @param  string  $name
84
     * @param  array  $headers
85
     * @return \Psr\Http\Message\ResponseInterface
86
     */
87
    function download($file, $name = null, array $headers = [])
88
    {
89
        return response()->download($file, $name, $headers);
90
    }
91
92
    /**
93
     * @param string $path
94
     * @param array $queries
95
     * @param int $status
96
     * @param array $headers
97
     * @return \Psr\Http\Message\ResponseInterface
98
     */
99
    function redirect($path, $queries = [], $status = 302, $headers = [])
100
    {
101
        $parsedQueries = [];
102
        foreach ($queries as $key => $value) {
103
            $parsedQueries[] = "{$key}=" . urlencode($value);
104
        }
105
        if (count($parsedQueries)) {
106
            $path .= '?' . implode('&', $parsedQueries);
107
        }
108
        return response()->redirect($path, $status, $headers);
109
    }
110
111
    /**
112
     * @param \Psr\Http\Message\ServerRequestInterface $request
113
     * @return \Psr\Http\Message\ResponseInterface
114
     * @throws \Wandu\Http\Exception\BadRequestException
115
     */
116
    function back(ServerRequestInterface $request)
117
    {
118
        if ($request->hasHeader('referer')) {
119
            return redirect($request->getHeader('referer'));
120
        }
121
        throw new BadRequestException();
122
    }
123
124
    /**
125
     * @deprecated use iterator
126
     * 
127
     * @param \Generator $generator
128
     * @param int $status
129
     * @param array $headers
130
     * @return \Psr\Http\Message\ResponseInterface
131
     */
132
    function generator(Generator $generator, $status = 200, array $headers = [])
133
    {
134
        return response()->iterator($generator, $status, $headers);
135
    }
136
137
    /**
138
     * @param \Traversable $iterator
139
     * @param int $status
140
     * @param array $headers
141
     * @return \Psr\Http\Message\ResponseInterface
142
     */
143
    function iterator(Traversable $iterator, $status = 200, array $headers = [])
144
    {
145
        return response()->iterator($iterator, $status, $headers);
146
    }
147
}
148