Completed
Push — master ( 460051...3519da )
by
unknown
19s
created

Device::getDeviceByUUID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
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 Illuminate\Support\Facades\DB;
8
9
class Device extends Model
10
{
11
    use SoftDeletes;
12
    
13
    /**
14
     * The attributes that should be mutated to dates.
15
     *
16
     * @var array
17
     */
18
    protected $dates = [
19
        'deleted_at'
20
    ];
21
    
22
    /**
23
     * The attributes that should be hidden for arrays.
24
     *
25
     * @var array
26
     */
27
    protected $hidden = ['token'];
28
    
29
    /**
30
     * The attributes that are mass assignable.
31
     *
32
     * @var array
33
     */
34
    protected $fillable = [
35
        'name', 'location_id', 'uuid', 'version', 'hostname', 'ip', 'mac_address', 'time', 
36
        'cover_status', 'error_msg', 'limitsw_open', 'limitsw_closed', 
37
        'light_in', 'light_out', 'cpu_temp', 'temperature', 'humidity'
38
    ];
39
    
40
    /**
41
     * The attributes that are for timestamps.
42
     *
43
     * @var array
44
     */
45
    public $timestamps = false;
46
    
47
    /**
48
     * Get the location for the device
49
     */
50
    public function location()
51
    {
52
        return $this->belongsTo('App\Location', 'location_id');
53
    }
54
    
55
    /**
56
     * Get the site for the device
57
     */
58
    public function site()
59
    {
60
        return $this->belongsTo('App\Site');
61
    }
62
    
63
    /**
64
     * Update the device with the matching id with a new location id
65
     *
66
     * @param int $id
67
     * @param int $location_id
68
     */
69
    public function updateLocationID($id, $location_id)
70
    {
71
        
72
        $device = $this->findOrFail($id);
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<App\Device>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
        $device->location_id = $location_id;
74
        $device->save();
75
    }
76
    
77
    /**
78
     * Get all the devices with the supplied location id
79
     *
80
     * @param int $location_id
81
     * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
82
     */
83
    public function getDevicesBasedOnLocation($location_id)
84
    {
85
        return self::where('location_id', $location_id)->get();
86
    }
87
    
88
    /**
89
     * Get the first device with the supplied location id
90
     *
91
     * @param int $location_id
92
     * @return Device|Illuminate\Database\Eloquent\Model
93
     */
94
    public function getFirstDeviceBasedOnLocation($location_id)
95
    {
96
        return self::where('location_id', $location_id)->first();
97
    }
98
    
99
    /**
100
     * Create a new API token for the device.
101
     */
102
    public function generateToken()
103
    {
104
        $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...
105
        $this->save();
106
        
107
        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...
108
    }
109
    
110
    /**
111
     * Get a device by uuid
112
     *
113
     * @param string $uuid
114
     * @return Device|Illuminate\Database\Eloquent\Model
115
     */
116
    public function getDeviceByUUID($uuid)
117
    {
118
        return self::where('uuid', $uuid)->first();
119
    }
120
}
121