Test Setup Failed
Pull Request — develop (#200)
by Tony
07:41
created

Device::portCountRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace App\Models;
4
5
use App\Events\DeviceCreated;
6
use App\Events\DeviceDeleted;
7
use App\Events\DeviceUpdated;
8
use Illuminate\Database\Eloquent\Model;
9
10
/**
11
 * App\Models\Device
12
 *
13
 * @property integer $device_id
14
 * @property string $hostname
15
 * @property string $sysName
16
 * @property mixed $ip
17
 * @property string $community
18
 * @property string $authlevel
19
 * @property string $authname
20
 * @property string $authpass
21
 * @property string $authalgo
22
 * @property string $cryptopass
23
 * @property string $cryptoalgo
24
 * @property string $snmpver
25
 * @property integer $port
26
 * @property string $transport
27
 * @property integer $timeout
28
 * @property integer $retries
29
 * @property string $bgpLocalAs
30
 * @property string $sysObjectID
31
 * @property string $sysDescr
32
 * @property string $sysContact
33
 * @property string $version
34
 * @property string $hardware
35
 * @property string $features
36
 * @property string $location
37
 * @property string $os
38
 * @property boolean $status
39
 * @property string $status_reason
40
 * @property boolean $ignore
41
 * @property boolean $disabled
42
 * @property integer $uptime
43
 * @property integer $agent_uptime
44
 * @property string $last_polled
45
 * @property string $last_poll_attempted
46
 * @property float $last_polled_timetaken
47
 * @property float $last_discovered_timetaken
48
 * @property string $last_discovered
49
 * @property string $last_ping
50
 * @property float $last_ping_timetaken
51
 * @property string $purpose
52
 * @property string $type
53
 * @property string $serial
54
 * @property string $icon
55
 * @property integer $poller_group
56
 * @property boolean $override_sysLocation
57
 * @property string $notes
58
 * @property integer $port_association_mode
59
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
60
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports
61
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Syslog[] $syslogs
62
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlogs
63
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Alerting\Alert[] $alerts
64
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Alerting\Rule[] $rules
65
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\General\Eventlog[] $eventlog
66
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Sensor[] $sensors
67
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDeviceId($value)
68
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHostname($value)
69
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysName($value)
70
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIp($value)
71
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCommunity($value)
72
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthlevel($value)
73
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthname($value)
74
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthpass($value)
75
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAuthalgo($value)
76
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptopass($value)
77
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereCryptoalgo($value)
78
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSnmpver($value)
79
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePort($value)
80
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTransport($value)
81
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereTimeout($value)
82
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereRetries($value)
83
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereBgpLocalAs($value)
84
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysObjectID($value)
85
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysDescr($value)
86
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSysContact($value)
87
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereVersion($value)
88
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereHardware($value)
89
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereFeatures($value)
90
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLocation($value)
91
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOs($value)
92
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatus($value)
93
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereStatusReason($value)
94
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIgnore($value)
95
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereDisabled($value)
96
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereUptime($value)
97
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereAgentUptime($value)
98
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolled($value)
99
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPollAttempted($value)
100
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPolledTimetaken($value)
101
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscoveredTimetaken($value)
102
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastDiscovered($value)
103
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPing($value)
104
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereLastPingTimetaken($value)
105
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePurpose($value)
106
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereType($value)
107
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereSerial($value)
108
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereIcon($value)
109
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePollerGroup($value)
110
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereOverrideSysLocation($value)
111
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device whereNotes($value)
112
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device wherePortAssociationMode($value)
113
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isUp()
114
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isDown()
115
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isIgnored()
116
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isNotIgnored()
117
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Device isDisabled()
118
 * @mixin \Eloquent
119
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DeviceGroup[] $groups
120
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Processor[] $processors
121
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Service[] $services
122
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Storage[] $storage
123
 */
124
class Device extends Model
125
{
126
    /**
127
     * Indicates if the model should be timestamped.
128
     *
129
     * @var bool
130
     */
131
    public $timestamps = false;
132
    /**
133
     * The table associated with the model.
134
     *
135
     * @var string
136
     */
137
    protected $table = 'devices';
138
    /**
139
     * The primary key column name.
140
     *
141
     * @var string
142
     */
143
    protected $primaryKey = 'device_id';
144
145
    /**
146
     * The attributes that are mass assignable.
147
     *
148
     * @var array
149
     */
150
    protected $fillable = ['hostname', 'ip', 'status', 'status_reason'];
151
152
    protected $events = [
153 10
        'created'  => DeviceCreated::class,
154
        'deleting' => DeviceDeleted::class,
155 10
        'updated'  => DeviceUpdated::class,
156
    ];
157 10
158
    /**
159 1
     * Initialize this class
160 1
     */
161 1
    public static function boot()
162 10
    {
163 10
        parent::boot();
164
165
        static::deleting(function (Device $device) {
166
            // delete related data
167
            $device->ports()->delete();
168
            $device->syslogs()->delete();
169
            $device->eventlogs()->delete();
170
        });
171
    }
172
173
    // ---- Helper Functions ----
174
175
    /**
176
     * @return string
177
     */
178
    public function logo()
179
    {
180
        $base_name = pathinfo($this->icon, PATHINFO_FILENAME);
181
        $options = [
182
            "images/logos/$base_name.svg",
183
            "images/logos/$base_name.png",
184
            "images/os/$base_name.svg",
185
            "images/os/$base_name.png",
186
        ];
187
188
        foreach ($options as $file) {
189
            if (is_file(public_path()."/$file")) {
190
                return asset($file);
191
            }
192
        }
193
194
        return asset('images/os/generic.svg');
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function statusColour()
201
    {
202
        $status = $this->status;
203
        $ignore = $this->ignore;
204
        $disabled = $this->disabled;
205
        if ($disabled == 1) {
206
            return 'teal';
207
        } elseif ($ignore == 1) {
208
            return 'yellow';
209 1
        } elseif ($status == 0) {
210
            return 'danger';
211 1
        } else {
212
            return 'success';
213
        }
214 1
    }
215
216 1
    // ---- Accessors/Mutators ----
217
    public function getPortCount()
218
    {
219 1
        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...
220
    }
221 1
222
    public function getSensorCount()
223
    {
224 1
        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...
225
    }
226 1
227
    public function getIconAttribute($icon)
228 1
    {
229
        if (isset($icon)) {
230
            return asset("images/os/$icon");
231
        }
232 1
        return asset('images/os/generic.svg');
233
    }
234
235 6
    public function getIpAttribute($ip)
236
    {
237 6
        if (empty($ip)) {
238 6
            return null;
239
        }
240
        // @ suppresses warning, inet_ntop() returns false if it fails
241
        return @inet_ntop($ip) ?: null;
242
    }
243
244
    public function setIpAttribute($ip)
245
    {
246
        $this->attributes['ip'] = inet_pton($ip);
247
    }
248
249
    // ---- Query scopes ----
250
251
    public function scopeIsUp($query)
252
    {
253
        return $query->where([
254
            ['status', '=', 1],
255
            ['ignore', '=', 0],
256
            ['disabled', '=', 0]
257
        ]);
258
    }
259
260
    public function scopeIsDown($query)
261
    {
262
        return $query->where([
263
            ['status', '=', 0],
264
            ['ignore', '=', 0],
265
            ['disabled', '=', 0]
266
        ]);
267
    }
268
269
    public function scopeIsIgnored($query)
270
    {
271
        return $query->where([
272
            ['ignore', '=', 1],
273
            ['disabled', '=', 0]
274
        ]);
275
    }
276
277
    public function scopeIsNotIgnored($query)
278
    {
279
        return $query->where([
280
            ['ignore', '=', 0]
281
        ]);
282
    }
283
284
    public function scopeIsDisabled($query)
285
    {
286
        return $query->where([
287
            ['disabled', '=', 1]
288
        ]);
289
    }
290
291
    // ---- Define Relationships ----
292
293
    /**
294
     * Relationship to App\Models\Alerting\Alert
295
     *
296
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
297
     */
298 1
    public function alerts()
299
    {
300 1
        return $this->hasMany('App\Models\Alerting\Alert', 'device_id');
301
    }
302
303
    /**
304
     * Relationship to App\Models\General\Eventlog
305
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
306
     */
307
    public function eventlogs()
308
    {
309
        return $this->hasMany('App\Models\General\Eventlog', 'host', 'device_id');
310
    }
311
312
    /**
313
     * Relationship to App\Models\DeviceGroup
314
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
315
     */
316 3
    public function groups()
317
    {
318 3
        return $this->belongsToMany('App\Models\DeviceGroup', 'device_group_device', 'device_id', 'device_group_id');
319
    }
320
321 1
    /**
322
     * Relationship to App\Models\Port
323 1
     * Returns a list of the ports this device has.
324 1
     */
325
    public function ports()
326
    {
327
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
328
    }
329
330
    public function portCountRelation()
331
    {
332
        return $this->hasOne('App\Models\Port', 'device_id')
333
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
334
    }
335
336
    /**
337
     * Relationship to App\Models\Processor
338
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
339
     */
340
    public function processors()
341
    {
342
        return $this->hasMany('App\Models\Processor', 'device_id');
343
    }
344
345
    /**
346
     * Relationship to App\Models\Alerting\Rule
347
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
348
     */
349
    public function rules()
350
    {
351
        return $this->hasMany('App\Models\Alerting\Rule', 'device_id');
352
    }
353
354 1
    /**
355
     * Relationship to App\Models\Sensor
356 1
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
357 1
     */
358
    public function sensors()
359
    {
360
        return $this->hasMany('App\Models\Sensor', 'device_id');
361
    }
362
363
    public function sensorCountRelation()
364
    {
365
        return $this->hasOne('App\Models\Sensor', 'device_id')
366
            ->selectRaw('device_id, count(*) as count')->groupBy('device_id');
367
    }
368
369
    /**
370
     * Relationship to App\Models\Service
371
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
372
     */
373
    public function services()
374
    {
375
        return $this->hasMany('App\Models\Service', 'device_id');
376
    }
377
378
    /**
379
     * Relationship to App\Models\Storage
380
     * @return \Illuminate\Database\Eloquent\Relations\hasMany
381
     */
382 1
    public function storage()
383
    {
384 1
        return $this->hasMany('App\Models\Storage', 'device_id');
385
    }
386
387
    /**
388
     * Relationship to App\Models\General\Syslog
389
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
390
     */
391
    public function syslogs()
392
    {
393
        return $this->hasMany('App\Models\General\Syslog', 'device_id', 'device_id');
394
    }
395
396
    /**
397
     * Relationship to App\Models\User
398
     * Does not include users with global permissions.
399
     *
400
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
401
     */
402
    public function users()
403
    {
404
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
405
    }
406
}
407