HttpResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 3
b 0
f 0
dl 0
loc 38
ccs 19
cts 19
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 28 7
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall;
24
25
use Psr\Http\Message\ResponseInterface;
26
use function header;
27
use function headers_sent;
28
use function sprintf;
29
use function stripos;
30
31
/**
32
 * Display the final result.
33
 */
34
class HttpResolver
35
{
36
    /**
37
     * Invoker.
38
     *
39
     * @param ResponseInterface $response The PSR-7 response.
40
     * @param bool              $finally  Terminate current PHP proccess if
41
     *                                    this value is true.
42
     * @return void
43
     */
44
    public function __invoke(ResponseInterface $response, $finally = true): void
45 55
    {
46
        if (!headers_sent()) {
47 55
            foreach ($response->getHeaders() as $key => $values) {
48
                $replace = stripos($key, 'Set-Cookie') === 0 ? false : true;
49 55
                foreach ($values as $value) {
50 15
                    header(sprintf('%s: %s', $key, $value), $replace);
51 15
                    $replace = false;
52 15
                }
53 15
            }
54
55
            header(
56
                sprintf(
57 55
                    'HTTP/%s %s %s',
58 55
                    $response->getProtocolVersion(),
59 55
                    $response->getStatusCode(),
60 55
                    $response->getReasonPhrase()
61 55
                ),
62 55
                true,
63 55
                $response->getStatusCode()
64 55
            );
65 55
        }
66 55
67
        echo $response->getBody()->getContents();
68
69 55
        if ($finally && !defined('PHP_UNIT_TEST')) {
70
            // @codeCoverageIgnoreStart
71 55
            exit;
72
            // @codeCoverageIgnoreEnd
73
        }
74
    }
75
}
76