Passed
Push — master ( 49b4e9...c9293f )
by Darko
11:23
created

UserRoleHistory::recordRoleChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
rs 9.9332
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Support\Carbon;
8
9
/**
10
 * App\Models\UserRoleHistory
11
 *
12
 * @property int $id
13
 * @property int $user_id
14
 * @property int|null $old_role_id
15
 * @property int $new_role_id
16
 * @property string|null $old_expiry_date
17
 * @property string|null $new_expiry_date
18
 * @property string $effective_date
19
 * @property bool $is_stacked
20
 * @property string|null $change_reason
21
 * @property int|null $changed_by
22
 * @property \Carbon\Carbon|null $created_at
23
 * @property \Carbon\Carbon|null $updated_at
24
 * @property-read User $user
25
 * @property-read User|null $changedByUser
26
 */
27
class UserRoleHistory extends Model
28
{
29
    protected $table = 'user_role_history';
30
31
    protected $fillable = [
32
        'user_id',
33
        'old_role_id',
34
        'new_role_id',
35
        'old_expiry_date',
36
        'new_expiry_date',
37
        'effective_date',
38
        'is_stacked',
39
        'change_reason',
40
        'changed_by',
41
    ];
42
43
    protected $casts = [
44
        'old_expiry_date' => 'datetime',
45
        'new_expiry_date' => 'datetime',
46
        'effective_date' => 'datetime',
47
        'is_stacked' => 'boolean',
48
    ];
49
50
    /**
51
     * Get the user this history belongs to
52
     */
53
    public function user(): BelongsTo
54
    {
55
        return $this->belongsTo(User::class);
56
    }
57
58
    /**
59
     * Get the admin who made this change
60
     */
61
    public function changedByUser(): BelongsTo
62
    {
63
        return $this->belongsTo(User::class, 'changed_by');
64
    }
65
66
    /**
67
     * Get the old role
68
     */
69
    public function oldRole(): BelongsTo
70
    {
71
        return $this->belongsTo(\Spatie\Permission\Models\Role::class, 'old_role_id');
72
    }
73
74
    /**
75
     * Get the new role
76
     */
77
    public function newRole(): BelongsTo
78
    {
79
        return $this->belongsTo(\Spatie\Permission\Models\Role::class, 'new_role_id');
80
    }
81
82
    /**
83
     * Record a role change
84
     */
85
    public static function recordRoleChange(
86
        int $userId,
87
        ?int $oldRoleId,
88
        int $newRoleId,
89
        ?Carbon $oldExpiryDate,
90
        ?Carbon $newExpiryDate,
91
        Carbon $effectiveDate,
92
        bool $isStacked = false,
93
        ?string $changeReason = null,
94
        ?int $changedBy = null
95
    ): self {
96
        return static::create([
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::create(ar...ged_by' => $changedBy)) could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the type-hinted return App\Models\UserRoleHistory. Consider adding an additional type-check to rule them out.
Loading history...
97
            'user_id' => $userId,
98
            'old_role_id' => $oldRoleId,
99
            'new_role_id' => $newRoleId,
100
            'old_expiry_date' => $oldExpiryDate,
101
            'new_expiry_date' => $newExpiryDate,
102
            'effective_date' => $effectiveDate,
103
            'is_stacked' => $isStacked,
104
            'change_reason' => $changeReason,
105
            'changed_by' => $changedBy,
106
        ]);
107
    }
108
109
    /**
110
     * Get role history for a user
111
     */
112
    public static function getUserHistory(int $userId)
113
    {
114
        return static::where('user_id', $userId)
115
            ->orderBy('effective_date', 'desc')
116
            ->get();
117
    }
118
119
    /**
120
     * Get stacked role changes for a user
121
     */
122
    public static function getUserStackedChanges(int $userId)
123
    {
124
        return static::where('user_id', $userId)
125
            ->where('is_stacked', true)
126
            ->orderBy('effective_date', 'desc')
127
            ->get();
128
    }
129
}
130
131