RoleCollection   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 50
c 2
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A permissions() 0 8 1
A hasPermission() 0 8 1
1
<?php namespace Anomaly\UsersModule\Role;
2
3
use Anomaly\Streams\Platform\Entry\EntryCollection;
4
use Anomaly\UsersModule\Role\Contract\RoleInterface;
5
6
/**
7
 * Class RoleCollection
8
 *
9
 * @link          http://pyrocms.com/
10
 * @author        PyroCMS, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 */
13
class RoleCollection extends EntryCollection
14
{
15
16
    /**
17
     * Create a new RoleCollection instance.
18
     *
19
     * @param array $items
20
     */
21
    public function __construct($items = [])
22
    {
23
        /* @var RoleInterface $item */
24
        foreach ($items as $key => $item) {
25
26
            if ($item instanceof RoleInterface) {
27
                $key = $item->getSlug();
28
            }
29
30
            $this->items[$key] = $item;
31
        }
32
    }
33
34
    /**
35
     * Return all permissions.
36
     *
37
     * @return array
38
     */
39
    public function permissions()
40
    {
41
        return $this->map(
42
            function (RoleInterface $role) {
43
                return $role->getPermissions();
44
            }
45
        )->flatten()->all();
46
    }
47
48
    /**
49
     * Return if a role as access to a the permission.
50
     *
51
     * @param  string         $permission
52
     * @return RoleCollection
53
     */
54
    public function hasPermission($permission)
55
    {
56
        return $this->filter(
57
            function (RoleInterface $role) use ($permission) {
58
                return $role->hasPermission($permission);
59
            }
60
        );
61
    }
62
}
63