Model   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Test Coverage

Coverage 67.33%

Importance

Changes 0
Metric Value
wmc 38
eloc 83
dl 0
loc 228
ccs 68
cts 101
cp 0.6733
rs 9.36
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 3 1
A __construct() 0 11 1
A getLocation() 0 3 1
B consolidate() 0 29 8
B import() 0 10 8
A setContent() 0 5 1
A unserialize() 0 3 1
A setLocation() 0 5 1
A setClientCache() 0 25 6
A getPreprocessor() 0 3 1
A getHeaders() 0 3 1
A setPreprocessor() 0 5 1
A export() 0 8 1
A getContent() 0 3 1
A getCookies() 0 3 1
A setHeaders() 0 5 1
A setCookies() 0 5 1
A serialize() 0 3 1
A setStatus() 0 5 1
1
<?php namespace Comodojo\Dispatcher\Response;
2
3
use \Comodojo\Dispatcher\Components\AbstractModel;
4
use \Comodojo\Dispatcher\Request\Model as Request;
5
use \Comodojo\Dispatcher\Router\Route;
6
use \Comodojo\Foundation\Timing\TimingTrait;
7
use \Comodojo\Foundation\Base\Configuration;
8
use \Comodojo\Cookies\CookieManager;
9
use \Psr\Log\LoggerInterface;
10
use \Serializable;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     MIT
17
 *
18
 * LICENSE:
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29
class Model extends AbstractModel implements Serializable {
30
31
    use TimingTrait;
32
33
    protected static $no_content_statuses = [100, 101, 102, 204, 304];
34
35
    protected static $cacheable_methods = ['GET', 'HEAD', 'POST', 'PUT'];
36
37
    protected static $cacheable_statuses = [200, 203, 300, 301, 302, 404, 410];
38
39
    protected $headers;
40
41
    protected $cookies;
42
43
    protected $status;
44
45
    protected $content;
46
47
    protected $location;
48
49
    protected $preprocessor;
50
51 7
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
52
53 7
        parent::__construct($configuration, $logger);
54
55 7
        $this->setHeaders(new Headers())
56 7
            ->setCookies(new CookieManager())
57 7
            ->setStatus(new Status())
58 7
            ->setContent(new Content())
59 7
            ->setLocation(new Location())
60 7
            ->setPreprocessor(new Preprocessor())
61 7
            ->setTiming();
62
63 7
    }
64
65 6
    public function getHeaders() {
66
67 6
        return $this->headers;
68
69
    }
70
71 7
    public function setHeaders(Headers $headers) {
72
73 7
        $this->headers = $headers;
74
75 7
        return $this;
76
77
    }
78
79 6
    public function getCookies() {
80
81 6
        return $this->cookies;
82
83
    }
84
85 7
    public function setCookies(CookieManager $cookies) {
86
87 7
        $this->cookies = $cookies;
88
89 7
        return $this;
90
91
    }
92
93 9
    public function getStatus() {
94
95 9
        return $this->status;
96
97
    }
98
99 7
    public function setStatus(Status $status) {
100
101 7
        $this->status = $status;
102
103 7
        return $this;
104
105
    }
106
107 9
    public function getContent() {
108
109 9
        return $this->content;
110
111
    }
112
113 7
    public function setContent(Content $content) {
114
115 7
        $this->content = $content;
116
117 7
        return $this;
118
119
    }
120
121 3
    public function getLocation() {
122
123 3
        return $this->location;
124
125
    }
126
127 7
    public function setLocation(Location $location) {
128
129 7
        $this->location = $location;
130
131 7
        return $this;
132
133
    }
134
135
    /**
136
     * Get the current preprocessor
137
     *
138
     * @return Preprocessor
139
     */
140 33
    public function getPreprocessor() {
141
142 33
        return $this->preprocessor;
143
144
    }
145
146
    /**
147
     * Set the current preprocessor
148
     *
149
     * @param Preprocessor $preprocessor
150
     *  The preprocessor to use
151
     * @return Model
152
     */
153 7
    public function setPreprocessor(Preprocessor $preprocessor) {
154
155 7
        $this->preprocessor = $preprocessor;
156
157 7
        return $this;
158
159
    }
160
161
    public function serialize() {
162
163
        return serialize($this->export());
164
165
    }
166
167
    public function unserialize($data) {
168
169
        $this->import(unserialize($data));
170
171
    }
172
173 1
    public function export() {
174
175
        return (object)[
176 1
            'headers' => $this->getHeaders(),
177 1
            'cookies' => $this->getCookies()->getAll(),
178 1
            'status' => $this->getStatus(),
179 1
            'content' => $this->getContent(),
180 1
            'location' => $this->getLocation()
181 1
        ];
182
183
    }
184
185 1
    public function import($data) {
186
187 1
        if (isset($data->headers)) $this->setHeaders($data->headers);
188 1
        if (isset($data->status)) $this->setStatus($data->status);
189 1
        if (isset($data->content)) $this->setContent($data->content);
190 1
        if (isset($data->location)) $this->setLocation($data->location);
191
192 1
        if (isset($data->cookies) && is_array($data->cookies)) {
193 1
            $cookies = $this->getCookies();
194 1
            foreach ($data->cookies as $name => $cookie) $cookies->add($cookie);
195 1
        }
196
197 1
    }
198
199 2
    public function consolidate(Request $request, Route $route=null) {
200
201 2
        $status = $this->getStatus()->get();
202 2
        $preprocessor = $this->getPreprocessor()->get($status);
203 2
        $preprocessor->consolidate($this);
204
205 2
        if ($route != null) {
0 ignored issues
show
Coding Style introduced by
Operator != prohibited; use !== instead
Loading history...
206
            $this->setClientCache($request, $route);
207
        }
208
209
        // extra checks
210 2
        $content = $this->getContent();
211 2
        $headers = $this->getHeaders();
212
213 2
        if ((string)$request->getMethod() == 'HEAD' && !in_array($status, self::$no_content_statuses)) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
214
            $length = $content->length();
215
            $content->set(null);
216
            if ($length) {
217
                $headers->set('Content-Length', $length);
218
            }
219
        }
220
221 2
        if ($headers->get('Transfer-Encoding') != null) {
0 ignored issues
show
Coding Style introduced by
Operator != prohibited; use !== instead
Loading history...
Bug introduced by
It seems like you are loosely comparing $headers->get('Transfer-Encoding') of type array|null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
222
            $headers->delete('Content-Length');
223
        }
224
225 2
        if ((string)$request->getVersion() == '1.0' && false !== strpos($headers->get('Cache-Control'), 'no-cache')) {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
Bug introduced by
It seems like $headers->get('Cache-Control') can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
        if ((string)$request->getVersion() == '1.0' && false !== strpos(/** @scrutinizer ignore-type */ $headers->get('Cache-Control'), 'no-cache')) {
Loading history...
226
            $headers->set('pragma', 'no-cache');
227
            $headers->set('expires', -1);
228
        }
229
230 2
    }
231
232
    private function setClientCache(Request $request, Route $route) {
233
234
        $cache = strtoupper($route->getParameter('cache'));
235
        $ttl = (int)$route->getParameter('ttl');
236
237
        if (
238
            ($cache == 'CLIENT' || $cache == 'BOTH') &&
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
239
            in_array((string)$request->getMethod(), self::$cacheable_methods) &&
240
            in_array($this->getStatus()->get(), self::$cacheable_statuses)
241
            // @TODO: here we should also check for Cache-Control no-store or private;
242
            //        the cache layer will be improoved in future versions.
243
        ) {
244
245
            $headers = $this->getHeaders();
246
            $timestamp = (int) $this->getTime()->format('U')+$ttl;
247
248
            if ($ttl > 0) {
249
250
                $headers->set("Cache-Control", "max-age=".$ttl.", must-revalidate");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cache-Control does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal max-age= does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal , must-revalidate does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
251
                $headers->set("Expires", gmdate("D, d M Y H:i:s", $timestamp)." GMT");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Expires does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal D, d M Y H:i:s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal GMT does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
252
253
            } else {
254
255
                $headers->set("Cache-Control", "no-cache, must-revalidate");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cache-Control does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal no-cache, must-revalidate does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
256
                $headers->set("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Expires does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal Mon, 26 Jul 1997 05:00:00 GMT does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
257
258
            }
259
260
        }
261
262
    }
263
264
}
265