Completed
Push — master ( cb2b26...7fdd78 )
by Hugues
02:41
created

Context   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0
Metric Value
wmc 20
lcom 3
cbo 0
dl 0
loc 156
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A isCli() 0 4 1
A getPhpInterface() 0 4 1
A getArguments() 0 4 1
A getRequestMethod() 0 4 1
A getPath() 0 4 1
A getHost() 0 4 1
A getCliInfo() 0 4 1
A argumentsToString() 0 19 4
A escapeToken() 0 4 2
A argumentsStringToArray() 0 4 1
A getHttpInfo() 0 6 1
A inCliContext() 0 4 2
A getSapiName() 0 4 1
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace HMLB\UserBundle\Message\Trace;
6
7
/**
8
 * Context of a message trace.
9
 *
10
 * Debug information about a trace: was it initiated with CLI or Web request, cli arguments, web URI ...
11
 *
12
 * @author Hugues Maignol <[email protected]>
13
 *
14
 * TODO: separate CLI and HTTP contexts in separate classes.
15
 */
16
class Context
17
{
18
    /**
19
     * @var string
20
     */
21
    private $phpInterface;
22
23
    /**
24
     * @var bool
25
     */
26
    private $cli;
27
28
    /**
29
     * @var string
30
     */
31
    private $arguments;
32
33
    /**
34
     * @var string
35
     */
36
    private $requestMethod;
37
38
    /**
39
     * @var string
40
     */
41
    private $path;
42
43
    /**
44
     * @var string
45
     */
46
    private $host;
47
48
    public function __construct()
49
    {
50
        $this->phpInterface = self::getSapiName();
51
        $this->cli = self::inCliContext();
52
        if ($this->cli) {
53
            $this->getCliInfo();
54
        } else {
55
            $this->getHttpInfo();
56
        }
57
    }
58
59
    /**
60
     * Context in CLI.
61
     *
62
     * @return bool
63
     */
64
    public function isCli(): bool
65
    {
66
        return true === $this->cli;
67
    }
68
69
    /**
70
     * Getter de phpInterface.
71
     *
72
     * @return string
73
     */
74
    public function getPhpInterface()
75
    {
76
        return $this->phpInterface;
77
    }
78
79
    /**
80
     * Arguments.
81
     *
82
     * @return array
83
     */
84
    public function getArguments()
85
    {
86
        return $this->argumentsStringToArray($this->arguments);
87
    }
88
89
    /**
90
     * RequestMethod.
91
     *
92
     * @return string
93
     */
94
    public function getRequestMethod()
95
    {
96
        return $this->requestMethod;
97
    }
98
99
    /**
100
     * Path.
101
     *
102
     * @return string
103
     */
104
    public function getPath()
105
    {
106
        return $this->path;
107
    }
108
109
    /**
110
     * Host.
111
     *
112
     * @return string
113
     */
114
    public function getHost()
115
    {
116
        return $this->host;
117
    }
118
119
120
    private function getCliInfo()
121
    {
122
        $this->arguments = $this->argumentsToString($_SERVER['argv']);
123
    }
124
125
    private function argumentsToString(array $args): string
126
    {
127
        $tokens = array_map(
128
            function ($token) {
129
                if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
130
                    return $match[1].$this->escapeToken($match[2]);
131
                }
132
133
                if ($token && $token[0] !== '-') {
134
                    return $this->escapeToken($token);
135
                }
136
137
                return $token;
138
            },
139
            $args
140
        );
141
142
        return implode(' ', $tokens);
143
    }
144
145
    private function escapeToken(string $token): string
146
    {
147
        return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
148
    }
149
150
    private function argumentsStringToArray(string $argsString): array
151
    {
152
        return explode(' ', $argsString);
153
    }
154
155
    private function getHttpInfo()
156
    {
157
        $this->requestMethod = $_SERVER['REQUEST_METHOD'];
158
        $this->host = $_SERVER['HTTP_HOST'];
159
        $this->path = $_SERVER['SCRIPT_URL'];
160
    }
161
162
    public static function inCliContext(): bool
163
    {
164
        return 'cli' === self::getSapiName() || defined('STDIN');
165
    }
166
167
    private static function getSapiName(): string
168
    {
169
        return php_sapi_name();
170
    }
171
}
172