Completed
Push — master ( 465cbd...e0d13e )
by Marco
06:44 queued 11s
created

Processor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 54
c 0
b 0
f 0
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 18 2
A __construct() 0 13 1
A parse() 0 10 1
1
<?php namespace Comodojo\Dispatcher\Output;
2
3
use \Comodojo\Dispatcher\Components\AbstractModel;
4
use \Comodojo\Dispatcher\Request\Model as Request;
5
use \Comodojo\Dispatcher\Response\Model as Response;
6
use \Comodojo\Dispatcher\Traits\RequestTrait;
7
use \Comodojo\Dispatcher\Traits\ResponseTrait;
8
use \Comodojo\Dispatcher\Components\HttpStatusCodes;
9
use \Comodojo\Foundation\Base\Configuration;
10
use \Psr\Log\LoggerInterface;
11
use \Exception;
12
13
/**
14
 * @package     Comodojo Dispatcher
15
 * @author      Marco Giovinazzi <[email protected]>
16
 * @author      Marco Castiello <[email protected]>
17
 * @license     MIT
18
 *
19
 * LICENSE:
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 */
29
30
//
31
// @WARNING: some method's preprocessor missing
32
//
33
//         _______
34
//        j_______j
35
//       /_______/_\
36
//       |Missing| |
37
//       |  ___  | |
38
//       | !418! | |
39
//       | !___! | |
40
//       |_______|,'
41
//
42
43
class Processor extends AbstractModel {
44
45
    use RequestTrait;
46
    use ResponseTrait;
47
48
    protected $codes;
49
50 4
    public function __construct(
51
        Configuration $configuration,
52
        LoggerInterface $logger,
53
        Request $request,
54
        Response $response
55
    ) {
56
57 4
        parent::__construct($configuration, $logger);
58
59 4
        $this->setResponse($response);
60 4
        $this->setRequest($request);
61
62 4
        $this->codes = new HttpStatusCodes();
63
64 4
    }
65
66 4
    public function send() {
67
68 4
        $response = $this->getResponse();
69 4
        $request = $this->getRequest();
70
71 4
        $status = $response->getStatus()->get();
72
73 4
        if (!$this->codes->exists($status)) throw new Exception("Invalid HTTP status code in response");
74
75 4
        $message = $this->codes->getMessage($status);
76
77 4
        $response->getHeaders()->send();
78
79 4
        header(sprintf('HTTP/%s %s %s', (string)$request->getVersion(), $status, $message), true, $status);
80
81 4
        $response->getCookies()->save();
82
83 4
        return $response->getContent()->get();
84
85
    }
86
87 4
    public static function parse(
88
        Configuration $configuration,
89
        LoggerInterface $logger,
90
        Request $request,
91
        Response $response
92
    ) {
93
94 4
        $processor = new Processor($configuration, $logger, $request, $response);
95
96 4
        return $processor->send();
97
98
    }
99
100
}
101