Completed
Push — notifications ( 1f762c...c8c5e4 )
by Tony
02:44
created

Device   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 2
cbo 1
dl 0
loc 81
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getIpAttribute() 0 6 2
A setIpAttribute() 0 4 1
A boot() 0 11 1
A users() 0 3 1
A ports() 0 3 1
A syslogs() 0 3 1
A eventlogs() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Device extends Model
8
{
9
    /**
10
     * The table associated with the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'devices';
15
16
    /**
17
     * The primary key column name.
18
     *
19
     * @var string
20
     */
21
    protected $primaryKey = 'device_id';
22
23
    /**
24
     * Indicates if the model should be timestamped.
25
     *
26
     * @var bool
27
     */
28
    public $timestamps = false;
29
30
31
    // ---- Accessors/Mutators ----
32
33
    public function getIpAttribute($ip)
34
    {
35
        if (!empty($ip)) {
36
            return inet_ntop($ip);
37
        }
38
    }
39
40
    public function setIpAttribute($ip)
41
    {
42
        $this->attributes['ip'] = inet_pton($ip);
43
    }
44
45
    public static function boot()
46
    {
47
        parent::boot();
48
49
        static::deleting(function(Device $device) {
50
            // delete related data
51
            $device->ports()->delete();
52
            $device->syslogs()->delete();
53
            $device->eventlogs()->delete();
54
        });
55
    }
56
57
58
    // ---- Define Reletionships ----
59
60
    /**
61
     * Returns a list of users that can access this device.
62
     */
63
    public function users() {
64
        return $this->belongsToMany('App\Models\User', 'devices_perms', 'device_id', 'user_id');
65
    }
66
67
    /**
68
     * Returns a list of the ports this device has.
69
     */
70
    public function ports() {
71
        return $this->hasMany('App\Models\Port', 'device_id', 'device_id');
72
    }
73
74
    /**
75
     * Returns a list of the Syslog entries this device has.
76
     */
77
    public function syslogs() {
78
        return $this->hasMany('App\Models\Syslog', 'device_id', 'device_id');
79
    }
80
81
    /**
82
     * Returns a list of the Eventlog entries this device has.
83
     */
84
    public function eventlogs() {
85
        return $this->hasMany('App\Models\Eventlog', 'device_id', 'device_id');
86
    }
87
}
88