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

TrackActivity::newEloquentBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Tracking\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\SoftDeletingScope;
8
use Sfneal\Scopes\CreatedOrderScope;
9
use Sfneal\Scopes\IdOrderScope;
10
use Sfneal\Tracking\Builders\TrackActivityBuilder;
11
use Sfneal\Tracking\Models\Base\AbstractTracking;
12
use Sfneal\Tracking\Models\Traits\TrackingRelationships;
13
14
class TrackActivity extends AbstractTracking
15
{
16
    // todo: add use of polymorphic relationships
17
    use TrackingRelationships;
0 ignored issues
show
introduced by
The trait Sfneal\Tracking\Models\T...s\TrackingRelationships requires some properties which are not provided by Sfneal\Tracking\Models\TrackActivity: $task, $planManagement, $project, $model_table, $plan, $taskRecord
Loading history...
18
19
    /**
20
     * The "booting" method of the model.
21
     *
22
     * @return void
23
     */
24
    protected static function boot()
25
    {
26
        parent::boot();
27
        static::addGlobalScope(new CreatedOrderScope());
28
        static::addGlobalScope(new IdOrderScope());
29
    }
30
31
    protected $table = 'track_activity';
32
    protected $primaryKey = 'track_activity_id';
33
34
    protected $fillable = [
35
        'track_activity_id',
36
        'user_id',
37
        'route',
38
        'description',
39
        'model_table',
40
        'model_key',
41
        'model_changes',
42
        'request_token',
43
    ];
44
45
    /**
46
     * Query Builder.
47
     *
48
     * @param $query
49
     *
50
     * @return TrackActivityBuilder
51
     */
52
    public function newEloquentBuilder($query)
53
    {
54
        return new TrackActivityBuilder($query);
55
    }
56
57
    /**
58
     * @return TrackActivityBuilder|Builder
59
     */
60
    public static function query(): TrackActivityBuilder
61
    {
62
        return parent::query();
63
    }
64
65
    /**
66
     * Related TrackTraffic data.
67
     *
68
     * @return BelongsTo
69
     */
70
    public function tracking()
71
    {
72
        return $this->belongsTo(TrackTraffic::class, 'request_token', 'request_token')
73
            ->withoutGlobalScope(SoftDeletingScope::class);
74
    }
75
76
    /**
77
     * Determine if the Activity has model changes or a request payload.
78
     *
79
     * @return bool
80
     */
81
    public function getHasModelChangesAttribute()
82
    {
83
        return parent::getHasModelChangesAttribute() || $this->hasTrackingLoadedWithPayload();
84
    }
85
86
    /**
87
     * Determine if the 'tracking' relationship has been eager loaded and that it has a request_payload.
88
     *
89
     * @return bool
90
     */
91
    public function hasTrackingLoadedWithPayload()
92
    {
93
        return $this->relationLoaded('tracking') && isset($this->tracking->request_payload);
94
    }
95
}
96