Passed
Pull Request — master (#36)
by Key
04:46
created

AuthenticationLogable::authentications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace KeyShang\AuthenticationLog;
4
5
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
8
trait AuthenticationLogable
9
{
10
    /**
11
     * Get the entity's authentications.
12
     */
13
    public function authentications(): MorphMany
14
    {
15
        /** @var HasRelationships $this */
16
        return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('login_at');
17
    }
18
19
    /**
20
     * The Authentication Log notifications delivery channels.
21
     */
22
    public function notifyAuthenticationLogVia(): array
23
    {
24
        return ['mail'];
25
    }
26
27
    /**
28
     * Get the entity's last login at.
29
     */
30
    public function lastLoginAt()
31
    {
32
        return optional($this->authentications()->first())->login_at;
33
    }
34
35
    /**
36
     * Get the entity's last login ip address.
37
     */
38
    public function lastLoginIp()
39
    {
40
        return optional($this->authentications()->first())->ip_address;
41
    }
42
43
    /**
44
     * Get the entity's previous login at.
45
     */
46
    public function previousLoginAt()
47
    {
48
        return optional($this->authentications()->skip(1)->first())->login_at;
49
    }
50
51
    /**
52
     * Get the entity's previous login ip.
53
     */
54
    public function previousLoginIp()
55
    {
56
        return optional($this->authentications()->skip(1)->first())->ip_address;
57
    }
58
}
59