RoleCollection::hasPermission()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 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