Passed
Push — master ( b8999c...a2f941 )
by Curtis
09:12 queued 03:15
created

ActivityLog   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeForEvents() 0 4 1
A scopeForRoles() 0 5 1
A scopeBetween() 0 5 1
A scopeForUsers() 0 4 1
1
<?php
2
3
namespace App\Models\enso\activitylogs;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Carbon\Carbon;
7
use LaravelEnso\TrackWho\Traits\CreatedBy;
8
9
class ActivityLog extends Model
10
{
11
    use CreatedBy;
12
13
    protected $guarded = ['id'];
14
15
    protected $casts = ['meta' => 'object'];
16
17
    public function scopeBetween($query, $startDate, $endDate)
18
    {
19
        $query->when($startDate, fn ($query) => $query
20
            ->where('created_at', '>=', Carbon::parse($startDate)))->when($endDate, fn ($query) => $query
21
            ->where('created_at', '<', Carbon::parse($endDate)));
22
    }
23
24
    public function scopeForUsers($query, array $userIds)
25
    {
26
        $query->when(count($userIds) > 0, fn ($query) => $query
27
            ->whereIn('created_by', $userIds));
28
    }
29
30
    public function scopeForEvents($query, array $events)
31
    {
32
        $query->when(count($events) > 0, fn ($query) => $query
33
            ->whereIn('event', $events));
34
    }
35
36
    public function scopeForRoles($query, array $roleIds)
37
    {
38
        $query->when(count($roleIds) > 0, fn ($query) => $query
39
            ->whereHas('createdBy', fn ($query) => $query
40
                ->whereIn('role_id', $roleIds)));
41
    }    
42
}
43