| Total Complexity | 5 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | trait LoginLoggable |
||
| 13 | { |
||
| 14 | 7 | public function loginLogs(): MorphMany |
|
| 15 | { |
||
| 16 | 7 | return $this->morphMany(UserLoginLog::class, 'loggable'); |
|
|
|
|||
| 17 | } |
||
| 18 | |||
| 19 | 5 | public function logLogin($seconds = null): ?UserLoginLog |
|
| 20 | { |
||
| 21 | 5 | $log = null; |
|
| 22 | 5 | if ($this->isNewLogin()) { |
|
| 23 | 5 | $log = $this->createLoginLog(); |
|
| 24 | } |
||
| 25 | |||
| 26 | 5 | Cacher::setCache($this, $seconds); |
|
| 27 | |||
| 28 | 5 | return $log; |
|
| 29 | } |
||
| 30 | |||
| 31 | 6 | public function createLoginLog(): UserLoginLog |
|
| 32 | { |
||
| 33 | 6 | $agent = new Agent(); |
|
| 34 | 6 | $log = new UserLoginLog([ |
|
| 35 | 6 | 'ip' => request()->ip(), |
|
| 36 | 6 | 'device' => $agent->device(), |
|
| 37 | 6 | 'platform' => $agent->platform(), |
|
| 38 | 6 | 'browser' => $agent->browser(), |
|
| 39 | 6 | 'device_type' => DeviceType::getValue($agent), |
|
| 40 | 6 | 'login_at' => Carbon::now(), |
|
| 41 | ]); |
||
| 42 | |||
| 43 | 6 | $log->platform_version = $agent->version($log->platform); |
|
| 44 | 6 | $log->browser_version = $agent->version($log->browser); |
|
| 45 | |||
| 46 | 6 | $this->loginLogs()->save($log); |
|
| 47 | 6 | return $log; |
|
| 48 | } |
||
| 49 | |||
| 50 | 5 | public function isNewLogin(): bool |
|
| 53 | } |
||
| 54 | } |
||
| 55 |