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