Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ProcessLoginHistoryAsyncJob | |
0.00% |
0 / 13 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| backoff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| created | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| updateUserInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parseFields | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Http\Models\Auth\LoginHistory; |
| 6 | use Illuminate\Bus\Queueable; |
| 7 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | use Illuminate\Queue\InteractsWithQueue; |
| 10 | use Illuminate\Queue\SerializesModels; |
| 11 | use App\Http\Models\UserInfo; |
| 12 | use App\Traits\ObjectMapper; |
| 13 | |
| 14 | class ProcessLoginHistoryAsyncJob implements ShouldQueue |
| 15 | { |
| 16 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ObjectMapper; |
| 17 | |
| 18 | public $tries = 5; |
| 19 | |
| 20 | public function __construct( |
| 21 | public LoginHistory $loginHistory, |
| 22 | public string $action |
| 23 | ) {} |
| 24 | |
| 25 | public function handle(): void |
| 26 | { |
| 27 | if ($this->action === 'created') { |
| 28 | $this->created($this->loginHistory); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public function backoff() |
| 33 | { |
| 34 | return [10, 30, 60, 110, 300]; |
| 35 | } |
| 36 | |
| 37 | private function created(LoginHistory $loginHistory): void |
| 38 | { |
| 39 | $created = $loginHistory->toArray(); |
| 40 | |
| 41 | $parsedFields = $this->parseFields($created); |
| 42 | |
| 43 | $this->updateUserInfo($parsedFields, $loginHistory); |
| 44 | } |
| 45 | |
| 46 | private function updateUserInfo(array $data, LoginHistory $loginHistory) |
| 47 | { |
| 48 | UserInfo::where('user_id', $loginHistory->user_id)->update($data); |
| 49 | } |
| 50 | |
| 51 | private function parseFields(array $fields): array |
| 52 | { |
| 53 | $map = [ |
| 54 | 'email' => ['rename' => 'email_used_for_login'], |
| 55 | 'created_at' => ['rename' => 'last_login'], |
| 56 | ]; |
| 57 | |
| 58 | return $this->mapObject($fields, $map); |
| 59 | } |
| 60 | } |