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

AbstractTracking::getTimeAttribute()   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 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Tracking\Models\Base;
4
5
use Domain\Users\Models\User;
0 ignored issues
show
Bug introduced by
The type Domain\Users\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Sfneal\Models\AbstractModel;
8
use Sfneal\Scopes\CreatedOrderScope;
9
10
abstract class AbstractTracking extends AbstractModel
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 $time 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 $time 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