Completed
Push — master ( 5acf75...dcb410 )
by Stephen
01:54
created

UserAndPermission::getLevel()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 1
crap 4
1
<?php
2
3
namespace z1haze\Acl\Traits;
4
5
use z1haze\Acl\Exceptions\LevelNotFoundException;
6
use z1haze\Acl\Models\Level;
7
8
trait UserAndPermission
9
{
10
    /**
11
     * USER & PERMISSION
12
     * A model belongs to a Level
13
     *
14
     * @return mixed
15
     */
16 14
    public function level()
17
    {
18 14
        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...
19
    }
20
21
    /**
22
     * USER & PERMISSION
23
     * Assign a model to a level
24
     *
25
     * @param $level
26
     * @return mixed
27
     */
28 17
    public function assignLevel($level)
29
    {
30 17
        $level = $this->getLevel($level);
31
32 16
        return $this->level()->associate($level)->save();
33
    }
34
35
    /**
36
     * USER & PERMISSION
37
     * Clear a model's level
38
     *
39
     * @return mixed
40
     */
41 2
    public function clearLevel()
42
    {
43 2
        return $this->level()->dissociate()->save();
44
    }
45
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Other Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * USER & PERMISSION
53
     * Helper function to get the level whether it is the ID
54
     * or the name of the level, or the level object itself.
55
     *
56
     * @param $level
57
     * @return mixed
58
     * @throws LevelNotFoundException
59
     */
60 17 View Code Duplication
    protected function getLevel($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...
61
    {
62 17
        if (is_string($level))
63 1
            $level = config('laravel-acl.level', Level::class)::whereName($level)->first();
64
65 17
        if (is_int($level))
66 2
            $level = config('laravel-acl.level', Level::class)::find($level);
67
68 17
        if (!$level)
69 1
            throw new LevelNotFoundException();
70
71 16
        return $level;
72
    }
73
}