Completed
Push — laravel-5.4 ( 381f13 )
by Tony
02:37
created

Device::getSensorCountAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
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
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Processor[] $processors
118
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Service[] $services
119
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Storage[] $storage
120
 */
121
class Device extends Model
122
{
123
    /**
124
     * Indicates if the model should be timestamped.
125
     *
126
     * @var bool
127
     */
128
    public $timestamps = false;
129
    /**
130
     * The table associated with the model.
131
     *
132
     * @var string
133
     */
134
    protected $table = 'devices';
135
    /**
136
     * The primary key column name.
137
     *
138
     * @var string
139
     */
140
    protected $primaryKey = 'device_id';
141
142
    /**
143
     * The attributes that are mass assignable.
144
     *
145
     * @var array
146
     */
147
    protected $fillable = ['hostname', 'ip', 'status', 'status_reason'];
148
    protected $appends = ['portCount', 'sensorCount'];
149
150
    /**
151
     * Initialize this class
152
     */
153
    public static function boot()
154
    {
155
        parent::boot();
156
157
        static::deleting(function (Device $device) {
158
            // delete related data
159
            $device->ports()->delete();
160
            $device->syslogs()->delete();
161
            $device->eventlogs()->delete();
162
        });
163
    }
164
165
    // ---- Helper Functions ----
166
167
    /**
168
     * @return string
169
     */
170
    public function logo()
171
    {
172
        $base_name = pathinfo($this->icon, PATHINFO_FILENAME);
173
        $options = [
174
            "images/logos/$base_name.svg",
175
            "images/logos/$base_name.png",
176
            "images/os/$base_name.svg",
177
            "images/os/$base_name.png",
178
        ];
179
180
        foreach ($options as $file) {
181
            if (is_file(public_path()."/$file")) {
182
                return asset($file);
183
            }
184
        }
185
186
        return asset('images/os/generic.svg');
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function statusColour()
193
    {
194
        $status = $this->status;
195
        $ignore = $this->ignore;
196
        $disabled = $this->disabled;
197
        if ($disabled == 1) {
198
            return 'teal';
199
        } elseif ($ignore == 1) {
200
            return 'yellow';
201
        } elseif ($status == 0) {
202
            return 'danger';
203
        } else {
204
            return 'success';
205
        }
206
    }
207
208
    // ---- Accessors/Mutators ----
209
    public function getPortCountAttribute()
210
    {
211
        return is_null($this->portCountRelation) ? 0 : $this->portCountRelation->count;
212
    }
213
214
    public function getSensorCountAttribute()
215
    {
216
        return is_null($this->sensorCountRelation) ? 0 : $this->sensorCountRelation->count;
217
    }
218
219
    public function getIconAttribute($icon)
220
    {
221
        if (isset($icon)) {
222
            return asset("images/os/$icon");
223
        }
224
        return asset('images/os/generic.svg');
225
    }
226
    public function getIpAttribute($ip)
227
    {
228
        if (empty($ip)) {
229
            return null;
230
        }
231
        // @ suppresses warning, inet_ntop() returns false if it fails
232
        return @inet_ntop($ip) ?: null;
233
    }
234
235
    public function setIpAttribute($ip)
236
    {
237
        $this->attributes['ip'] = inet_pton($ip);
238
    }
239
240
    // ---- Query scopes ----
241
242 View Code Duplication
    public function scopeIsUp($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
    {
244
        return $query->where([
245
            ['status', '=', 1],
246
            ['ignore', '=', 0],
247
            ['disabled', '=', 0]
248
        ]);
249
    }
250
251 View Code Duplication
    public function scopeIsDown($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
    {
253
        return $query->where([
254
            ['status', '=', 0],
255
            ['ignore', '=', 0],
256
            ['disabled', '=', 0]
257
        ]);
258
    }
259
260
    public function scopeIsIgnored($query)
261
    {
262
        return $query->where([
263
            ['ignore', '=', 1],
264
            ['disabled', '=', 0]
265
        ]);
266
    }
267
268
    public function scopeNotIgnored($query)
269
    {
270
        return $query->where([
271
            ['ignore', '=', 0]
272
        ]);
273
    }
274
275
    public function scopeIsDisabled($query)
276
    {
277
        return $query->where([
278
            ['disabled', '=', 1]
279
        ]);
280
    }
281
282
    // ---- Define Relationships ----
283
284
    /**
285
     * Relationship to App\Models\Alerting\Alert
286
     *
287
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
288
     */
289
    public function alerts()
290
    {
291
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
292
    }
293
294
    /**
295
     * Relationship to App\Models\General\Eventlog
296
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
297
     */
298
    public function eventlogs()
299
    {
300
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
301
    }
302
303
    /**
304
     * Relationship to App\Models\DeviceGroup
305
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
306
     */
307
    public function groups()
308
    {
309
        return $this->belongsToMany('App\Models\DeviceGroup', 'device_group_device', 'device_id', 'device_group_id');
310
    }
311
312
    /**
313
     * Relationship to App\Models\Port
314
     * Returns a list of the ports this device has.
315
     */
316
    public function ports()
317
    {
318
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
319
    }
320
321
    public function portCountRelation()
322
    {
323
        return $this->hasOne('App\Models\Port', 'device_id')
324
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
325
    }
326
327
    /**
328
     * Relationship to App\Models\Processor
329
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
330
     */
331
    public function processors()
332
    {
333
        return $this->hasMany('App\Models\Processor', 'device_id');
334
    }
335
336
    /**
337
     * Relationship to App\Models\Alerting\Rule
338
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
339
     */
340
    public function rules()
341
    {
342
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
343
    }
344
345
    /**
346
     * Relationship to App\Models\Sensor
347
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
348
     */
349
    public function sensors()
350
    {
351
        return $this->hasMany('App\Models\Sensor', 'device_id');
352
    }
353
354
    public function sensorCountRelation()
355
    {
356
        return $this->hasOne('App\Models\Sensor', 'device_id')
357
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
358
    }
359
360
    /**
361
     * Relationship to App\Models\Service
362
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
363
     */
364
    public function services()
365
    {
366
        return $this->hasMany('App\Models\Service', 'device_id');
367
    }
368
369
    /**
370
     * Relationship to App\Models\Storage
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
    /**
379
     * Relationship to App\Models\General\Syslog
380
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
381
     */
382
    public function syslogs()
383
    {
384
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
385
    }
386
387
    /**
388
     * Relationship to App\Models\User
389
     * Does not include users with global permissions.
390
     *
391
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
392
     */
393
    public function users()
394
    {
395
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
396
    }
397
}
398