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

TrackActionQuery::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
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\TrackActionBuilder;
8
use Sfneal\Tracking\Models\TrackAction;
9
use Sfneal\Tracking\Queries\Base\TrackingQuery;
10
11
class TrackActionQuery extends TrackingQuery
12
{
13
    use ParamGetter;
14
15
    /**
16
     * Relationships that should be eager loaded by default.
17
     */
18
    private const DEFAULT_RELATIONSHIPS = [
19
        'plan',
20
        'planManagement',
21
        'planManagement.plan',
22
        'project',
23
        'task',
24
        'task.project',
25
        'taskRecord',
26
        'taskRecord.task',
27
        'taskRecord.task.project',
28
    ];
29
30
    /**
31
     * Retrieve a Query builder.
32
     *
33
     * @return TrackActionBuilder
34
     */
35
    protected function builder(): TrackActionBuilder
36
    {
37
        return TrackAction::query()
38
            ->with($this->relationships ?? self::DEFAULT_RELATIONSHIPS);
39
    }
40
41
    /**
42
     * Retrieve a TrackActivity by table query result set.
43
     *
44
     * @return TrackActionBuilder
45
     */
46
    public function execute(): TrackActionBuilder
47
    {
48
        $tracking = $this->builder();
49
50
        // Table
51
        if ($table = self::getParam($this->request, $this->parameters, 'table')) {
52
            $tracking->whereModelTable($table);
53
        }
54
55
        // Key
56
        if ($model_keys = self::getParam($this->request, $this->parameters, 'key')) {
57
            $tracking->whereModelKey($model_keys);
58
        }
59
60
        // Time Period
61
        if ($period = self::getParam($this->request, $this->parameters, 'period')) {
62
            if (is_string($period)) {
63
                $period = TimePeriods::timePeriod($period);
64
            }
65
            $tracking->whereBetween('created_at', $period);
0 ignored issues
show
Bug introduced by
The method whereBetween() does not exist on Sfneal\Tracking\Builders\TrackActionBuilder. 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

65
            $tracking->/** @scrutinizer ignore-call */ 
66
                       whereBetween('created_at', $period);
Loading history...
66
        }
67
68
        return $tracking;
69
    }
70
}
71