LoginLogService::getUserRecentRecord()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace App\Services;
4
5
use App\Repositories\Criteria\Limit;
6
use App\Repositories\Criteria\OrderBy;
7
use App\Repositories\Criteria\Where;
8
use App\Repositories\LoginLogRepository;
9
use Carbon\Carbon;
10
11
class LoginLogService
12
{
13
    /** @var LoginLogRepository */
14
    private $repository;
15
16
    public function __construct()
17
    {
18
        $this->repository = app(LoginLogRepository::class);
19
    }
20
21
    public function getUserRecentRecord($user)
22
    {
23
        $this->repository->clearCriteria();
24
        $this->repository->pushCriteria(new Where('user_id', $user->id));
25
        $this->repository->pushCriteria(new Limit(10));
26
        $this->repository->pushCriteria(new OrderBy('id', 'desc'));
27
28
        return $this->repository->all();
29
    }
30
31
    public function getUserRecordsNumber($user)
32
    {
33
        $this->repository->clearCriteria();
34
        $this->repository->pushCriteria(new Where('user_id', $user->id));
35
36
        return $this->repository->count();
37
    }
38
39
    public function cleanUserLog($user)
40
    {
41
        $this->repository->clearCriteria();
42
        $this->repository->pushCriteria(new Where('user_id', $user->id));
43
        $total = $this->repository->count();
44
45
        if ($total > 10) {
46
            $theDay = Carbon::now()->subDays(90)->setTime(0, 0);
47
            $this->repository->pushCriteria(new Where('created_at', $theDay, '<'));
48
            $this->repository->query()->delete();
49
        }
50
    }
51
}
52