Granter::dispatchGrantedEvent()   A
last analyzed

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 2
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
 * @author enea dhack <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Enea\Authorization\Operators;
13
14
use Closure;
15
use Enea\Authorization\Contracts\{
16
    Grantable, Owner, PermissionContract, PermissionsOwner, RolesOwner
17
};
18
use Enea\Authorization\Events\Granted;
19
use Enea\Authorization\Exceptions\AuthorizationNotGrantedException;
20
use Exception;
21
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
22
use Illuminate\Support\Collection;
23
24
class Granter extends Operator
25
{
26
    use WithDeniablePermission;
27
28 36
    public function permissions(PermissionsOwner $owner, Collection $permissions): void
29
    {
30 36
        $granted = $this->getPermissions($owner, $permissions);
31 36
        $granted->each($this->denialStatus($owner, false));
32 36
        $this->grantTo($owner->permissions())($this->except($granted, $permissions));
33 35
        $this->dispatchGrantedEvent($owner, $permissions);
34 35
    }
35
36 26
    public function roles(RolesOwner $owner, Collection $roles): void
37
    {
38 26
        $this->grantTo($owner->roles())($roles);
39 25
        $this->dispatchGrantedEvent($owner, $roles);
40 25
    }
41
42 56
    private function grantTo(BelongsToMany $repository): Closure
43
    {
44 56
        return function (Collection $grantableCollection) use ($repository): void {
45 56
            $grantableCollection->each($this->addTo($repository));
46 56
        };
47
    }
48
49 56
    protected function addTo(BelongsToMany $authorizations): Closure
50
    {
51 56
        return function (Grantable $grantable) use ($authorizations): void {
52 56
            $this->saveIn($grantable, $authorizations);
53 56
        };
54
    }
55
56 56
    private function saveIn(Grantable $grantable, BelongsToMany $authorizations): void
57
    {
58
        try {
59 56
            $authorizations->save($grantable);
60 2
        } catch (Exception $exception) {
61 2
            throw new AuthorizationNotGrantedException($grantable, $exception);
62
        }
63 54
    }
64
65 54
    private function dispatchGrantedEvent(Owner $owner, Collection $grantableCollection): void
66
    {
67 54
        $this->dispatchEvent(new Granted($owner, $grantableCollection));
68 54
    }
69
70 4
    protected function isModifiable(PermissionContract $permission): bool
71
    {
72 4
        return $permission->pivot->isDenied();
73
    }
74
75
    protected function throwException(Grantable $grantable): void
76
    {
77
        throw new AuthorizationNotGrantedException($grantable);
78
    }
79
}
80