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

HasRole   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A locateByName() 0 4 2
A revoke() 0 3 1
A permissions() 0 3 1
A syncGrant() 0 3 1
A cannot() 0 3 1
A can() 0 3 1
A grant() 0 3 1
A getPermissionModels() 0 3 1
A syncRevoke() 0 3 1
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