Completed
Push — master ( ecdd68...e69772 )
by Arnold
02:25
created

ByLevel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLevels() 0 4 1
A getLevel() 0 7 2
1
<?php
2
3
namespace Jasny\Authz;
4
5
/**
6
 * Authorize by access level.
7
 * 
8
 * <code>
9
 *   class Auth extends Jasny\Auth
10
 *   {
11
 *     use Jasny\Authz\ByLevel;
12
 *
13
 *     protected $levels = [
14
 *       'user' => 1,
15
 *       'moderator' => 100,
16
 *       'admin' => 1000
17
 *     ];
18
 *   }
19
 * </code>
20
 */
21
trait ByLevel
22
{
23
    /**
24
     * Authorization levels.
25
     * 
26
     * NOTE: Level names should not contain only digits.
27
     * 
28
     * @var array
29
     */
30
    protected $levels = [
31
        'user' => 1
32
    ];
33
    
34
    
35
    /**
36
     * Get all access levels.
37
     *  
38
     * @return array
39
     */
40
    public function getLevels()
41
    {
42
        return $this->levels;
43
    }
44
    
45
    /**
46
     * Get access level by name.
47
     * 
48
     * @param string $name  Level name
49
     * @return int
50
     * @throws DomainException for unknown level names
51
     */
52
    public function getLevel($name)
53
    {
54
        $levels = $this->getLevels();
55
        if (!isset($levels[$name])) throw new \DomainException("Authorization level '$name' isn't defined.");
56
        
57
        return $levels[$name];
58
    }
59
}
60