WithRoles::getAllRolesOnDomain()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 20
rs 9.6111
cc 5
nc 8
nop 1
1
<?php
2
3
namespace Uccello\Core\Support\Traits;
4
5
use Uccello\Core\Models\Domain;
6
7
trait WithRoles
8
{
9
    /**
10
     * Returns all roles available in a domain
11
     *
12
     * @param \Uccello\Core\Models\Domain $domain
13
     * @return array
14
     */
15
    protected function getAllRolesOnDomain(Domain $domain)
16
    {
17
        // Display ancestors roles only if it is allowed
18
        if (config('uccello.roles.display_ancestors_roles')) {
19
            $treeDomainsIds = $domain->findAncestors()->pluck('id');
20
        } else {
21
            $treeDomainsIds = collect([ $domain->id ]);
22
        }
23
24
        // Get all roles
25
        $roles = [ ];
26
        foreach ($treeDomainsIds as $treeDomainId) {
27
            $_domain = Domain::find($treeDomainId);
28
            foreach ($_domain->roles as $role) {
29
                $roleName = $_domain->id === $domain->id ? $role->name : $_domain->name . ' > ' . $role->name;
30
                $roles[ $role->id ] = $roleName;
31
            }
32
        }
33
34
        return $roles;
35
    }
36
}