Completed
Push — develop ( 49f866...6f4c5d )
by Neil
03:50
created

Port   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 5
c 4
b 2
f 1
lcom 0
cbo 1
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getifSpeedHumanAttribute() 0 3 1
A formatBps() 0 11 2
A device() 0 3 1
A users() 0 3 1
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Port extends Model
8
{
9
    /**
10
     * The table associated with the model.
11
     *
12
     * @var string
13
     */
14
    protected $table = 'ports';
15
16
    /**
17
     * The primary key column name.
18
     *
19
     * @var string
20
     */
21
    protected $primaryKey = 'port_id';
22
23
    /**
24
     * Indicates if the model should be timestamped.
25
     *
26
     * @var bool
27
     */
28
    public $timestamps = false;
29
30
    // TODO: transform DB to be snake case?
31
//    public static $snakeAttributes = false;
32
33
    // ---- Accessors/Mutators ----
34
35
    //TODO this is the wrong place for this as it messes up sorting
36
//    public function getifSpeedAttribute($ifSpeed) {
37
//        return $this->getifSpeedHumanAttribute($ifSpeed);
38
//    }
39
40
    public function getifSpeedHumanAttribute($ifSpeed) {
41
        return $this->formatBps($ifSpeed);
42
    }
43
44
45
    // TODO: move to a common file
46
    // base = 1024 for bits, 1000 for mibibits
47
    function formatBps($bits, $precision = 2, $base = 1000) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
        $units = array('bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps');
49
50
        $bits = max($bits, 0);
51
        $pow = floor(($bits ? log($bits) : 0) / log($base));
52
        $pow = min($pow, count($units) - 1);
53
54
        $bits /= pow($base, $pow);
55
56
        return round($bits, $precision).' '.$units[$pow];
57
    }
58
59
60
    // ---- Define Reletionships ----
61
62
    /**
63
     * Get the device this port belongs to.
64
     *
65
     */
66
    public function device() {
67
        return $this->belongsTo('App\Device', 'device_id', 'device_id');
68
    }
69
70
    /**
71
     * Returns a list of users that can access this port.
72
     */
73
    public function users() {
74
        return $this->belongsToMany('App\User', 'ports_perms', 'port_id', 'user_id');
75
    }
76
77
}
78