1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality to keep track of visitor sessions of registered users |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
use BZIon\Model\Column\Timestamp; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A player's visit on the website |
12
|
|
|
* @package BZiON\Models |
13
|
|
|
*/ |
14
|
|
|
class Visit extends Model |
15
|
|
|
{ |
16
|
|
|
use Timestamp; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The id of the visiting user |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $player; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The ip of the visiting user |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $ip; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The host of the visiting user |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $host; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The user agent of the visiting user |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $user_agent; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The HTTP_REFERER of the visiting user |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $referer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The name of the database table used for queries |
50
|
|
|
*/ |
51
|
|
|
const TABLE = "visits"; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
protected function assignResult($visit) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->player = $visit['player']; |
59
|
|
|
$this->ip = $visit['ip']; |
60
|
|
|
$this->host = $visit['host']; |
61
|
|
|
$this->user_agent = $visit['user_agent']; |
62
|
|
|
$this->referer = $visit['referer']; |
63
|
|
|
$this->timestamp = TimeDate::fromMysql($visit['timestamp']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Enter a new visit into the database |
68
|
|
|
* @param int $visitor The visitor's id |
69
|
|
|
* @param string $ip The visitor's ip address |
70
|
|
|
* @param string $host The visitor's host |
71
|
|
|
* @param string $user_agent The visitor's user agent |
72
|
|
|
* @param string $referrer The HTTP_REFERRER of the visit |
73
|
|
|
* @param string $timestamp The timestamp of the visit |
74
|
|
|
* @return Visit An object representing the visit that was just entered |
75
|
|
|
*/ |
76
|
|
|
public static function enterVisit($visitor, $ip, $host, $user_agent, $referrer, $timestamp = "now") |
77
|
|
|
{ |
78
|
|
|
$timestamp = TimeDate::from($timestamp); |
79
|
|
|
|
80
|
|
|
return self::create(array( |
81
|
|
|
'player' => $visitor, |
82
|
|
|
'ip' => $ip, |
83
|
|
|
'host' => $host, |
84
|
|
|
'user_agent' => $user_agent, |
85
|
|
|
'referer' => $referrer, |
86
|
|
|
'timestamp' => $timestamp->toMysql(), |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get a query builder for players |
92
|
|
|
* @return QueryBuilder |
93
|
|
|
*/ |
94
|
|
|
public static function getQueryBuilder() |
95
|
|
|
{ |
96
|
|
|
return new VisitQueryBuilder('Visit', array( |
97
|
|
|
'columns' => array( |
98
|
|
|
'ip' => 'ip', |
99
|
|
|
'timestamp' => 'timestamp' |
100
|
|
|
), |
101
|
|
|
'name' => 'name', |
102
|
|
|
)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the visiting player |
107
|
|
|
* @return Player |
108
|
|
|
*/ |
109
|
|
|
public function getPlayer() |
110
|
|
|
{ |
111
|
|
|
return Player::get($this->player); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the IP address of the player |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getIpAddress() |
119
|
|
|
{ |
120
|
|
|
return $this->ip; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the visiting host |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getHost() |
128
|
|
|
{ |
129
|
|
|
return $this->host; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the visitor's user agent |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getUserAgent() |
137
|
|
|
{ |
138
|
|
|
return $this->user_agent; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the visitor's referer or referrer |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getReferrer() |
146
|
|
|
{ |
147
|
|
|
return $this->referer; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.