Completed
Push — 4.0 ( ad8a54...ed771a )
by Marco
06:33
created

Processor::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php namespace Comodojo\Dispatcher\Output;
2
3
use \Comodojo\Dispatcher\Output\HttpStatus\StatusGeneric;
4
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Response\Model as Response;
7
use \Comodojo\Dispatcher\Components\Configuration;
8
use \Psr\Log\LoggerInterface;
9
use \Exception;
10
11
/**
12
 * @package     Comodojo Dispatcher
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @author      Marco Castiello <[email protected]>
15
 * @license     GPL-3.0+
16
 *
17
 * LICENSE:
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
 */
32
33
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
// @WARNING: some method's preprocessor missing
35
//
36
//         _______
37
//        j_______j
38
//       /_______/_\
39
//       |Missing| |
40
//       |  ___  | |
41
//       | !418! | |
42
//       | !___! | |
43
//       |_______|,'
44
//
45
46
class Processor extends DispatcherClassModel {
47
48
    private $codes = array(
49
        // Informational 1xx
50
        100 => 'Continue',
51
        101 => 'Switching Protocols',
52
        102 => 'Processing',
53
        // Successful 2xx
54
        200 => 'OK',
55
        201 => 'Created',
56
        202 => 'Accepted',
57
        203 => 'Non-Authoritative Information',
58
        204 => 'No Content',
59
        205 => 'Reset Content',
60
        206 => 'Partial Content',
61
        207 => 'Multi-Status', // missing
62
        208 => 'Already Reported', // missing
63
        226 => 'IM Used', // missing
64
        // Redirection 3xx
65
        300 => 'Multiple Choices',
66
        301 => 'Moved Permanently',
67
        302 => 'Found',
68
        303 => 'See Other',
69
        304 => 'Not Modified',
70
        305 => 'Use Proxy',
71
        307 => 'Temporary Redirect',
72
        308 => 'Permanent Redirect',
73
        // Client Error 4xx
74
        400 => 'Bad Request',
75
        401 => 'Unauthorized', // missing
76
        402 => 'Payment Required', // missing
77
        403 => 'Forbidden',
78
        404 => 'Not Found',
79
        405 => 'Method Not Allowed',
80
        406 => 'Not Acceptable', // missing
81
        407 => 'Proxy Authentication Required', // missing
82
        408 => 'Request Timeout', // missing
83
        409 => 'Conflict', // missing
84
        410 => 'Gone',
85
        411 => 'Length Required', // missing
86
        412 => 'Precondition Failed', // missing
87
        413 => 'Payload Too Large', // missing
88
        414 => 'URI Too Long', // missing
89
        415 => 'Unsupported Media Type', // missing
90
        416 => 'Range Not Satisfiable', // missing
91
        417 => 'Expectation Failed', // missing
92
        421 => 'Misdirected Request', // missing
93
        422 => 'Unprocessable Entity', // missing
94
        423 => 'Locked', // missing
95
        424 => 'Failed Dependency', // missing
96
        426 => 'Upgrade Required', // missing
97
        428 => 'Precondition Required', // missing
98
        429 => 'Too Many Requests', // missing
99
        431 => 'Request Header Fields Too Large', // missing
100
        451 => 'Unavailable For Legal Reasons', // missing
101
        // Server Error 5xx
102
        500 => 'Internal Server Error',
103
        501 => 'Not Implemented',
104
        502 => 'Bad Gateway',
105
        503 => 'Service Unavailable',
106
        504 => 'Gateway Timeout',
107
        505 => 'HTTP Version Not Supported',
108
        506 => 'Variant Also Negotiates (Experimental)', // missing
109
        507 => 'Insufficient Storage', // missing
110
        508 => 'Loop Detected', // missing
111
        510 => 'Not Extended', // missing
112
        511 => 'Network Authentication Required' // missing
113
    );
114
115
    private $request;
116
117
    private $response;
118
119 1
    public function __construct(Configuration $configuration, LoggerInterface $logger, Request $request, Response $response) {
120
121 1
        parent::__construct($configuration, $logger);
122
123 1
        $this->response = $response;
124
125 1
        $this->request = $request;
126
127 1
    }
128
129 1
    public function send() {
130
131 1
        $status = $this->response->status()->get();
132
133 1
        if ( !array_key_exists($status, $this->codes) ) throw new Exception("Invalid HTTP status code in response");
134
135 1
        $message = $this->codes[$status];
136
137 1
        $this->response->headers()->send();
138
139 1
        header(sprintf('HTTP/%s %s %s', $this->request->version()->get(), $status, $message), true, $status);
140
141 1
        $this->response->cookies()->save();
142
143 1
        return $this->response->content()->get();
144
145
    }
146
147 1
    public static function parse(Configuration $configuration, LoggerInterface $logger, Request $request, Response $response) {
148
149 1
        $processor = new Processor($configuration, $logger, $request, $response);
150
151 1
        return $processor->send();
152
153
    }
154
155
}
156