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