Completed
Pull Request — develop (#116)
by Neil
03:19
created

Device::setIpAttribute()   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 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * App\Models\Device
9
 *
10
 * @property integer $device_id
11
 * @property string $hostname
12
 * @property string $sysName
13
 * @property mixed $ip
14
 * @property string $community
15
 * @property string $authlevel
16
 * @property string $authname
17
 * @property string $authpass
18
 * @property string $authalgo
19
 * @property string $cryptopass
20
 * @property string $cryptoalgo
21
 * @property string $snmpver
22
 * @property integer $port
23
 * @property string $transport
24
 * @property integer $timeout
25
 * @property integer $retries
26
 * @property string $bgpLocalAs
27
 * @property string $sysObjectID
28
 * @property string $sysDescr
29
 * @property string $sysContact
30
 * @property string $version
31
 * @property string $hardware
32
 * @property string $features
33
 * @property string $location
34
 * @property string $os
35
 * @property boolean $status
36
 * @property string $status_reason
37
 * @property boolean $ignore
38
 * @property boolean $disabled
39
 * @property integer $uptime
40
 * @property integer $agent_uptime
41
 * @property string $last_polled
42
 * @property string $last_poll_attempted
43
 * @property float $last_polled_timetaken
44
 * @property float $last_discovered_timetaken
45
 * @property string $last_discovered
46
 * @property string $last_ping
47
 * @property float $last_ping_timetaken
48
 * @property string $purpose
49
 * @property string $type
50
 * @property string $serial
51
 * @property string $icon
52
 * @property integer $poller_group
53
 * @property boolean $override_sysLocation
54
 * @property string $notes
55
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
56
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports
57
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Syslog[] $syslogs
58
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlogs
59
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDeviceId($value)
60
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHostname($value)
61
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysName($value)
62
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIp($value)
63
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCommunity($value)
64
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthlevel($value)
65
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthname($value)
66
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthpass($value)
67
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthalgo($value)
68
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptopass($value)
69
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptoalgo($value)
70
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSnmpver($value)
71
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePort($value)
72
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTransport($value)
73
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTimeout($value)
74
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereRetries($value)
75
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereBgpLocalAs($value)
76
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysObjectID($value)
77
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysDescr($value)
78
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysContact($value)
79
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereVersion($value)
80
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHardware($value)
81
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereFeatures($value)
82
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLocation($value)
83
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOs($value)
84
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatus($value)
85
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatusReason($value)
86
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIgnore($value)
87
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDisabled($value)
88
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereUptime($value)
89
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAgentUptime($value)
90
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolled($value)
91
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPollAttempted($value)
92
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolledTimetaken($value)
93
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscoveredTimetaken($value)
94
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscovered($value)
95
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPing($value)
96
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPingTimetaken($value)
97
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePurpose($value)
98
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereType($value)
99
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSerial($value)
100
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIcon($value)
101
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePollerGroup($value)
102
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOverrideSysLocation($value)
103
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereNotes($value)
104
 * @mixin \Eloquent
105
 */
106
class Device extends Model
107
{
108
    /**
109
     * Indicates if the model should be timestamped.
110
     *
111
     * @var bool
112
     */
113
    public $timestamps = false;
114
    /**
115
     * The table associated with the model.
116
     *
117
     * @var string
118
     */
119
    protected $table = 'devices';
120
    /**
121
     * The primary key column name.
122
     *
123
     * @var string
124
     */
125
    protected $primaryKey = 'device_id';
126
127
    /**
128
     * Initialize this class
129
     */
130 20
    public static function boot()
131
    {
132 20
        parent::boot();
133
134 20
        static::deleting(function(Device $device) {
135
            // delete related data
136 1
            $device->ports()->delete();
137 1
            $device->syslogs()->delete();
138 1
            $device->eventlogs()->delete();
139 20
        });
140 20
    }
141
142
    // ---- Define Reletionships ----
143
144
    /**
145
     * Returns a list of users that can access this device.
146
     */
147
    public function users()
148
    {
149
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
150
    }
151
152
    /**
153
     * Returns a list of the ports this device has.
154
     */
155 18
    public function ports()
156
    {
157 18
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
158
    }
159
160
    /**
161
     * Returns a list of the Syslog entries this device has.
162
     */
163 1
    public function syslogs()
164
    {
165 1
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
166
    }
167
168
    /**
169
     * Returns a list of the Eventlog entries this device has.
170
     */
171 1
    public function eventlogs()
172
    {
173 1
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
174
    }
175
176
    /**
177
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
178
     */
179
    public function alerts()
180
    {
181
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
182
    }
183
184
    /**
185
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
186
     */
187
    public function rules()
188
    {
189
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
190
    }
191
192
    /**
193
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
194
     */
195
    public function eventlog()
196
    {
197
        return $this->hasMany('App\Models\General\Eventlog', 'device_id');
198
    }
199
200
    /**
201
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
202
     */
203
    public function sensors()
204 1
    {
205
        return $this->hasMany('App\Models\Sensor', 'device_id');
206 1
    }
207 1
208
    /**
209
     * @return asset
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
210
     */
211
    public function logo()
212 18
    {
213
        $icon = $this->icon;
214 18
        if (isset($icon)) {
215 18
            return asset('images/os/'.$icon.'.png');
216
        }
217
        else {
218
            return asset('images/os/generic.png');
219
        }
220
    }
221
222
    /**
223
     * @param int $seconds
224
     * @return string
225
     */
226
    public function formatUptime($seconds)
227
    {
228
        $from = new \DateTime("@0");
229
        $to = new \DateTime("@$seconds");
230
        return $from->diff($to)->format('%a d, %h h, %i m and %s s');
231
    }
232
233
    // ---- Accessors/Mutators ----
234
235
    public function getIpAttribute($ip)
236
    {
237
        if (!empty($ip)) {
238
            return inet_ntop($ip);
239
        }
240
        return null;
241
    }
242
243
    public function setIpAttribute($ip)
244
    {
245
        $this->attributes['ip'] = inet_pton($ip);
246
    }
247
248
}
249