Completed
Push — master ( 45ec31...d70347 )
by Ivan
03:16
created

Response::setCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace vakata\http;
4
5
use Zend\Diactoros\Stream;
6
use Zend\Diactoros\Response as PSRResponse;
7
8
class Response extends PSRResponse
9
{
10
    protected $callback = null;
11
12 1
    public function __construct($status = 200, string $body = null, array $headers = [])
13
    {
14 1
        parent::__construct('php://memory', $status, $headers);
15 1
        if ($body !== null) {
16
            $this->setBody($body);
17
        }
18 1
    }
19 1
    public function hasCache()
20
    {
21 1
        return $this->hasHeader('Cache-Control') ||
22 1
            $this->hasHeader('Expires') ||
23 1
            $this->hasHeader('Last-Modified') ||
24 1
            $this->hasHeader('ETag');
25
    }
26
    /**
27
     * Make the response cacheable.
28
     * @param  int|string     $expires when should the request expire - either a timestamp or strtotime expression
29
     * @return self
30
     */
31 1
    public function cacheUntil($expires)
32
    {
33 1
        if (!is_int($expires)) {
34 1
            $expires = strtotime($expires);
35
        }
36
        return $this
37 1
            ->withHeader('Pragma', 'public')
38 1
            ->withHeader('Cache-Control', 'maxage='.($expires - time()))
39 1
            ->withHeader('Expires', gmdate('D, d M Y H:i:s', $expires).' GMT');
40
    }
41
    /**
42
     * Prevent caching
43
     *
44
     * @return self
45
     */
46
    public function noCache()
47
    {
48
        return $this
49
            ->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')
50
            ->withHeader('Expires', gmdate('D, d M Y H:i:s', 0).' GMT');
51
    }
52 1
    public function setBody(string $body)
53
    {
54 1
        $temp = (new Stream('php://temp', 'wb+'));
55 1
        $temp->write($body);
56 1
        return $this->withBody($temp);
57
    }
58
    /**
59
     * Set a cookie
60
     * @param  string    $name  the cookie name
61
     * @param  string    $value the cookie value
62
     * @param  string    $extra optional extra params for the cookie (semicolon delimited)
63
     * @return  self
64
     */
65
    public function withCookie($name, $value, $extra = '')
66
    {
67
        return $this->withAddedHeader('Set-Cookie', $name . '=' . urlencode($value) . '; ' . $extra);
68
    }
69
    /**
70
     * Expires an existing cookie
71
     * @param  string    $name the cookie name
72
     * @param  string    $extra optional extra params for the cookie (semicolon delimited)
73
     * @return self
74
     */
75
    public function expireCookie($name, $extra = '')
76
    {
77
        $extra = implode('; ', array_filter([ $extra, 'Expires=' . date('r', 0) ]));
78
        return $this->withCookie($name, 'deleted', $extra);
79
    }
80 1
    public function setContentTypeByExtension(string $extension)
81
    {
82 1
        switch (strtolower($extension)) {
83 1
            case "txt":
84 1
            case "text":
85
                $type = "text/plain; charset=UTF-8";
86
                break;
87 1
            case "xml":
88 1
            case "xsl":
89
                $type = "text/xml; charset=UTF-8";
90
                break;
91 1
            case "json":
92 1
                $type = "application/json; charset=UTF-8";
93 1
                break;
94
            case "pdf":
95
                $type = "application/pdf";
96
                break;
97
            case "exe":
98
                $type = "application/octet-stream";
99
                break;
100
            case "zip":
101
                $type = "application/zip";
102
                break;
103
            case "docx":
104
                $type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
105
                break;
106
            case "doc":
107
                $type = "application/msword";
108
                break;
109
            case "xlsx":
110
                $type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
111
                break;
112
            case "xls":
113
                $type = "application/vnd.ms-excel";
114
                break;
115
            case "ppt":
116
                $type = "application/vnd.ms-powerpoint";
117
                break;
118
            case "pptx":
119
                $type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
120
                break;
121
            case "gif":
122
                $type = "image/gif";
123
                break;
124
            case "png":
125
                $type = "image/png";
126
                break;
127
            case "mp3":
128
                $type = "audio/mpeg";
129
                break;
130
            case "mp4":
131
                $type = "video/mpeg";
132
                break;
133
            case "jpeg":
134
            case "jpg":
135
                $type = "image/jpeg";
136
                break;
137
            case "html":
138
            case "php":
139
            case "htm":
140
                $type = "text/html; charset=UTF-8";
141
                break;
142
            default:
143
                $type = "application/binary";
144
                break;
145
        }
146 1
        return $this->withHeader('Content-Type', $type);
147
    }
148
149
    public function withCallback(callable $callback = null)
150
    {
151
        return $this->setBody('')->setCallback($callback);
152
    }
153
    protected function setCallback(callable $callback = null)
154
    {
155
        $this->callback = $callback;
156
        return $this;
157
    }
158
    public function hasCallback()
159
    {
160
        return $this->callback !== null;
161
    }
162
    public function getCallback()
163
    {
164
        return $this->callback;
165
    }
166
}
167