Passed
Push — master ( 8168c4...abbd19 )
by Dev
01:59
created

LogLine::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace PiedWeb\LogsAnalyzer;
4
5
class LogLine
6
{
7
    protected $ip;
8
    protected $logname;
9
    protected $user;
10
    protected $date;
11
    protected $time;
12
    protected $domain;
13
    protected $method;
14
    protected $url;
15
    protected $httpVersion;
16
    protected $status;
17
    protected $responseBytes;
18
    protected $referer;
19
    protected $useragent;
20
    protected $hit;
21
22 3
    public function __construct(?string $domain = null)
23
    {
24 3
        $this->domain = $domain;
25 3
    }
26
27
    public function getKeys()
28
    {
29
        return array_keys(get_object_vars($this));
30
    }
31
32
    public function get()
33
    {
34
        return array_values(get_object_vars($this));
35
    }
36
37
    public function getIp()
38
    {
39
        return $this->ip;
40
    }
41
42 3
    public function setHost($ip)
43
    {
44 3
        $this->setIp($ip);
45 3
    }
46
47 3
    public function setIp($ip)
48
    {
49 3
        $this->ip = $ip;
50 3
    }
51
52
    public function getLogname()
53
    {
54
        return $this->logname;
55
    }
56
57 3
    public function setLogname($logname)
58
    {
59 3
        if ('-' != $logname) {
60
            $this->logname = $logname;
61
        }
62 3
    }
63
64
    public function getUser()
65
    {
66
        return $this->user;
67
    }
68
69 3
    public function setUser($user)
70
    {
71 3
        if ('-' != $user) {
72
            $this->user = $user;
73
        }
74 3
    }
75
76
    public function getTime()
77
    {
78
        return $this->time;
79
    }
80
81 3
    public function setTime($time)
82
    {
83 3
        list($date, $time) = explode(':', $time, 2);
84 3
        $this->time = $time;
85
86 3
        $date = \DateTime::createFromFormat('j/M/Y', $date);
87 3
        $this->date = $date->format('Ymd');
88 3
    }
89
90
    public function getDomain()
91
    {
92
        return $this->domain;
93
    }
94
95
    public function setDomain($domain)
96
    {
97
        $this->domain = $domain;
98
    }
99
100 3
    public function setMethod($method)
101
    {
102 3
        $this->method = $method;
103 3
    }
104
105
    public function getMethod()
106
    {
107
        return $this->method;
108
    }
109
110 3
    public function setUrl($url)
111
    {
112 3
        $this->url = $url;
113 3
    }
114
115
    public function getUrl()
116
    {
117
        return $this->url;
118
    }
119
120 3
    public function setRequest($request)
121
    {
122 3
        $request = str_replace(['http://'.$this->domain.'/', 'https://'.$this->domain.'/'], '/', $request);
123 3
        list($method, $url, $httpVersion) = explode(' ', $request);
124 3
        $this->setMethod($method);
125 3
        $this->setUrl($url);
126 3
        $this->setHttpVersion($httpVersion);
127 3
    }
128
129
    public function getHttpVersion()
130
    {
131
        return $this->httpVersion;
132
    }
133
134 3
    public function setHttpVersion($httpVersion)
135
    {
136 3
        $this->httpVersion = $httpVersion;
137 3
    }
138
139
    public function getStatus()
140
    {
141
        return $this->status;
142
    }
143
144 3
    public function setStatus($status)
145
    {
146 3
        $this->status = $status;
147 3
    }
148
149
    public function getResponseBytes()
150
    {
151
        return $this->responseBytes;
152
    }
153
154 3
    public function setResponseBytes($responseBytes)
155
    {
156 3
        $this->responseBytes = $responseBytes;
157 3
    }
158
159
    public function getReferer()
160
    {
161
        return $this->referer;
162
    }
163
164 3
    protected function isInternalReferer($referer)
165
    {
166 3
        return 0 === strpos($referer, 'http://'.$this->domain) || 0 === strpos($referer, 'https://'.$this->domain);
167
    }
168
169 3
    public function setHeaderReferer($referer)
170
    {
171 3
        if (!empty($referer) && '-' != $referer) {
172 3
            $this->setReferer($referer);
173
        }
174 3
    }
175
176 3
    public function setReferer($referer)
177
    {
178 3
        $referer = $this->isInternalReferer($referer) ? null : $referer;
179
180 3
        $this->referer = $referer;
181 3
    }
182
183 3
    public function getUseragent()
184
    {
185 3
        return $this->useragent;
186
    }
187
188 3
    public function setHeaderUserAgent($useragent)
189
    {
190 3
        $this->setUserAgent($useragent);
191 3
    }
192
193 3
    public function setUseragent($useragent)
194
    {
195 3
        $this->useragent = $useragent;
196 3
    }
197
198
    public function getHit()
199
    {
200
        return $this->hit ?? 1;
201
    }
202
203
    public function incrementHit()
204
    {
205
        $this->hit++;
206
    }
207
208
    public function ipAdressNumber($dotted)
209
    {
210
        $dotted = preg_split('/[.]+/', $dotted);
211
212
        return (float) ($dotted[0] * 16777216) + ($dotted[1] * 65536) + ($dotted[2] * 256) + ($dotted[3]);
213
    }
214
}
215