Completed
Push — master ( c436ad...d0a6e2 )
by Stephen
02:15
created

UserAndPermission::cacheGetLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace z1haze\Acl\Traits;
4
5
use z1haze\Acl\Exceptions\LevelNotFoundException;
6
use Illuminate\Support\Facades\Cache;
7
use z1haze\Acl\Models\Level;
8
9
trait UserAndPermission
10
{
11
    /**
12
     * USER & PERMISSION
13
     * A model belongs to a Level
14
     *
15
     * @return mixed
16
     */
17 15
    public function level()
18
    {
19 15
        return $this->belongsTo(config('laravel-acl.level', Level::class));
0 ignored issues
show
Bug introduced by
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
    }
21
22
    /**
23
     * USER & PERMISSION
24
     * Assign a model to a level
25
     *
26
     * @param $level
27
     * @return mixed
28
     */
29 19
    public function assignLevel($level)
30
    {
31 19
        $level = $this->getALevel($level);
32
33 18
        return $this->level()->associate($level)->save();
34
    }
35
36
    /**
37
     * USER & PERMISSION
38
     * Clear a model's level
39
     *
40
     * @return mixed
41
     */
42 2
    public function clearLevel()
43
    {
44 2
        return $this->level()->dissociate()->save();
45
    }
46
47
    /**
48
     * USER & PERMISSION
49
     * First try the cache to return the collection,
50
     * then fetch it from the database.
51
     *
52
     * See @cacheGetLevel()
53
     *
54
     * @return mixed
55
     */
56 3
    public function getLevel()
57
    {
58 3
        $model = is_a($this, config('laravel-acl.level'), true) ? 'Level' : 'User';
59
60 3
        return Cache::remember(
61 3
            'laravel-acl.getLevelFor' . $model . '_' . $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62 3
            config('laravel-acl.cacheMinutes'),
63 3
            function () {
64 3
                return $this->cacheGetLevel();
65 3
            }
66
        );
67
    }
68
69
70
    /* ------------------------------------------------------------------------------------------------
71
     |  Other Functions
72
     | ------------------------------------------------------------------------------------------------
73
     */
74
    /**
75
     * USER & PERMISSION
76
     * Return a relationship of the level on a user/permission
77
     * associated to a user by direct or inheritance
78
     *
79
     * @return mixed
80
     */
81 3
    protected function cacheGetLevel()
82
    {
83 3
        return $this->level;
0 ignored issues
show
Bug introduced by
The property level does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
84
    }
85
86
    /**
87
     * USER & PERMISSION
88
     * Helper function to get the level whether it is the ID
89
     * or the name of the level, or the level object itself.
90
     *
91
     * @param $level
92
     * @return mixed
93
     * @throws LevelNotFoundException
94
     */
95 19 View Code Duplication
    protected function getALevel($level)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 19
        if (is_string($level))
98 1
            $level = config('laravel-acl.level', Level::class)::whereName($level)->first();
99
100 19
        if (is_int($level))
101 1
            $level = config('laravel-acl.level', Level::class)::find($level);
102
103 19
        if (!$level)
104 1
            throw new LevelNotFoundException();
105
106 18
        return $level;
107
    }
108
}