Passed
Push — master ( 6b0e6b...43789e )
by Stephen
03:30
created

TrackActionQuery::execute()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 12
nop 3
dl 0
loc 24
rs 9.6111
1
<?php
2
3
namespace Sfneal\Tracking\Queries;
4
5
use Illuminate\Http\Request;
6
use Sfneal\Helpers\Time\TimePeriods;
7
use Sfneal\Queries\AbstractQueryStatic;
8
use Sfneal\Queries\Traits\ParamGetter;
9
use Sfneal\Tracking\Builders\TrackActionBuilder;
10
use Sfneal\Tracking\Models\TrackAction;
11
12
class TrackActionQuery extends AbstractQueryStatic
13
{
14
    use ParamGetter;
15
16
    // todo: add to config
17
    /**
18
     * Relationships that should be eager loaded by default.
19
     */
20
    private const DEFAULT_RELATIONSHIPS = [
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 TrackActivity by table query result set.
34
     *
35
     * @param Request|null $request
36
     * @param array        $parameters
37
     * @param array|null   $relationships
38
     *
39
     * @return TrackActionBuilder
40
     */
41
    public static function execute(Request $request = null, array $parameters = [], array $relationships = null): TrackActionBuilder
42
    {
43
        $tracking = TrackAction::query()
44
            ->with($relationships ?? self::DEFAULT_RELATIONSHIPS);
45
46
        // Table
47
        if ($table = self::getParam($request, $parameters, 'table')) {
48
            $tracking->whereModelTable($table);
49
        }
50
51
        // Key
52
        if ($model_keys = self::getParam($request, $parameters, 'key')) {
53
            $tracking->whereModelKey($model_keys);
54
        }
55
56
        // Time Period
57
        if ($period = self::getParam($request, $parameters, 'period')) {
58
            if (is_string($period)) {
59
                $period = TimePeriods::timePeriod($period);
60
            }
61
            $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

61
            $tracking->/** @scrutinizer ignore-call */ 
62
                       whereBetween('created_at', $period);
Loading history...
62
        }
63
64
        return $tracking;
65
    }
66
}
67