Passed
Push — main ( bfff7a...f85971 )
by PRATIK
13:07
created

ActivityTable::getActivities()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 16
nop 0
dl 0
loc 24
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\Activity;
4
5
use Carbon\Carbon;
6
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Livewire\Component;
8
use Livewire\WithPagination;
9
use Spatie\Activitylog\Models\Activity;
10
11
class ActivityTable extends Component
12
{
13
    use WithPagination;
14
15
    protected $paginationTheme = 'bootstrap';
16
17
    public $log_name;
18
    public $log_names;
19
20
    public $description;
21
22
    public $model;
23
    public $models;
24
25
    public $user_id;
26
    public $users;
27
28
    public $start_date;
29
    public $end_date;
30
    public $data;
31
    public $interval = 30;
32
    public $to_from = 1;
33
    public $delete_limit = 30;
34
35
    public $activity_count = 0;
36
37
    protected $listeners = ['date_range_filter' => 'dateRangeFilter', 'date_filter' => 'dateFilter'];
38
39
    public function mount($model = null)
40
    {
41
        $this->model = $model;
42
        $activities = Activity::all();
43
        $this->log_names = array_unique($activities->pluck('log_name')->toArray());
44
        $this->models = array_unique($activities->pluck('subject_type')->toArray());
45
        $this->users = User::find($activities->pluck('causer_id')->toArray());
46
    }
47
48
    public function deleteAll()
49
    {
50
        Activity::truncate();
51
        $this->emit('activity_success', 'All Activities Deleted Successfully');
52
    }
53
54
    public function deleteWithLimit()
55
    {
56
        if (!is_null($this->delete_limit)) {
0 ignored issues
show
introduced by
The condition is_null($this->delete_limit) is always false.
Loading history...
57
            Activity::whereDate('created_at', '<', Carbon::now()->subDays($this->delete_limit))->delete();
58
            $this->emit('activity_success', 'All activities Deleted except last ' . $this->delete_limit . 'days activities');
59
        }
60
    }
61
62
    public function dateFilter($date)
63
    {
64
        $date = Carbon::create($date);
65
        $to_from = $this->to_from;
66
67
        if ($to_from == 1) {
68
            $this->start_date = $date;
69
            $this->end_date = $date->addDays($this->interval ?? 30);
70
        } elseif ($to_from == 2) {
71
            $this->end_date = $date;
72
            $this->start_date = $date->subDays($this->interval ?? 30);
73
        }
74
    }
75
    public function dateRangeFilter($start_date, $end_date)
76
    {
77
        $this->start_date = Carbon::create($start_date);
78
        $this->end_date = Carbon::create($end_date);
79
    }
80
81
    public function deleteActivity(Activity $activity)
82
    {
83
        $activity->delete();
84
        $this->emit('activity_success', 'Activity Deleted Successfully');
85
    }
86
    public function render()
87
    {
88
        $activities = $this->getActivities();
89
        return view('adminetic::livewire.admin.activity.activity-table', compact('activities'));
90
    }
91
92
    private function getActivities()
93
    {
94
        $this->resetPage();
95
        $data = Activity::query();
96
        // Filter By Log Name
97
        if (!is_null($this->log_name)) {
98
            $data = $data->where('log_name', $this->log_name);
99
        }
100
        // Filter By Model
101
        if (!is_null($this->model)) {
102
            $data = $data->where('subject_type', $this->model);
103
        }
104
        // Filter By User
105
        if (!is_null($this->user_id)) {
106
            $data = $data->where('causer_id', $this->user_id);
107
        }
108
        // Filter By Date
109
        if (!is_null($this->start_date) && !is_null($this->end_date)) {
110
            $data = $data->whereDate('created_at', [$this->start_date, $this->end_date]);
111
        }
112
        $this->activity_count = with($data)->count();
113
        $this->emit('initialize_activity');
114
        return
115
            $data->latest()->paginate(10);
116
    }
117
}
118