Completed
Push — develop ( 969b3c...da2fc5 )
by Enea
01:58
created

Authorizable::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 13/02/18 by enea dhack.
7
 */
8
9
namespace Enea\Authorization\Traits;
10
11
use Closure;
12
use Enea\Authorization\Contracts\Grantable;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Enea\Authorization\Traits\Grantable. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Enea\Authorization\Contracts\PermissionContract;
14
use Enea\Authorization\Contracts\RoleContract;
15
use Enea\Authorization\Facades\Authorizer;
16
use Enea\Authorization\Facades\Granter;
17
use Enea\Authorization\Facades\Revoker;
18
use Enea\Authorization\Support\Config;
19
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
20
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
21
use Illuminate\Support\Collection;
22
23
/**
24
 * Trait Authorizable.
25
 *
26
 * @package Enea\Authorization\Traits
27
 *
28
 * @property EloquentCollection permissions
29
 * @property EloquentCollection roles
30
 */
31
trait Authorizable
32
{
33
    use Model;
34
35 1
    public function getIdentificationKey(): string
36
    {
37 1
        return (string) $this->getKey();
38
    }
39
40 9
    public function grant(Grantable $grantable): void
41
    {
42 9
        $this->syncGrant([$grantable]);
43 9
    }
44
45 22
    public function syncGrant(array $grantables): void
46
    {
47 22
        Granter::permissions($this, $this->filterPermissions($grantables));
48 22
        Granter::roles($this, $this->filterRoles($grantables));
49 22
    }
50
51 2
    public function revoke(Grantable $grantable): void
52
    {
53 2
        $this->syncRevoke([$grantable]);
54 2
    }
55
56 4
    public function syncRevoke(array $grantables): void
57
    {
58 4
        Revoker::permissions($this, $this->filterPermissions($grantables));
59 4
        Revoker::roles($this, $this->filterRoles($grantables));
60 4
    }
61
62 4
    public function can(string $permission): bool
63
    {
64 4
        return Authorizer::can($this, $permission);
65
    }
66
67 2
    public function cannot(string $permission): bool
68
    {
69 2
        return ! $this->can($permission);
70
    }
71
72 4
    public function isMemberOf(string $role): bool
73
    {
74 4
        return Authorizer::is($this, $role);
75
    }
76
77 2
    public function isntMemberOf(string $role): bool
78
    {
79 2
        return ! $this->isMemberOf($role);
80
    }
81
82 28
    public function permissions(): BelongsToMany
83
    {
84 28
        return $this->morphToMany(Config::permissionModel(), 'authorizable', Config::userPermissionTableName());
85
    }
86
87 30
    public function roles(): BelongsToMany
88
    {
89 30
        return $this->morphToMany(Config::roleModel(), 'authorizable', Config::userRoleTableName());
90
    }
91
92 1
    public function getPermissionModels(): EloquentCollection
93
    {
94 1
        return $this->permissions;
95
    }
96
97 1
    public function getRoleModels(): EloquentCollection
98
    {
99 1
        return $this->roles;
100
    }
101
102 22
    private function filterPermissions(array $grantables): Collection
103
    {
104 22
        return $this->filterOnly(PermissionContract::class)($grantables);
105
    }
106
107 22
    private function filterRoles(array $grantables): Collection
108
    {
109 22
        return $this->filterOnly(RoleContract::class)($grantables);
110
    }
111
112
    private function filterOnly(string $abstract): Closure
113
    {
114
        return function (array $grantables) use ($abstract): Collection {
115 22
            return collect($grantables)->filter(function (Grantable $grantable) use ($abstract) {
116 22
                return $grantable instanceof $abstract;
117 22
            });
118 22
        };
119
    }
120
}
121