Test Setup Failed
Push — master ( 9340e9...95bd49 )
by he
05:10
created

AddUserAccessTime::handle()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 17
rs 9.9
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Entities\LoginLog;
6
use App\Entities\User;
7
use Illuminate\Console\Command;
8
use Illuminate\Database\Eloquent\Collection;
9
10
class AddUserAccessTime extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'user:access-time';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'migrate user access time';
25
26
    private $total;
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $this->total = 0;
46
        User::all()->chunk(500)->map(function (Collection $users) {
47
            $this->total = $this->total + $users->count();
48
            $users->load('logs');
49
            foreach ($users as $user) {
50
                /** @var User $user */
51
                if (count($user->logs) == 0) {
52
                    continue;
53
                }
54
                /** @var LoginLog $lastAccess */
55
                $lastAccess = $user->logs->first();
56
                $user->access_at = $lastAccess->created_at;
57
                $user->save();
58
            }
59
            $this->info("process {$this->total}");
60
        });
61
    }
62
63
}
64