Completed
Push — master ( 4005ae...fa1aa2 )
by Anne Jan
12:42
created

User::u2fKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\Relations\HasOne;
8
use Illuminate\Foundation\Auth\User as Authenticatable;
9
use Illuminate\Notifications\Notifiable;
10
use Lahaxearnaud\U2f\Models\U2fKey;
11
12
class User extends Authenticatable
13
{
14
    use Notifiable;
15
16
    /**
17
     * The attributes that are mass assignable.
18
     *
19
     * @var array
20
     */
21
    protected $fillable = [
22
        'first_name', 'middle_name', 'last_name', 'email',
23
        'password', 'cyber_code', 'date_of_birth', 'place_of_birth',
24
    ];
25
26
    /**
27
     * The attributes that should be hidden for arrays.
28
     *
29
     * @var array
30
     */
31
    protected $hidden = [
32
        'password', 'remember_token', 'google2fa_secret',
33
    ];
34
35
    /**
36
     * The "booting" method of the model.
37
     *
38
     * @return void
39
     */
40 81
    protected static function boot()
41
    {
42 81
        parent::boot();
43 81
        static::deleting(function ($user) {
44 3
            $user->expertises()->delete();
45 3
            $user->pcePoints()->delete();
46 81
        });
47 81
    }
48
49
    /**
50
     * Get the route key for the model.
51
     *
52
     * @return string
53
     */
54 14
    public function getRouteKeyName(): string
55
    {
56 14
        return 'cyber_code';
57
    }
58
59
    /**
60
     * @return string
61
     */
62 25
    public function getNameAttribute(): string
63
    {
64 25
        $names = [];
65 25
        if ($this->first_name) {
66 25
            $names[] = $this->first_name;
67
        }
68 25
        if ($this->middle_name) {
69 1
            $names[] = $this->middle_name;
70
        }
71 25
        if ($this->last_name) {
72 25
            $names[] = $this->last_name;
73
        }
74
75 25
        return implode(' ', $names);
76
    }
77
78
    /**
79
     * Get the users expertises.
80
     *
81
     * @return HasMany
82
     */
83 7
    public function expertises(): HasMany
84
    {
85 7
        return $this->hasMany(Expertise::class);
86
    }
87
88
    /**
89
     * @return array
90
     */
91 1
    public function getCodesAttribute()
92
    {
93 1
        $codes = [];
94 1
        foreach ($this->expertises as $expertise) {
95 1
            $codes[$expertise->code] = $expertise->description;
96
        }
97
98 1
        return $codes;
99
    }
100
101
    /**
102
     * Get the users PCE points.
103
     *
104
     * @return HasMany
105
     */
106 4
    public function pcePoints(): HasMany
107
    {
108 4
        return $this->hasMany(PcePoint::class);
109
    }
110
111
    /**
112
     * @param $value
113
     */
114 80
    public function setDateOfBirthAttribute($value)
115
    {
116
        try {
117 80
            $date = Carbon::createFromFormat('Y-m-d', $value);
118 1
        } catch (\InvalidArgumentException $exception) {
119 1
            $date = Carbon::createFromFormat('d-m-Y', $value);
120
        }
121 80
        $this->attributes['date_of_birth'] = $date;
122 80
    }
123
124
    /**
125
     * @return HasOne
126
     */
127 24
    public function u2fKey(): HasOne
128
    {
129 24
        return $this->hasOne(U2fKey::class);
130
    }
131
132
    /**
133
     * @return HasOne
134
     */
135 66
    public function twoFAKey(): HasOne
136
    {
137 66
        return $this->hasOne(TwoFAKey::class);
138
    }
139
}
140