Completed
Push — master ( 59e0d1...2311d4 )
by Dev
15:01 queued 13:47
created

LogLine::setUrl()   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
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    public function __construct(?string $domain = null)
23
    {
24
        $this->domain = $domain;
25
    }
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
    public function setHost($ip)
43
    {
44
        $this->setIp($ip);
45
    }
46
47
    public function setIp($ip)
48
    {
49
        $this->ip = $ip;
50
    }
51
52
    public function getLogname()
53
    {
54
        return $this->logname;
55
    }
56
57
    public function setLogname($logname)
58
    {
59
        if ('-' != $logname) {
60
            $this->logname = $logname;
61
        }
62
    }
63
64
    public function getUser()
65
    {
66
        return $this->user;
67
    }
68
69
    public function setUser($user)
70
    {
71
        if ('-' != $user) {
72
            $this->user = $user;
73
        }
74
    }
75
76
    public function getTime()
77
    {
78
        return $this->time;
79
    }
80
81
    public function setTime($time)
82
    {
83
        list($date, $time) = explode(':', $time, 2);
84
        $this->time = $time;
85
86
        $date = \DateTime::createFromFormat('j/M/Y', $date);
87
        $this->date = $date->format('Ymd');
88
    }
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
    public function getRequest()
101
    {
102
        return $this->request;
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on PiedWeb\LogsAnalyzer\LogLine. Did you maybe forget to declare it?
Loading history...
103
    }
104
105
    public function setMethod($method)
106
    {
107
        $this->method = $method;
108
    }
109
110
    public function getMethod()
111
    {
112
        return $this->method;
113
    }
114
115
    public function setUrl($url)
116
    {
117
        $this->url = $url;
118
    }
119
120
    public function getUrl()
121
    {
122
        return $this->url;
123
    }
124
125
    public function setRequest($request)
126
    {
127
        $request = str_replace(['http://'.$this->domain.'/', 'https://'.$this->domain.'/'], '/', $request);
128
        list($method, $url, $httpVersion) = explode(' ', $request);
129
        $this->setMethod($method);
130
        $this->setUrl($url);
131
        $this->setHttpVersion($httpVersion);
132
    }
133
134
    public function getHttpVersion()
135
    {
136
        return $this->httpVersion;
137
    }
138
139
    public function setHttpVersion($httpVersion)
140
    {
141
        $this->httpVersion = $httpVersion;
142
    }
143
144
    public function getStatus()
145
    {
146
        return $this->status;
147
    }
148
149
    public function setStatus($status)
150
    {
151
        $this->status = $status;
152
    }
153
154
    public function getResponseBytes()
155
    {
156
        return $this->responseBytes;
157
    }
158
159
    public function setResponseBytes($responseBytes)
160
    {
161
        $this->responseBytes = $responseBytes;
162
    }
163
164
    public function getReferer()
165
    {
166
        return $this->referer;
167
    }
168
169
    protected function isInternalReferer($referer)
170
    {
171
        return 0 === strpos($referer, 'http://'.$this->domain) || 0 === strpos($referer, 'https://'.$this->domain);
172
    }
173
174
    public function setHeaderReferer($referer)
175
    {
176
        if (!empty($referer) && '-' != $referer) {
177
            $this->setReferer($referer);
178
        }
179
    }
180
181
    public function setReferer($referer)
182
    {
183
        $referer = $this->isInternalReferer($referer) ? null : $referer;
184
185
        $this->referer = $referer;
186
    }
187
188
    public function getUseragent()
189
    {
190
        return $this->useragent;
191
    }
192
193
    public function setHeaderUserAgent($useragent)
194
    {
195
        $this->setUserAgent($useragent);
196
    }
197
198
    public function setUseragent($useragent)
199
    {
200
        $this->useragent = $useragent;
201
    }
202
203
    public function getHit()
204
    {
205
        return $this->hit ?? 1;
206
    }
207
208
    public function incrementHit()
209
    {
210
        $this->hit++;
211
    }
212
213
    public function ipAdressNumber($dotted)
214
    {
215
        $dotted = preg_split('/[.]+/', $dotted);
216
217
        return (float) ($dotted[0] * 16777216) + ($dotted[1] * 65536) + ($dotted[2] * 256) + ($dotted[3]);
218
    }
219
}
220