Test Failed
Push — develop ( 55b248...7d9761 )
by Tony
21:25
created

Device::formatUptime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 6
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 integer $port_association_mode
56
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
57
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports
58
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Syslog[] $syslogs
59
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlogs
60
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Alerting\Alert[] $alerts
61
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Alerting\Rule[] $rules
62
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlog
63
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Sensor[] $sensors
64
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDeviceId($value)
65
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHostname($value)
66
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysName($value)
67
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIp($value)
68
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCommunity($value)
69
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthlevel($value)
70
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthname($value)
71
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthpass($value)
72
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthalgo($value)
73
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptopass($value)
74
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptoalgo($value)
75
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSnmpver($value)
76
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePort($value)
77
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTransport($value)
78
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTimeout($value)
79
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereRetries($value)
80
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereBgpLocalAs($value)
81
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysObjectID($value)
82
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysDescr($value)
83
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysContact($value)
84
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereVersion($value)
85
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHardware($value)
86
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereFeatures($value)
87
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLocation($value)
88
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOs($value)
89
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatus($value)
90
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatusReason($value)
91
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIgnore($value)
92
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDisabled($value)
93
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereUptime($value)
94
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAgentUptime($value)
95
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolled($value)
96
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPollAttempted($value)
97
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolledTimetaken($value)
98
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscoveredTimetaken($value)
99
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscovered($value)
100
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPing($value)
101
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPingTimetaken($value)
102
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePurpose($value)
103
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereType($value)
104
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSerial($value)
105
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIcon($value)
106
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePollerGroup($value)
107
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOverrideSysLocation($value)
108
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereNotes($value)
109
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePortAssociationMode($value)
110
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isUp()
111
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isDown()
112
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isIgnored()
113
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device notIgnored()
114
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isDisabled()
115
 * @mixin \Eloquent
116
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DeviceGroup[] $groups
117
 */
118
class Device extends Model
119
{
120
    /**
121
     * Indicates if the model should be timestamped.
122
     *
123
     * @var bool
124
     */
125
    public $timestamps = false;
126
    /**
127
     * The table associated with the model.
128
     *
129
     * @var string
130 20
     */
131
    protected $table = 'devices';
132 20
    /**
133
     * The primary key column name.
134 20
     *
135
     * @var string
136 1
     */
137 1
    protected $primaryKey = 'device_id';
138 1
139 20
    /**
140 20
     * The attributes that are mass assignable.
141
     *
142
     * @var array
143
     */
144
    protected $fillable = ['hostname', 'ip', 'status', 'status_reason'];
145
146
    /**
147
     * Initialize this class
148
     */
149
    public static function boot()
150
    {
151
        parent::boot();
152
153
        static::deleting(function(Device $device) {
154
            // delete related data
155 18
            $device->ports()->delete();
156
            $device->syslogs()->delete();
157 18
            $device->eventlogs()->delete();
158
        });
159
    }
160
161
    /**
162
     * Relationship to App\Models\Port
163 1
     * Returns a list of the ports this device has.
164
     */
165 1
    public function ports()
166
    {
167
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
168
    }
169
170
    /**
171 1
     * Relationship to App\Models\General\Syslog
172
     *
173 1
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
174
     */
175
    public function syslogs()
176
    {
177
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
178
    }
179
180
    // ---- Accessors/Mutators ----
181
182
    /**
183
     * Relationship to App\Models\General\Eventlog
184
     *
185
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
186
     */
187
    public function eventlogs()
188
    {
189
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function logo()
196
    {
197
        $icon = $this->icon;
198
        if (isset($icon)) {
199
            return asset('images/os/'.$icon.'.png');
200
        }
201
        else {
202
            return asset('images/os/generic.png');
203
        }
204 1
    }
205
206 1
    /**
207 1
     * @return string
208
     */
209
    public function status_colour()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
210
    {
211
        $status = $this->status;
212 18
        $ignore = $this->ignore;
213
        $disabled = $this->disabled;
214 18
        if ($disabled == 1) {
215 18
            return 'teal';
216
        } elseif ($ignore == 1) {
217
            return 'yellow';
218
        } elseif ($status == 0) {
219
            return 'danger';
220
        } else {
221
            return 'success';
222
        }
223
    }
224
225
    // ---- Query scopes ----
226
227
    /**
228
     * @param int $seconds
229
     * @return string
230
     */
231
    public function formatUptime($seconds)
232
    {
233
        if (empty($seconds)) {
234
            $seconds = 0;
235
        }
236
        $from = new \DateTime("@0");
237
        $to = new \DateTime("@$seconds");
238
        return $from->diff($to)->format('%a d, %h h, %i m and %s s');
239
    }
240
241
    public function getIpAttribute($ip)
242
    {
243
        if (empty($ip)) {
244
            return null;
245
        }
246
        // @ suppresses warning, inet_ntop() returns false if it fails
247
        return @inet_ntop($ip) ?: null;
248
    }
249
250
    public function setIpAttribute($ip)
251
    {
252
        $this->attributes['ip'] = inet_pton($ip);
253
    }
254
255
    public function scopeIsUp($query)
256
    {
257
        return $query->where([
258
            ['status', '=', 1],
259
            ['ignore', '=', 0],
260
            ['disabled', '=', 0]
261
        ]);
262
    }
263
264
    public function scopeIsDown($query)
265
    {
266
        return $query->where([
267
            ['status', '=', 0],
268
            ['ignore', '=', 0],
269
            ['disabled', '=', 0]
270
        ]);
271
    }
272
273
    // ---- Define Relationships ----
274
275
    public function scopeIsIgnored($query)
276
    {
277
        return $query->where([
278
            ['ignore', '=', 1],
279
            ['disabled', '=', 0]
280
        ]);
281
    }
282
283
    public function scopeNotIgnored($query)
284
    {
285
        return $query->where([
286
            ['ignore', '=', 0]
287
        ]);
288
    }
289
290
    public function scopeIsDisabled($query)
291
    {
292
        return $query->where([
293
            ['disabled', '=', 1]
294
        ]);
295
    }
296
297
    /**
298
     * Relationship to App\Models\Alerting\Alert
299
     *
300
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
301
     */
302
    public function alerts()
303
    {
304
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
305
    }
306
307
    /**
308
     * Relationship to App\Models\DeviceGroup
309
     *
310
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
311
     */
312
    public function groups()
313
    {
314
        return $this->belongsToMany('App\Models\DeviceGroup', 'device_group_device', 'device_id', 'device_group_id');
315
    }
316
317
    /**
318
     * Relationship to App\Models\Alerting\Rule
319
     *
320
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
321
     */
322
    public function rules()
323
    {
324
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
325
    }
326
327
    /**
328
     * Relationship to App\Models\Sensor
329
     *
330
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
331
     */
332
    public function sensors()
333
    {
334
        return $this->hasMany('App\Models\Sensor', 'device_id');
335
    }
336
337
    /**
338
     * Relationship to App\Models\Service
339
     *
340
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
341
     */
342
    public function services()
343
    {
344
        return $this->hasMany('App\Models\Service', 'device_id');
345
    }
346
347
    /**
348
     * Relationship to App\Models\User
349
     * Does not include users with global permissions.
350
     *
351
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
352
     */
353
    public function users()
354
    {
355
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
356
    }
357
358
    /**
359
     * Relationship to App\Models\Processor
360
     *
361
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
362
     */
363
    public function processors()
364
    {
365
        return $this->hasMany('App\Models\Processor', 'device_id');
366
    }
367
368
    /**
369
     * Relationship to App\Models\Storage
370
     *
371
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
372
     */
373
    public function storage()
374
    {
375
        return $this->hasMany('App\Models\Storage', 'device_id');
376
    }
377
}
378