Passed
Push — master ( 0bc69d...cc8e66 )
by Stephen
56s queued 13s
created

TrackActivityQuery::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sfneal\Tracking\Queries;
4
5
use Sfneal\Helpers\Time\TimePeriods;
6
use Sfneal\Queries\Traits\ParamGetter;
7
use Sfneal\Tracking\Builders\TrackActivityBuilder;
8
use Sfneal\Tracking\Models\TrackActivity;
9
use Sfneal\Tracking\Queries\Base\TrackingQuery;
10
11
class TrackActivityQuery extends TrackingQuery
12
{
13
    use ParamGetter;
14
15
    /**
16
     * Relationships that should be eager loaded by default.
17
     */
18
    private const DEFAULT_RELATIONSHIPS = [
19
        'user',
20
        'tracking',
21
        'plan',
22
        'planManagement',
23
        'planManagement.plan',
24
        'project',
25
        'task',
26
        'task.project',
27
        'taskRecord',
28
        'taskRecord.task',
29
        'taskRecord.task.project',
30
    ];
31
32
    /**
33
     * Retrieve a Query builder.
34
     *
35
     * @return TrackActivityBuilder
36
     */
37
    protected function builder(): TrackActivityBuilder
38
    {
39
        return TrackActivity::query()
40
            ->with($this->relationships ?? self::DEFAULT_RELATIONSHIPS);
41
    }
42
43
    /**
44
     * Retrieve a TrackActivity by table query result set.
45
     *
46
     * @return TrackActivityBuilder
47
     */
48
    public function execute(): TrackActivityBuilder
49
    {
50
        $tracking = $this->builder();
51
52
        // Table
53
        if ($table = self::getParam($this->request, $this->parameters, 'table')) {
54
            $tracking->whereModelTable($table);
55
        }
56
57
        // User
58
        if ($user = self::getParam($this->request, $this->parameters, 'user')) {
59
            $tracking->whereUser($user);
60
        }
61
62
        // User(s)
63
        if ($users = self::getParam($this->request, $this->parameters, 'users')) {
64
            $tracking->whereUserIn($users);
65
        }
66
67
        // Key
68
        if ($model_keys = self::getParam($this->request, $this->parameters, 'key')) {
69
            $tracking->whereModelKey($model_keys);
70
        }
71
72
        // Time Period
73
        if ($period = self::getParam($this->request, $this->parameters, 'period')) {
74
            if (is_string($period)) {
75
                $period = TimePeriods::timePeriod($period);
76
            }
77
            $tracking->whereBetween('created_at', $period);
0 ignored issues
show
Bug introduced by
The method whereBetween() does not exist on Sfneal\Tracking\Builders\TrackActivityBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $tracking->/** @scrutinizer ignore-call */ 
78
                       whereBetween('created_at', $period);
Loading history...
78
        }
79
80
        return $tracking;
81
    }
82
}
83