Completed
Pull Request — master (#116)
by
unknown
02:19
created

SensorData::getUpdatedAtHumanAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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 SensorData extends Model 
10
{
11
    //use LogsActivity;
12
13
    protected $table = 'sensor_data';
14
15
    /**
16
     * Update the updated_at and created_at timestamps?
17
     *
18
     * @var array
19
     */
20
    public $timestamps = true;
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = array('sensor_id', 'value');
28
29
    /**
30
     * The attributes that are visible.
31
     *
32
     * @var array
33
     */
34
    protected $visible = array('id', 'sensor_id', 'value', 'created_at', 'updated_at');
35
    
36
    /**
37
     * The attributes to log in the Activity Log
38
     *
39
     * @var array
40
     */
41
    protected static $logAttributes = array('id', 'sensor_id', 'value', 'created_at', 'updated_at');
42
    
43
    /**
44
     * Only log those that have actually changed after the update.
45
     *
46
     * @var array
47
     */
48
    protected static $logOnlyDirty = true;
49
50
    /**
51
     * Get the device associated with the sensor.
52
     */
53
    public function sensor()
54
    {
55
        return $this->belongsTo('App\Sensor');
56
    }
57
    
58
    /**
59
     * Accessor: Get the sensor data's last update time in seconds/minutes/hours since update or converted to user
60
     * friendly readable format.
61
     * If the time is less then a day old then display time since it last updated
62
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
63
     * and using the user's preferred timezone
64
     *
65
     *
66
     * @return string
67
     */
68 View Code Duplication
    public function getUpdatedAtHumanAttribute()
69
    {
70
        if ($this->updated_at->diffInDays() > 0)
71
            return $this->updated_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
        else
73
            return $this->updated_at->diffForHumans();
74
    }
75
    
76
    /**
77
     * Accessor: Get the sensor data's creation time in seconds/minutes/hours since update or converted to user
78
     * friendly readable format.
79
     * If the time is less then a day old then display time since creation
80
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
81
     * and using the user's preferred timezone
82
     *
83
     *
84
     * @return string
85
     */
86 View Code Duplication
    public function getCreatedAtHumanAttribute()
87
    {
88
        if ($this->created_at->diffInDays() > 0)
89
            return $this->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
        else
91
            return $this->created_at->diffForHumans();
92
    }
93
}