Passed
Push — master ( 2c2f0a...77dc50 )
by Stephen
01:46 queued 14s
created

TrackActivity::trackable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Tracking\Models;
4
5
use Database\Factories\TrackActivityFactory;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
use Illuminate\Database\Eloquent\SoftDeletingScope;
11
use Sfneal\Scopes\CreatedOrderScope;
12
use Sfneal\Scopes\IdOrderScope;
13
use Sfneal\Tracking\Builders\TrackActivityBuilder;
14
use Sfneal\Tracking\Models\Base\Tracking;
15
use Sfneal\Tracking\Utils\ModelAdapter;
16
17
class TrackActivity extends Tracking
18
{
19
    use HasFactory;
20
21
    /**
22
     * The "booting" method of the model.
23
     *
24
     * @return void
25
     */
26
    protected static function boot()
27
    {
28
        parent::boot();
29
        static::addGlobalScope(new CreatedOrderScope());
30
        static::addGlobalScope(new IdOrderScope());
31
    }
32
33
    protected $table = 'track_activity';
34
    protected $primaryKey = 'track_activity_id';
35
36
    protected $fillable = [
37
        'track_activity_id',
38
        'user_id',
39
        'route',
40
        'description',
41
        'model_changes',
42
        'request_token',
43
        'trackable_id',
44
        'trackable_type',
45
    ];
46
47
    /**
48
     * The attributes that should be cast to native types.
49
     *
50
     * @var array
51
     */
52
    protected $casts = [
53
        'user_id' => 'int',
54
        'model_changes' => 'array',
55
        'trackable_id' => 'int',
56
    ];
57
58
    /**
59
     * Create a new factory instance for the model.
60
     *
61
     * @return TrackActivityFactory
62
     */
63
    protected static function newFactory(): TrackActivityFactory
64
    {
65
        return new TrackActivityFactory();
66
    }
67
68
    /**
69
     * Query Builder.
70
     *
71
     * @param $query
72
     *
73
     * @return TrackActivityBuilder
74
     */
75
    public function newEloquentBuilder($query)
76
    {
77
        return new TrackActivityBuilder($query);
78
    }
79
80
    /**
81
     * @return TrackActivityBuilder|Builder
82
     */
83
    public static function query(): TrackActivityBuilder
84
    {
85
        return parent::query();
86
    }
87
88
    /**
89
     * Get the owning trackable model.
90
     *
91
     * @return MorphTo
92
     */
93
    public function trackable(): MorphTo
94
    {
95
        return $this->morphTo();
96
    }
97
98
    /**
99
     * Related TrackTraffic data.
100
     *
101
     * @return BelongsTo
102
     */
103
    public function tracking(): BelongsTo
104
    {
105
        return $this->belongsTo(ModelAdapter::TrackTraffic(), 'request_token', 'request_token')
106
            ->withoutGlobalScope(SoftDeletingScope::class);
107
    }
108
109
    /**
110
     * Determine if the Activity has model changes or a request payload.
111
     *
112
     * @return bool
113
     */
114
    public function getHasModelChangesAttribute()
115
    {
116
        return parent::getHasModelChangesAttribute() || $this->hasTrackingLoadedWithPayload();
117
    }
118
119
    /**
120
     * Determine if the 'tracking' relationship has been eager loaded and that it has a request_payload.
121
     *
122
     * @return bool
123
     */
124
    public function hasTrackingLoadedWithPayload()
125
    {
126
        return $this->relationLoaded('tracking') && isset($this->tracking->request_payload);
0 ignored issues
show
Bug introduced by
The property tracking does not seem to exist on Sfneal\Tracking\Models\TrackActivity. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
127
    }
128
}
129