| Total Complexity | 16 |
| Total Lines | 86 |
| Duplicated Lines | 0 % |
| Coverage | 44.74% |
| Changes | 0 | ||
| 1 | <?php |
||
| 28 | trait Authorizable |
||
| 29 | { |
||
| 30 | use Model; |
||
| 31 | |||
| 32 | public function getIdentificationKey(): string |
||
| 33 | { |
||
| 34 | return $this->getKey(); |
||
| 35 | } |
||
| 36 | |||
| 37 | 8 | public function grant(Grantable $grantable): void |
|
| 38 | { |
||
| 39 | 8 | $this->syncGrant([$grantable]); |
|
| 40 | 8 | } |
|
| 41 | |||
| 42 | 22 | public function syncGrant(array $grantables): void |
|
| 46 | 22 | } |
|
| 47 | |||
| 48 | public function revoke(Grantable $grantable): void |
||
| 49 | { |
||
| 50 | $this->syncRevoke([$grantable]); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function syncRevoke(array $grantables): void |
||
| 54 | { |
||
| 55 | Revoker::permissions($this, $this->filterPermissions($grantables)); |
||
|
1 ignored issue
–
show
|
|||
| 56 | Revoker::roles($this, $this->filterRoles($grantables)); |
||
|
1 ignored issue
–
show
|
|||
| 57 | } |
||
| 58 | |||
| 59 | public function can(string $permission): bool |
||
| 60 | { |
||
| 61 | return Authorizer::can($this, $permission); |
||
|
1 ignored issue
–
show
|
|||
| 62 | } |
||
| 63 | |||
| 64 | public function cannot(string $permission): bool |
||
| 65 | { |
||
| 66 | return ! $this->can($permission); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function isMemberOf(string $role): bool |
||
| 70 | { |
||
| 71 | return Authorizer::is($this, $role); |
||
|
1 ignored issue
–
show
|
|||
| 72 | } |
||
| 73 | |||
| 74 | public function isntMemberOf(string $role): bool |
||
| 75 | { |
||
| 76 | return ! $this->isMemberOf($role); |
||
| 77 | } |
||
| 78 | |||
| 79 | 31 | public function permissions(): BelongsToMany |
|
| 80 | { |
||
| 81 | 31 | return $this->morphToMany(Config::permissionModel(), 'authorizable', Config::userPermissionTableName()); |
|
| 82 | } |
||
| 83 | |||
| 84 | 40 | public function roles(): BelongsToMany |
|
| 85 | { |
||
| 86 | 40 | return $this->morphToMany(Config::roleModel(), 'authorizable', Config::userRoleTableName()); |
|
| 87 | } |
||
| 88 | |||
| 89 | public function getPermissionModels(): EloquentCollection |
||
| 90 | { |
||
| 91 | return $this->permissions; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getRoleModels(): EloquentCollection |
||
| 95 | { |
||
| 96 | return $this->roles; |
||
| 97 | } |
||
| 98 | |||
| 99 | 22 | private function filterPermissions(array $grantables): Collection |
|
| 100 | { |
||
| 101 | 22 | return $this->filterOnly(PermissionContract::class)($grantables); |
|
| 102 | } |
||
| 103 | |||
| 104 | 22 | private function filterRoles(array $grantables): Collection |
|
| 107 | } |
||
| 108 | |||
| 109 | private function filterOnly(string $abstract): Closure |
||
| 110 | { |
||
| 111 | return function (array $grantables) use ($abstract): Collection { |
||
| 112 | 22 | return collect($grantables)->filter(function (Grantable $grantable) use ($abstract) { |
|
| 113 | 22 | return $grantable instanceof $abstract; |
|
| 114 | 22 | }); |
|
| 115 | 22 | }; |
|
| 116 | } |
||
| 117 | } |
||
| 118 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: