WithDeniablePermission::isSuccessful()   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 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\Grantable;
16
use Enea\Authorization\Contracts\PermissionContract;
17
use Enea\Authorization\Contracts\PermissionsOwner;
18
use Enea\Authorization\Facades\Helper;
19
use Illuminate\Support\Collection;
20
21
trait WithDeniablePermission
22
{
23
    abstract protected function isModifiable(PermissionContract $permission): bool;
24
25
    abstract protected function throwException(Grantable $grantable): void;
26
27 38
    private function getPermissions(PermissionsOwner $owner, Collection $permissions): Collection
28
    {
29 38
        return $owner->permissions()->whereKey($permissions->pluck('id'))->limit(count($permissions))->get();
30
    }
31
32 38
    private function except(Collection $granted, Collection $permissions): Collection
33
    {
34 38
        return Helper::except($permissions, $granted->pluck('secret_name')->toArray());
35
    }
36
37 38
    private function denialStatus(PermissionsOwner $owner, bool $denied): Closure
38
    {
39 38
        return function (PermissionContract $permission) use ($owner, $denied): void {
40 5
            if (! $this->isModifiable($permission)) {
41
                return;
42
            }
43
44 5
            $result = $owner->permissions()->updateExistingPivot($permission->getIdentificationKey(), [
45 5
                'denied' => $denied
46
            ]);
47
48 5
            if (! $this->isSuccessful($result)) {
49
                $this->throwException($permission);
50
            }
51 38
        };
52
    }
53
54 5
    private function isSuccessful(int $results): bool
55
    {
56 5
        return $results > 0;
57
    }
58
}
59