UserSecurity::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 5
1
<?php
2
3
namespace App\Models;
4
5
/**
6
 * Class UserSecurity.
7
 */
8
class UserSecurity extends ChocolateyModel
9
{
10
    /**
11
     * Disable Timestamps.
12
     *
13
     * @var bool
14
     */
15
    public $timestamps = false;
16
17
    /**
18
     * The table associated with the model.
19
     *
20
     * @var string
21
     */
22
    protected $table = 'chocolatey_users_security';
23
24
    /**
25
     * Primary Key of the Table.
26
     *
27
     * @var string
28
     */
29
    protected $primaryKey = 'user_id';
30
31
    /**
32
     * The attributes excluded from the model's JSON form.
33
     *
34
     * @var array
35
     */
36
    protected $hidden = ['user_id'];
37
38
    /**
39
     * The Appender(s) of the Model.
40
     *
41
     * @var array
42
     */
43
    protected $appends = [
44
        'trustedDevices',
45
    ];
46
47
    /**
48
     * The attributes that are mass assignable.
49
     *
50
     * @var array
51
     */
52
    protected $fillable = ['user_id', 'firstQuestion', 'secondQuestion', 'firstAnswer', 'secondAnswer'];
53
54
    /**
55
     * Get Trusted Devices.
56
     *
57
     * @return array
58
     */
59
    public function getTrustedDevicesAttribute(): array
60
    {
61
        return TrustedDevice::where('user_id', $this->attributes['user_id'])->get(['ip_address'])->map(function ($item) {
62
            return $item->ip_address;
63
        })->toArray();
64
    }
65
66
    /**
67
     * Store a new User Security Metadata.
68
     *
69
     * @param int    $userId
70
     * @param int    $firstQuestion
71
     * @param int    $secondQuestion
72
     * @param string $firstAnswer
73
     * @param string $secondAnswer
74
     *
75
     * @return UserSecurity
76
     */
77
    public function store(int $userId, int $firstQuestion, int $secondQuestion, string $firstAnswer, string $secondAnswer): UserSecurity
78
    {
79
        $this->attributes['user_id'] = $userId;
80
        $this->attributes['firstQuestion'] = $firstQuestion;
81
        $this->attributes['secondQuestion'] = $secondQuestion;
82
        $this->attributes['firstAnswer'] = $firstAnswer;
83
        $this->attributes['secondAnswer'] = $secondAnswer;
84
        $this->timestamps = false;
85
86
        return $this;
87
    }
88
}
89