Completed
Push — master ( 7db559...7fc8c5 )
by Ryan
01:57
created

RoleCollection::hasPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
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
 * @package       Anomaly\UsersModule\Role
13
 */
14
class RoleCollection extends EntryCollection
15
{
16
17
    /**
18
     * Create a new RoleCollection instance.
19
     *
20
     * @param array $items
21
     */
22
    public function __construct($items = [])
23
    {
24
        /* @var RoleInterface $item */
25
        foreach ($items as $key => $item) {
26
27
            if ($item instanceof RoleInterface) {
28
                $key = $item->getSlug();
29
            }
30
31
            $this->items[$key] = $item;
32
        }
33
    }
34
35
    /**
36
     * Return all permissions.
37
     *
38
     * @return array
39
     */
40
    public function permissions()
41
    {
42
        return $this->map(
43
            function (RoleInterface $role) {
44
                return $role->getPermissions();
45
            }
46
        )->flatten()->all();
47
    }
48
49
    /**
50
     * Return if a role as access to a the permission.
51
     *
52
     * @param string $permission
53
     * @return RoleCollection
54
     */
55
    public function hasPermission($permission)
56
    {
57
        return $this->filter(
58
            function (RoleInterface $role) use ($permission) {
59
                return $role->hasPermission($permission);
60
            }
61
        );
62
    }
63
}
64