Completed
Push — master ( 7d085e...e849bf )
by Schlaefer
15:17 queued 07:36
created

Roles::get()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 7
nop 3
dl 0
loc 20
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\Permission;
14
15
use RuntimeException;
16
17
class Roles
18
{
19
    /**
20
     * Old mylittleforum string based user roles
21
     *
22
     * @var array
23
     */
24
    private $roles = [];
25
26
    /**
27
     * Shadows $this->roles for easy access with integer ID
28
     *
29
     * @var array
30
     */
31
    private $rolesAsInt = [];
32
33
    /**
34
     * Adds a new role
35
     *
36
     * @param string $role Short title like 'user', 'mod', or 'admin'
37
     * @param int $id A unique id for this role
38
     * @param array $subroles Other roles this role represents
39
     * @return self
40
     */
41
    public function add(string $role, int $id, array $subroles = []): self
42
    {
43
        $this->roles[$role] = ['id' => $id, 'type' => $role];
44
        $this->roles[$role]['subroles'] = $subroles;
45
        $this->rolesAsInt[$id] = $this->roles[$role];
46
47
        return $this;
48
    }
49
50
    /**
51
     * Get all roles for role
52
     *
53
     * @param string $role Role
54
     * @param bool $includeAnon Include 'anon' user in roles-list
55
     * @param bool $includeOwn If false a list all other roles a user has
56
     * @return array All roles a role has
57
     */
58
    public function get(string $role, bool $includeAnon = true, bool $includeOwn = true): array
59
    {
60
        if (!isset($this->roles[$role])) {
61
            return [];
62
        }
63
64
        $roles = [];
65
66
        if ($includeOwn) {
67
            $roles[] = $role;
68
        }
69
70
        foreach ($this->roles[$role]['subroles'] as $role) {
71
            if ($role === 'anon' && !$includeAnon) {
72
                continue;
73
            }
74
            $roles[] = $role;
75
        }
76
77
        return $roles;
78
    }
79
80
    /**
81
     * Get all configured  roles
82
     *
83
     * @param bool $includeAnon Include anon user
84
     * @return array
85
     */
86
    public function getAvailable(bool $includeAnon = false): array
87
    {
88
        $roles = $this->rolesAsInt;
89
        if (!$includeAnon) {
90
            unset($roles[0]);
91
        }
92
93
        return $roles;
94
    }
95
96
    /**
97
     * Get role id for type
98
     *
99
     * @param string $type Type
100
     * @return int
101
     */
102
    public function typeToId(string $type): int
103
    {
104
        if (isset($this->roles[$type])) {
105
            return $this->roles[$type]['id'];
106
        }
107
108
        throw new RuntimeException(sprintf('Role "%s" not found.', $type));
109
    }
110
}
111