helpers.php ➔ aclGetALevel()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
use z1haze\Acl\Exceptions\LevelNotFoundException;
4
use z1haze\Acl\Exceptions\UserNotFoundException;
5
use z1haze\Acl\Models\Level;
6
7
if (!function_exists('aclGetALevel')) {
8
    /**
9
     * Helper function to get the level whether it is the ID
10
     * or the name of the level, or the level object itself.
11
     *
12
     * @param  mixed $level
13
     * @return Level
14
     * @throws LevelNotFoundException
15
     */
16
    function aclGetALevel($level)
17
    {
18 21
        if (is_string($level)) {
19 1
            $level = config('laravel-acl.level', Level::class)::whereName($level)->first();
20
        }
21
22 21
        if (is_int($level)) {
23 2
            $level = config('laravel-acl.level', Level::class)::find($level);
24
        }
25
26 21
        if (!$level) {
27 1
            throw new LevelNotFoundException();
28
        }
29
30 20
        return $level;
31
    }
32
}
33
34
if (!function_exists('aclGetUser')) {
35
    /**
36
     * PERMISSION
37
     * Helper function to get the user whether it is the user ID
38
     * or the user object itself.
39
     *
40
     * @param $user
41
     * @return User
42
     * @throws UserNotFoundException
43
     */
44
    function aclGetUser($user)
45
    {
46 6
        if (is_int($user)) {
47 2
            $user = config('laravel-acl.user')::find($user);
48
        }
49
50 6
        if (!$user) {
51 1
            throw new UserNotFoundException;
52
        }
53
54 5
        return $user;
55
    }
56
}