Completed
Pull Request — master (#63)
by Brandon
02:14
created

Device::setCloseTimeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Carbon\Carbon;
8
use Illuminate\Support\Facades\Auth;
9
10
class Device extends Model
11
{
12
    use SoftDeletes;
13
    
14
    /**
15
     * The attributes that should be mutated to dates.
16
     *
17
     * @var array
18
     */
19
    protected $dates = [
20
        'deleted_at'
21
    ];
22
    
23
    /**
24
     * The attributes that should be hidden for arrays.
25
     *
26
     * @var array
27
     */
28
    protected $hidden = ['token'];
29
    
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', 
37
        'time', 'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', 
38
        'light_in', 'light_out', 'cpu_temp', 'temperature', 'humidity', 
39
        'update_rate', 'image_rate', 'sensor_rate'
40
    ];
41
    
42
    /**
43
     * Update the updated_at and created_at timestamps?
44
     *
45
     * @var array
46
     */
47
    public $timestamps = true;
48
    
49
    /**
50
     * Get the location for the device
51
     */
52
    public function location()
53
    {
54
        return $this->belongsTo('App\Location', 'location_id');
55
    }
56
    
57
    /**
58
     * Get the site for the device
59
     */
60
    public function site()
61
    {
62
        return $this->location->site;
0 ignored issues
show
Documentation introduced by
The property location does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
    }
64
    
65
    /**
66
     * Accessor: Get the open time of the device converted to hours and minutes
67
     *
68
     * @param  string $value
69
     * @return string
70
     */
71
    public function getOpenTimeAttribute($value)
72
    {
73
        $time = new Carbon($value, 'UTC');
74
        $time = $time->setTimezone(Auth::user()->timezone);
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...
75
        return $time->format('H:i');
76
    }
77
    
78
    /**
79
     * Accessor: Get the close time of the device converted to hours and minutes
80
     *
81
     * @param  string $value
82
     * @return string
83
     */
84
    public function getCloseTimeAttribute($value)
85
    {
86
        $time = new Carbon($value, 'UTC');
87
        $time = $time->setTimezone(Auth::user()->timezone);
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...
88
        return $time->format('H:i');
89
    }
90
    
91
    /**
92
     * Set the open time to UTC
93
     *
94
     * @param  string  $value
95
     * @return void
96
     */
97
    public function setOpenTimeAttribute($value)
98
    {
99
        $time = new Carbon($value, Auth::user()->timezone);
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...
100
        $time = $time->setTimezone('UTC');
101
        $this->attributes['open_time'] = $time;
102
    }
103
    
104
    /**
105
     * Set the close time to UTC
106
     *
107
     * @param  string  $value
108
     * @return void
109
     */
110
    public function setCloseTimeAttribute($value)
111
    {
112
        $time = new Carbon($value, Auth::user()->timezone);
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...
113
        $time = $time->setTimezone('UTC');
114
        $this->attributes['close_time'] = $time;
115
    }
116
    
117
    /**
118
     * Scope a query to only include devices belonging to a given location
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $query
121
     * @param int $location_id
122
     * @return \Illuminate\Database\Eloquent\Builder
123
     */
124
    public function scopeByLocation($query, $location_id)
125
    {
126
        return $query->where('location_id', $location_id);
127
    }
128
    
129
    /**
130
     * Scope a query to limit the included columns to only include what is publicly needed to be displayed on the
131
     * dashboard
132
     *
133
     * @param \Illuminate\Database\Eloquent\Builder $query
134
     * @return \Illuminate\Database\Eloquent\Builder
135
     */
136
    public function scopePublicDashData($query)
137
    {
138
        return $query->select([
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
139
                            'id',
140
                            'name',
141
                            'location_id',
142
                            'cover_command',
143
                            'cover_status',
144
                            'temperature',
145
                            'humidity',
146
                            'light_in',
147
                            'open_time',
148
                            'close_time',
149
                            'update_rate',
150
                            'image_rate',
151
                            'sensor_rate',
152
                            'cpu_temp',
153
                        ]);
154
    }
155
    
156
    /**
157
     * Create a new API token for the device.
158
     */
159
    public function generateToken()
160
    {
161
        $this->token = str_random(60);
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<App\Device>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
        $this->save();
163
        
164
        return $this->token;
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<App\Device>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
    }
166
    
167
    /**
168
     * Get a device by uuid
169
     *
170
     * @param string $uuid
171
     * @return Device|Illuminate\Database\Eloquent\Model
172
     */
173
    public static function getDeviceByUUID($uuid)
174
    {
175
        return self::where('uuid', $uuid)->first();
176
    }
177
    
178
    /**
179
     * Get the deviceimage record associated with the device.
180
     */
181
    public function image()
182
    {
183
        return $this->hasOne('App\Deviceimage');
184
    }
185
}
186