IsRole::permissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author enea dhack <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Enea\Authorization\Traits;
13
14
use Enea\Authorization\Contracts\PermissionContract;
15
use Enea\Authorization\Contracts\RoleContract;
16
use Enea\Authorization\Facades\Authorizer;
17
use Enea\Authorization\Facades\Granter;
18
use Enea\Authorization\Facades\Revoker;
19
use Enea\Authorization\Support\Config;
20
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
21
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
22
23
/**
24
 * Trait HasRole.
25
 *
26
 * @package Enea\Authorization\Traits
27
 *
28
 * @property EloquentCollection permissions
29
 */
30
trait IsRole
31
{
32
    use Grantable;
33
34 1
    public static function locateByName(string $secretName): ?RoleContract
35
    {
36 1
        $role = static::query()->where('secret_name', $secretName)->first();
37 1
        return $role instanceof RoleContract ? $role : null;
38
    }
39
40 4
    public function can(string $permission): bool
41
    {
42 4
        return Authorizer::can($this, $permission);
0 ignored issues
show
Bug introduced by
$this of type Enea\Authorization\Traits\IsRole is incompatible with the type Enea\Authorization\Contracts\PermissionsOwner expected by parameter $| of Enea\Authorization\Facades\Authorizer::can(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        return Authorizer::can(/** @scrutinizer ignore-type */ $this, $permission);
Loading history...
43
    }
44
45 2
    public function cannot(string $permission): bool
46
    {
47 2
        return ! $this->can($permission);
48
    }
49
50 6
    public function grant(PermissionContract $permission): void
51
    {
52 6
        $this->grantMultiple([$permission]);
53 6
    }
54
55 11
    public function grantMultiple(array $permissions): void
56
    {
57 11
        Granter::permissions($this, collect($permissions));
58 11
    }
59
60 1
    public function revoke(PermissionContract $permission): void
61
    {
62 1
        $this->revokeMultiple([$permission]);
63 1
    }
64
65 2
    public function revokeMultiple(array $permissions): void
66
    {
67 2
        Revoker::permissions($this, collect($permissions));
68 2
    }
69
70 15
    public function permissions(): BelongsToMany
71
    {
72 15
        return $this->belongsToMany(Config::permissionModel(), Config::rolePermissionTableName(), 'role_id', 'permission_id');
73
    }
74
75 4
    public function getPermissionModels(): EloquentCollection
76
    {
77 4
        return $this->permissions;
78
    }
79
}
80