Test Failed
Pull Request — develop (#200)
by Tony
04:49
created

Device::scopeNotIgnored()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 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 isNotIgnored()
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
149
    /**
150
     * Initialize this class
151
     */
152
    public static function boot()
153 10
    {
154
        parent::boot();
155 10
156
        static::deleting(function (Device $device) {
157 10
            // delete related data
158
            $device->ports()->delete();
159 1
            $device->syslogs()->delete();
160 1
            $device->eventlogs()->delete();
161 1
        });
162 10
    }
163 10
164
    // ---- Helper Functions ----
165
166
    /**
167
     * @return string
168
     */
169
    public function logo()
170
    {
171
        $base_name = pathinfo($this->icon, PATHINFO_FILENAME);
172
        $options = [
173
            "images/logos/$base_name.svg",
174
            "images/logos/$base_name.png",
175
            "images/os/$base_name.svg",
176
            "images/os/$base_name.png",
177
        ];
178
179
        foreach ($options as $file) {
180
            if (is_file(public_path()."/$file")) {
181
                return asset($file);
182
            }
183
        }
184
185
        return asset('images/os/generic.svg');
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function statusColour()
192
    {
193
        $status = $this->status;
194
        $ignore = $this->ignore;
195
        $disabled = $this->disabled;
196
        if ($disabled == 1) {
197
            return 'teal';
198
        } elseif ($ignore == 1) {
199
            return 'yellow';
200
        } elseif ($status == 0) {
201
            return 'danger';
202
        } else {
203
            return 'success';
204
        }
205
    }
206
207
    // ---- Accessors/Mutators ----
208
    public function getPortCount()
209 1
    {
210
        return is_null($this->portCountRelation) ? 0 : $this->portCountRelation->count;
0 ignored issues
show
Documentation introduced by
The property portCountRelation does not exist on object<App\Models\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...
211 1
    }
212
213
    public function getSensorCount()
214 1
    {
215
        return is_null($this->sensorCountRelation) ? 0 : $this->sensorCountRelation->count;
0 ignored issues
show
Documentation introduced by
The property sensorCountRelation does not exist on object<App\Models\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...
216 1
    }
217
218
    public function getIconAttribute($icon)
219 1
    {
220
        if (isset($icon)) {
221 1
            return asset("images/os/$icon");
222
        }
223
        return asset('images/os/generic.svg');
224 1
    }
225
    public function getIpAttribute($ip)
226 1
    {
227
        if (empty($ip)) {
228 1
            return null;
229
        }
230
        // @ suppresses warning, inet_ntop() returns false if it fails
231
        return @inet_ntop($ip) ?: null;
232 1
    }
233
234
    public function setIpAttribute($ip)
235 6
    {
236
        $this->attributes['ip'] = inet_pton($ip);
237 6
    }
238 6
239
    // ---- Query scopes ----
240
241
    public function scopeIsUp($query)
242
    {
243
        return $query->where([
244
            ['status', '=', 1],
245
            ['ignore', '=', 0],
246
            ['disabled', '=', 0]
247
        ]);
248
    }
249
250
    public function scopeIsDown($query)
251
    {
252
        return $query->where([
253
            ['status', '=', 0],
254
            ['ignore', '=', 0],
255
            ['disabled', '=', 0]
256
        ]);
257
    }
258
259
    public function scopeIsIgnored($query)
260
    {
261
        return $query->where([
262
            ['ignore', '=', 1],
263
            ['disabled', '=', 0]
264
        ]);
265
    }
266
267
    public function scopeIsNotIgnored($query)
268
    {
269
        return $query->where([
270
            ['ignore', '=', 0]
271
        ]);
272
    }
273
274
    public function scopeIsDisabled($query)
275
    {
276
        return $query->where([
277
            ['disabled', '=', 1]
278
        ]);
279
    }
280
281
    // ---- Define Relationships ----
282
283
    /**
284
     * Relationship to App\Models\Alerting\Alert
285
     *
286
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
287
     */
288
    public function alerts()
289
    {
290
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
291
    }
292
293
    /**
294
     * Relationship to App\Models\General\Eventlog
295
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
296
     */
297
    public function eventlogs()
298 1
    {
299
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
300 1
    }
301
302
    /**
303
     * Relationship to App\Models\DeviceGroup
304
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
305
     */
306
    public function groups()
307
    {
308
        return $this->belongsToMany('App\Models\DeviceGroup', 'device_group_device', 'device_id', 'device_group_id');
309
    }
310
311
    /**
312
     * Relationship to App\Models\Port
313
     * Returns a list of the ports this device has.
314
     */
315
    public function ports()
316 3
    {
317
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
318 3
    }
319
320
    public function portCountRelation()
321 1
    {
322
        return $this->hasOne('App\Models\Port', 'device_id')
323 1
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
324 1
    }
325
326
    /**
327
     * Relationship to App\Models\Processor
328
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
329
     */
330
    public function processors()
331
    {
332
        return $this->hasMany('App\Models\Processor', 'device_id');
333
    }
334
335
    /**
336
     * Relationship to App\Models\Alerting\Rule
337
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
338
     */
339
    public function rules()
340
    {
341
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
342
    }
343
344
    /**
345
     * Relationship to App\Models\Sensor
346
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
347
     */
348
    public function sensors()
349
    {
350
        return $this->hasMany('App\Models\Sensor', 'device_id');
351
    }
352
353
    public function sensorCountRelation()
354 1
    {
355
        return $this->hasOne('App\Models\Sensor', 'device_id')
356 1
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
357 1
    }
358
359
    /**
360
     * Relationship to App\Models\Service
361
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
362
     */
363
    public function services()
364
    {
365
        return $this->hasMany('App\Models\Service', 'device_id');
366
    }
367
368
    /**
369
     * Relationship to App\Models\Storage
370
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
371
     */
372
    public function storage()
373
    {
374
        return $this->hasMany('App\Models\Storage', 'device_id');
375
    }
376
377
    /**
378
     * Relationship to App\Models\General\Syslog
379
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
380
     */
381
    public function syslogs()
382 1
    {
383
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
384 1
    }
385
386
    /**
387
     * Relationship to App\Models\User
388
     * Does not include users with global permissions.
389
     *
390
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
391
     */
392
    public function users()
393
    {
394
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
395
    }
396
}
397