Completed
Push — master ( 9f721d...dab6df )
by Alex
02:17
created

HttpRequestParams::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Mezon\Service\ServiceHttpTransport;
3
4
/**
5
 * Class HttpRequestParams
6
 *
7
 * @package ServiceHttpTransport
8
 * @subpackage HttpRequestParams
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/07)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Request params fetcher.
16
 */
17
class HttpRequestParams extends \Mezon\Service\ServiceRequestParams
18
{
19
20
    /**
21
     * Fetching auth token from headers
22
     *
23
     * @param array $headers
24
     *            Request headers
25
     * @return string Session id
26
     */
27
    protected function getSessionIdFromHeaders(array $headers)
28
    {
29
        if (isset($headers['Authentication'])) {
30
            return str_replace('Basic ', '', $headers['Authentication']);
31
        } elseif (isset($headers['Authorization'])) {
32
            return str_replace('Basic ', '', $headers['Authorization']);
33
        } elseif (isset($headers['Cgi-Authorization'])) {
34
            return str_replace('Basic ', '', $headers['Cgi-Authorization']);
35
        }
36
37
        throw (new \Exception('Invalid session token', 2));
38
    }
39
40
    /**
41
     * Method returns list of the request's headers
42
     *
43
     * @return array Array of headers
44
     */
45
    protected function getHttpRequestHeaders(): array
46
    {
47
        $headers = getallheaders();
48
49
        return $headers === false ? [] : $headers;
50
    }
51
52
    /**
53
     * Method returns session id from HTTP header
54
     *
55
     * @return string Session id
56
     */
57
    protected function getSessionId()
58
    {
59
        $headers = $this->getHttpRequestHeaders();
60
61
        return $this->getSessionIdFromHeaders($headers);
62
    }
63
64
    /**
65
     * Method returns request parameter
66
     *
67
     * @param string $param
68
     *            parameter name
69
     * @param mixed $default
70
     *            default value
71
     * @return mixed Parameter value
72
     */
73
    public function getParam($param, $default = false)
74
    {
75
        $headers = $this->getHttpRequestHeaders();
76
77
        $return = $default;
78
79
        if ($param == 'session_id') {
80
            $return = $this->getSessionId();
81
        } elseif ($this->router->hasParam($param)) {
82
            $return = $this->router->getParam($param);
83
        } elseif (isset($headers[$param])) {
84
            $return = $headers[$param];
85
        } elseif (isset($_POST[$param])) {
86
            $return = $_POST[$param];
87
        } elseif (isset($_GET[$param])) {
88
            $return = $_GET[$param];
89
        }
90
91
        return $return;
92
    }
93
}
94