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

Tracking::boot()   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\Models\Base;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Sfneal\Models\Model;
7
use Sfneal\Scopes\CreatedOrderScope;
8
use Sfneal\Users\Models\User;
9
10
abstract class Tracking extends Model
11
{
12
    /**
13
     * The "booting" method of the model.
14
     *
15
     * @return void
16
     */
17
    protected static function boot()
18
    {
19
        parent::boot();
20
        static::addGlobalScope(new CreatedOrderScope());
21
    }
22
23
    /**
24
     * The attributes that should be mutated to dates.
25
     *
26
     * @var array
27
     */
28
    protected $dates = ['deleted_at', 'updated_at', 'created_at'];
29
30
    /**
31
     * The attributes that should be cast to native types.
32
     *
33
     * @var array
34
     */
35
    protected $casts = [
36
        'model_changes'   => 'array',
37
        'request_payload' => 'array',
38
    ];
39
40
    protected $appends = [
41
        'has_model_changes',
42
    ];
43
44
    /**
45
     * User relationship.
46
     *
47
     * @return BelongsTo|User
48
     */
49
    public function user()
50
    {
51
        return $this->belongsTo(User::class, 'user_id', 'id');
52
    }
53
54
    /**
55
     * Mutate date into human readable format.
56
     *
57
     * @return false|string
58
     */
59
    public function getDateAttribute()
60
    {
61
        return date('Y-m-d', strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

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

61
        return date('Y-m-d', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
62
    }
63
64
    /**
65
     * Mutate time into human readable format.
66
     *
67
     * @return false|string
68
     */
69
    public function getTimeAttribute()
70
    {
71
        return date('h:i a', strtotime($this->created_at));
0 ignored issues
show
Bug introduced by
$this->created_at of type DateTime is incompatible with the type string expected by parameter $datetime of strtotime(). ( Ignorable by Annotation )

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

71
        return date('h:i a', strtotime(/** @scrutinizer ignore-type */ $this->created_at));
Loading history...
72
    }
73
74
    /**
75
     * Mutate model changes array to remove any dates attributes before display.
76
     *
77
     * @param $value
78
     *
79
     * @return array
80
     */
81
    public function getModelChangesAttribute($value)
82
    {
83
        // Remove $dates from returned array
84
        if (isset($value) && ! empty($value)) {
85
            return array_filter(json_decode($value, true), function ($val) {
86
                return ! in_array($val, $this->dates);
87
            }, ARRAY_FILTER_USE_KEY);
88
        } else {
89
            return [];
90
        }
91
    }
92
93
    /**
94
     * Determine if an activity or action instance has model_changes.
95
     *
96
     * @return bool
97
     */
98
    public function getHasModelChangesAttribute()
99
    {
100
        return count($this->model_changes) > 0;
101
    }
102
}
103