Completed
Push — develop ( 2606e4...9302c9 )
by Neil
04:35
created

User   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasGlobalRead() 0 4 2
A devices() 0 3 1
A ports() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Foundation\Auth\User as Authenticatable;
6
7
class User extends Authenticatable
8
{
9
    /**
10
     * The attributes that are mass assignable.
11
     *
12
     * @var array
13
     */
14
    protected $fillable = [
15
        'realname', 'username', 'password', 'email', 'level',
16
    ];
17
    protected $primaryKey = 'user_id';
18
19
    /**
20
     * The attributes excluded from the model's JSON form.
21
     *
22
     * @var array
23
     */
24
    protected $hidden = [
25
        'password', 'remember_token',
26
    ];
27
28
29
    // ---- Define Convience Functions ----
30
31
    /**
32
     * Test if the User is an admin or demo.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Builder
35
     */
36
    public function isAdmin()
37
    {
38
        return $this->level >= 10;
0 ignored issues
show
Documentation introduced by
The property level does not exist on object<App\Models\User>. 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...
39
    }
40
41
    /**
42
     * Test if this user has global read access
43
     * these users have a level of 5, 10 or 11 (demo).
44
     *
45
     * @return \Illuminate\Database\Eloquent\Builder
46
     */
47
    public function hasGlobalRead()
48
    {
49
        return $this->isAdmin() || $this->level == 5;
0 ignored issues
show
Documentation introduced by
The property level does not exist on object<App\Models\User>. 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...
50
    }
51
52
53
    // ---- Define Reletionships ----
54
55
    /**
56
     * Returns a list of devices this user has access to
57
     */
58
    public function devices() {
59
        return $this->belongsToMany('App\Models\Device', 'devices_perms', 'user_id', 'device_id');
60
    }
61
62
    /**
63
     * Returns a list of ports this user has access to
64
     */
65
    public function ports() {
66
        return $this->belongsToMany('App\Models\Port', 'ports_perms', 'user_id', 'port_id');
67
    }
68
}
69