Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| LoginHistoryService | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use App\Http\Models\Auth\LoginHistory; |
| 6 | use App\Http\Models\UserInfo; |
| 7 | use Jenssegers\Agent\Agent; |
| 8 | use MongoDB\BSON\UTCDateTime; |
| 9 | |
| 10 | class LoginHistoryService |
| 11 | { |
| 12 | public function create($user, $email, $signin_source) |
| 13 | { |
| 14 | $agent = new Agent(); |
| 15 | $browser = $agent->browser(); |
| 16 | $platform = $agent->platform(); |
| 17 | $device = $agent->device(); |
| 18 | |
| 19 | $historyData = [ |
| 20 | 'user_id' => $user->id, |
| 21 | 'email' => $user->email, |
| 22 | 'browser' => $browser, |
| 23 | 'browser_version' => $agent->version($browser), |
| 24 | 'platform' => $platform, |
| 25 | 'platform_version' => $agent->version($platform), |
| 26 | 'device' => $agent->version($device), |
| 27 | 'device_version' => $agent->version($device), |
| 28 | 'device_type' => $agent->deviceType(), |
| 29 | ]; |
| 30 | if ($agent->isRobot()) { |
| 31 | $robot = $agent->robot(); |
| 32 | $historyData['robot'] = $robot; |
| 33 | $historyData['robot_version'] = $agent->version($robot); |
| 34 | } |
| 35 | $history = LoginHistory::create($historyData); |
| 36 | |
| 37 | $user->last_login = $history->id; |
| 38 | if (empty($user->signup_source)) { |
| 39 | $user->signup_source = $signin_source; |
| 40 | } |
| 41 | $user->save(); |
| 42 | |
| 43 | $userInfo = UserInfo::where('user_id', $user->id)->first(); |
| 44 | $userInfo->email = $email; |
| 45 | $userInfo->signin_source = $signin_source; |
| 46 | $userInfo->signup_source = $user->signup_source; |
| 47 | $userInfo->last_login = new UTCDateTime(now()->getTimestamp() * 1000); |
| 48 | $userInfo->save(); |
| 49 | } |
| 50 | } |