Completed
Push — master ( 18874d...aab377 )
by Andreas
18:55
created

midcom_compat_environment::_httpd_setup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.compat
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
11
/**
12
 * Support for interactions with environment
13
 *
14
 * @package midcom.compat
15
 */
16
class midcom_compat_environment
17
{
18
    private static $_headers = [];
19
20
    private static $_implementation;
21
22
    public function __construct()
23
    {
24
        if (   php_sapi_name() != 'cli'
25
            || !empty($_SERVER['REMOTE_ADDR'])) {
26
            $this->_httpd_setup();
27
        }
28
    }
29
30 29
    public static function get()
31
    {
32 29
        return self::$_implementation;
33
    }
34
35
    public static function initialize()
36
    {
37
        self::$_implementation = new static;
38
    }
39
40
    private function _httpd_setup()
41
    {
42
        /*
43
         * make sure the URLs not having query string (or midcom-xxx- -method signature)
44
         * have trailing slash or some extension in the "filename".
45
         *
46
         * This makes life much, much better when making static copies for whatever reason
47
         */
48
        $redirect_test_uri = (string)$_SERVER['REQUEST_URI'];
49
        if (   !preg_match('%\?|/$|midcom-.+-|/.*\.[^/]+$%', $redirect_test_uri)
50
            && (empty($_POST))) {
51
            $response = new RedirectResponse($redirect_test_uri . '/', 301);
52
            $response->send();
53
            $this->stop_request();
54
        }
55
    }
56
57 18
    public function header($string, $replace = true, $http_response_code = null)
58
    {
59 18
        if (!defined('OPENPSA2_UNITTEST_RUN')) {
60
            header($string, $replace, $http_response_code);
61
        } else {
62 18
            self::$_headers[] = [
63 18
                'value' => $string,
64 18
                'replace' => $replace,
65 18
                'http_response_code' => $http_response_code
66
            ];
67
        }
68 18
    }
69
70 11
    public function stop_request($message = '')
71
    {
72 11
        if (!defined('OPENPSA2_UNITTEST_RUN')) {
73
            exit($message);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
74
        }
75 11
    }
76
77 730
    public static function flush_registered_headers()
78
    {
79 730
        $headers = self::$_headers;
80 730
        self::$_headers = [];
81 730
        return $headers;
82
    }
83
}
84