Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

ResponseFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 27.03%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 10
cts 37
cp 0.2703
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 3
A capture() 0 8 1
A json() 0 5 1
A download() 0 16 3
A redirect() 0 9 2
A generator() 0 4 1
1
<?php
2
namespace Wandu\Http\Factory;
3
4
use Closure;
5
use Generator;
6
use InvalidArgumentException;
7
use Psr\Http\Message\StreamInterface;
8
use Psr\Http\Message\UriInterface;
9
use Wandu\Http\Psr\Response;
10
use Wandu\Http\Psr\Stream\GeneratorStream;
11
use Wandu\Http\Psr\Stream\StringStream;
12
13
class ResponseFactory
14
{
15
    /**
16
     * @param \Psr\Http\Message\StreamInterface|string $content
17
     * @param int $status
18
     * @param array $headers
19
     * @return \Psr\Http\Message\ResponseInterface
20
     */
21 41
    public function create($content = null, $status = 200, array $headers = [])
22
    {
23 41
        if (isset($content) && !($content instanceof StreamInterface)) {
24 39
            $content = new StringStream($content);
25 39
        }
26 41
        return new Response($status, $content, $headers, '', '1.1');
27
    }
28
29
    /**
30
     * @param \Closure $area
31
     * @param int $status
32
     * @param array $headers
33
     * @return \Psr\Http\Message\ResponseInterface
34
     */
35
    public function capture(Closure $area, $status = 200, array $headers = [])
36
    {
37
        ob_start();
38
        $area();
39
        $contents = ob_get_contents();
40
        ob_end_clean();
41
        return $this->create($contents, $status, $headers);
42
    }
43
44
    /**
45
     * @param string|array $data
46
     * @param int $status
47
     * @param array $headers
48
     * @return \Psr\Http\Message\ResponseInterface
49
     */
50 2
    public function json($data = [], $status = 200, array $headers = [])
51
    {
52 2
        return $this->create(json_encode($data), $status, $headers)
53 2
            ->withHeader('Content-Type', 'application/json');
54
    }
55
56
    /**
57
     * @param string $file
58
     * @param string $name
59
     * @param array $headers
60
     * @return \Psr\Http\Message\ResponseInterface
61
     */
62
    public function download($file, $name = null, array $headers = [])
63
    {
64
        if (!is_file($file)) {
65
            new InvalidArgumentException("\"{$file}\" is not a file.");
66
        }
67
        return $this->create(file_get_contents($file), 200, $headers)
68
            ->withHeader('Pragma', 'public')
69
            ->withHeader('Expires', '0')
70
            ->withHeader('Content-Type', 'application/octet-stream')
71
            ->withHeader(
72
                'Content-Disposition',
73
                'attachment; filename=' . (isset($name) ? $name : basename($file))
74
            )
75
            ->withHeader('Content-Transfer-Encoding', 'binary')
76
            ->withHeader('Content-Length', filesize($file) . '');
77
    }
78
79
    /**
80
     * @param \Psr\Http\Message\UriInterface|string $path
81
     * @param int $status
82
     * @param array $headers
83
     * @return \Psr\Http\Message\ResponseInterface
84
     */
85
    public function redirect($path, $status = 302, $headers = [])
86
    {
87
        if ($path instanceof UriInterface) {
88
            $path = $path->__toString();
89
        }
90
        return $this->create(null, $status, $headers)
91
            ->withStatus($status)
92
            ->withAddedHeader('Location', $path);
93
    }
94
95
    /**
96
     * @param \Generator $generator
97
     * @param int $status
98
     * @param array $headers
99
     * @return \Psr\Http\Message\ResponseInterface
100
     */
101 2
    public function generator(Generator $generator, $status = 200, array $headers = [])
102
    {
103 2
        return $this->create(new GeneratorStream($generator), $status, $headers);
104
    }
105
}
106