Passed
Branch feature/deny (0fd688)
by Enea
02:29
created

Denier::__construct()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 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 Enea\Authorization\Contracts\DeniableOwner;
15
use Enea\Authorization\Contracts\PermissionContract;
16
use Enea\Authorization\Events\Denied;
17
use Enea\Authorization\Exceptions\AuthorizationNotDeniedException;
18
use Exception;
19
use Illuminate\Contracts\Events\Dispatcher;
20
use Illuminate\Support\Collection;
21
22
class Denier
23
{
24
    private $event;
25
26 11
    public function __construct(Dispatcher $event)
27
    {
28 11
        $this->event = $event;
29 11
    }
30
31
    public function deny(DeniableOwner $owner, Collection $permissions): void
32
    {
33 11
        $permissions->each(function (PermissionContract $permission) use ($owner) {
34 11
            $this->save($owner, $permission);
35 11
        });
36
37 10
        $this->event->dispatch(new Denied($owner, $permissions));
38 10
    }
39
40 11
    protected function save(DeniableOwner $owner, PermissionContract $permission): void
41
    {
42
        try {
43 11
            $owner->denied()->save($permission);
44 1
        } catch (Exception $exception) {
45 1
            throw new AuthorizationNotDeniedException($permission, $exception);
46
        }
47 10
    }
48
}
49