1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Models; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Session |
5
|
|
|
* |
6
|
|
|
* @package Arcanedev\LaravelTracker\Models |
7
|
|
|
* @author ARCANEDEV <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @property int id |
10
|
|
|
* @property string uuid |
11
|
|
|
* @property int user_id |
12
|
|
|
* @property int device_id |
13
|
|
|
* @property int agent_id |
14
|
|
|
* @property string client_ip |
15
|
|
|
* @property int referrer_id |
16
|
|
|
* @property int cookie_id |
17
|
|
|
* @property int geoip_id |
18
|
|
|
* @property int language_id |
19
|
|
|
* @property bool is_robot |
20
|
|
|
* @property \Carbon\Carbon created_at |
21
|
|
|
* @property \Carbon\Carbon updated_at |
22
|
|
|
* |
23
|
|
|
* @property \Arcanedev\LaravelTracker\Models\User user |
24
|
|
|
* @property \Arcanedev\LaravelTracker\Models\Device device |
25
|
|
|
* @property \Arcanedev\LaravelTracker\Models\Agent agent |
26
|
|
|
* @property \Arcanedev\LaravelTracker\Models\Referer referer |
27
|
|
|
* @property \Arcanedev\LaravelTracker\Models\Cookie cookie |
28
|
|
|
* @property \Arcanedev\LaravelTracker\Models\GeoIp geoip |
29
|
|
|
* @property \Arcanedev\LaravelTracker\Models\Language language |
30
|
|
|
*/ |
31
|
|
|
class Session extends AbstractModel |
32
|
|
|
{ |
33
|
|
|
/* ------------------------------------------------------------------------------------------------ |
34
|
|
|
| Traits |
35
|
|
|
| ------------------------------------------------------------------------------------------------ |
36
|
|
|
*/ |
37
|
|
|
use Presenters\SessionPresenter; |
38
|
|
|
|
39
|
|
|
/* ------------------------------------------------------------------------------------------------ |
40
|
|
|
| Properties |
41
|
|
|
| ------------------------------------------------------------------------------------------------ |
42
|
|
|
*/ |
43
|
|
|
/** |
44
|
|
|
* The table associated with the model. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $table = 'sessions'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The attributes that are mass assignable. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $fillable = [ |
56
|
|
|
'uuid', |
57
|
|
|
'user_id', |
58
|
|
|
'device_id', |
59
|
|
|
'language_id', |
60
|
|
|
'agent_id', |
61
|
|
|
'client_ip', |
62
|
|
|
'cookie_id', |
63
|
|
|
'referer_id', |
64
|
|
|
'geoip_id', |
65
|
|
|
'is_robot', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The attributes that should be cast to native types. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $casts = [ |
74
|
|
|
'user_id' => 'integer', |
75
|
|
|
'device_id' => 'integer', |
76
|
|
|
'language_id' => 'integer', |
77
|
|
|
'agent_id' => 'integer', |
78
|
|
|
'cookie_id' => 'integer', |
79
|
|
|
'referer_id' => 'integer', |
80
|
|
|
'geoip_id' => 'integer', |
81
|
|
|
'is_robot' => 'boolean', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/* ------------------------------------------------------------------------------------------------ |
85
|
|
|
| Relationships |
86
|
|
|
| ------------------------------------------------------------------------------------------------ |
87
|
|
|
*/ |
88
|
|
|
/** |
89
|
|
|
* User relationship. |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
92
|
|
|
*/ |
93
|
|
|
public function user() |
94
|
|
|
{ |
95
|
|
|
return $this->belongsTo( |
96
|
|
|
$this->getModelClass(self::MODEL_USER, User::class) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Device relationship. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
104
|
|
|
*/ |
105
|
|
|
public function device() |
106
|
|
|
{ |
107
|
|
|
return $this->belongsTo( |
108
|
|
|
$this->getModelClass(self::MODEL_DEVICE, Device::class) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Agent relationship. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
116
|
|
|
*/ |
117
|
|
|
public function agent() |
118
|
|
|
{ |
119
|
|
|
return $this->belongsTo( |
120
|
|
|
$this->getModelClass(self::MODEL_AGENT, Agent::class) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Referer relationship. |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
128
|
|
|
*/ |
129
|
|
|
public function referer() |
130
|
|
|
{ |
131
|
|
|
return $this->belongsTo( |
132
|
|
|
$this->getModelClass(self::MODEL_REFERER, Referer::class) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Cookie relationship. |
138
|
|
|
* |
139
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
140
|
|
|
*/ |
141
|
|
|
public function cookie() |
142
|
|
|
{ |
143
|
|
|
return $this->belongsTo( |
144
|
|
|
$this->getModelClass(self::MODEL_COOKIE, Cookie::class) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* GeoIp relationship. |
150
|
|
|
* |
151
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
152
|
|
|
*/ |
153
|
|
|
public function geoip() |
154
|
|
|
{ |
155
|
|
|
return $this->belongsTo( |
156
|
|
|
$this->getModelClass(self::MODEL_GEOIP, GeoIp::class), 'geoip_id' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Language relationship. |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
164
|
|
|
*/ |
165
|
|
|
public function language() |
166
|
|
|
{ |
167
|
|
|
return $this->belongsTo( |
168
|
|
|
$this->getModelClass(self::MODEL_LANGUAGE, Language::class) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Session activities relationship. |
174
|
|
|
* |
175
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
176
|
|
|
*/ |
177
|
|
|
public function activities() |
178
|
|
|
{ |
179
|
|
|
return $this->hasMany( |
180
|
|
|
$this->getModelClass(self::MODEL_SESSION_ACTIVITY, SessionActivity::class) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/* ------------------------------------------------------------------------------------------------ |
185
|
|
|
| Check Functions |
186
|
|
|
| ------------------------------------------------------------------------------------------------ |
187
|
|
|
*/ |
188
|
|
|
/** |
189
|
|
|
* Check if the user exists. |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasUser() |
194
|
|
|
{ |
195
|
|
|
return ! is_null($this->user); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if the geoip exists. |
200
|
|
|
* |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function hasGeoip() |
204
|
|
|
{ |
205
|
|
|
return ! is_null($this->geoip); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|