Completed
Push — develop ( 20a922...5bb600 )
by Enea
05:08 queued 03:13
created

HasRole::permissions()   A

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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Created on 11/02/18 by enea dhack.
7
 */
8
9
namespace Enea\Authorization\Traits;
10
11
use Enea\Authorization\Contracts\PermissionContract;
12
use Enea\Authorization\Contracts\RoleContract;
13
use Enea\Authorization\Facades\Authorizer;
14
use Enea\Authorization\Facades\Granter;
15
use Enea\Authorization\Facades\Revoker;
16
use Enea\Authorization\Support\Config;
17
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
18
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
19
20
/**
21
 * Trait HasRole.
22
 *
23
 * @package Enea\Authorization\Traits
24
 *
25
 * @property EloquentCollection permissions
26
 */
27
trait HasRole
28
{
29
    use Grantable;
30
31 1
    public static function locateByName(string $secretName): ?RoleContract
32
    {
33 1
        $role = static::query()->where('secret_name', $secretName)->first();
34 1
        return $role instanceof RoleContract ? $role : null;
35
    }
36
37 4
    public function can(string $permission): bool
38
    {
39 4
        return Authorizer::can($this, $permission);
40
    }
41
42 2
    public function cannot(string $permission): bool
43
    {
44 2
        return ! $this->can($permission);
45
    }
46
47 3
    public function grant(PermissionContract $permission): void
48
    {
49 3
        $this->syncGrant([$permission]);
50 3
    }
51
52 7
    public function syncGrant(array $permissions): void
53
    {
54 7
        Granter::permissions($this, collect($permissions));
55 7
    }
56
57 1
    public function revoke(PermissionContract $permission): void
58
    {
59 1
        $this->syncRevoke([$permission]);
60 1
    }
61
62 2
    public function syncRevoke(array $permissions): void
63
    {
64 2
        Revoker::permissions($this, collect($permissions));
65 2
    }
66
67 20
    public function permissions(): BelongsToMany
68
    {
69 20
        return $this->belongsToMany(Config::permissionModel(), Config::rolePermissionTableName(), 'role_id', 'permission_id');
70
    }
71
72 1
    public function getPermissionModels(): EloquentCollection
73
    {
74 1
        return $this->permissions;
75
    }
76
}
77