Completed
Push — master ( bfc4a1...019307 )
by
unknown
12s queued 11s
created

PermissionResolver::getScopeRestrictions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 15
rs 9.9332
1
<?php
2
3
namespace Mrluke\Privileges;
4
5
/**
6
 * Class PermissionResolver
7
 * @package Mrluke\Privileges
8
 *
9
 * @author  Hubert Smusz <[email protected]>
10
 * @version 1.0.0
11
 */
12
class PermissionResolver
13
{
14
15
    /**
16
     * @param          $subject
17
     * @param string   $scope
18
     * @param int|null $level
19
     *
20
     * @return array
21
     */
22
    public static function getScopeRestrictions($subject, string $scope, int $level = null): array
23
    {
24
        $scopes = $subject->roles->flatMap(function ($role) use ($scope, $level) {
25
            if ($level) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $level of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
26
                $permissions = $role->permissions->where('level', $level);
27
            } else {
28
                $permissions = $role->permissions;
29
            }
30
31
            return preg_grep("/^${scope}:/", array_column($permissions->toArray(), 'scope'));
32
        })->toArray();
33
34
        return array_map(function ($scope) {
35
            return explode(':', $scope)[1];
36
        }, $scopes);
37
    }
38
}
39