Completed
Push — master ( 6e8adf...c24137 )
by
unknown
27s
created

Location::getUpdatedAtHumanAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 6
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Activitylog\Traits\LogsActivity;
7
use Illuminate\Support\Facades\Auth;
8
9
class Location extends Model
10
{
11
    use LogsActivity;
12
    
13
    /**
14
     * The attributes that are mass assignable.
15
     *
16
     * @var array
17
     */
18
    protected $fillable = [
19
        'name', 'site_id'
20
    ];
21
    
22
    /**
23
     * The attributes to ignore in the Activity Log
24
     *
25
     * @var array
26
     */
27
    protected static $ignoreChangedAttributes = [ 'updated_at' ];
28
    
29
    /**
30
     * The attributes to log in the Activity Log
31
     *
32
     * @var array
33
     */
34
    protected static $logAttributes = [
35
        'name', 'site_id'
36
    ];
37
    
38
    /**
39
     * Only log those that have actually changed after the update.
40
     *
41
     * @var array
42
     */
43
    protected static $logOnlyDirty = true;
44
    
45
    /**
46
     * Update the updated_at and created_at timestamps?
47
     *
48
     * @var array
49
     */
50
    public $timestamps = true;
51
    
52
    /**
53
     * Get the site for the location
54
     */
55
    public function site()
56
    {
57
        return $this->belongsTo('App\Site', 'site_id');
58
    }
59
    
60
    /**
61
     * Get devices for the location
62
     */
63
    public function devices()
64
    {
65
        return $this->hasMany('App\Device');
66
    }
67
    
68
    /**
69
     * Scope a query to only include locations belonging to a given site
70
     *
71
     * @param \Illuminate\Database\Eloquent\Builder $query
72
     * @param int $site_id
73
     * @return \Illuminate\Database\Eloquent\Builder
74
     */
75
    public function scopeBySite($query, $site_id)
76
    {
77
        return $query->where('site_id', $site_id);
78
    }
79
    
80
    /**
81
     * Accessor: Get the location's last update time in seconds/minutes/hours since update or converted to user
82
     * friendly readable format.
83
     * If the time is less then a day old then display time since it last updated
84
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
85
     * and using the user's preferred timezone
86
     *
87
     *
88
     * @return string
89
     */
90 View Code Duplication
    public function getUpdatedAtHumanAttribute()
91
    {
92
        if ($this->updated_at->diffInDays() > 0) {
93
                    return $this->updated_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
94
        } else {
95
                    return $this->updated_at->diffForHumans();
96
        }
97
    }
98
    
99
    /**
100
     * Accessor: Get the location's creation time in seconds/minutes/hours since update or converted to user
101
     * friendly readable format.
102
     * If the time is less then a day old then display time since creation
103
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
104
     * and using the user's preferred timezone
105
     *
106
     *
107
     * @return string
108
     */
109 View Code Duplication
    public function getCreatedAtHumanAttribute()
110
    {
111
        if ($this->created_at->diffInDays() > 0) {
112
                    return $this->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
113
        } else {
114
                    return $this->created_at->diffForHumans();
115
        }
116
    }
117
}
118