Completed
Push — develop ( 0bdcf1...3b506c )
by Enea
09:31 queued 08:05
created

Operator::throwErrorIfNotSaved()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2.0625
1
<?php
2
/**
3
 * Created on 13/02/18 by enea dhack.
4
 */
5
6
namespace Enea\Authorization\Operators;
7
8
use Enea\Authorization\Contracts\{
9
    Grantable, PermissionsOwner, RolesOwner
10
};
11
use Enea\Authorization\Events\Operation;
12
use Enea\Authorization\Exceptions\{
13
    GrantableIsNotValidModelException
14
};
15
use Illuminate\Contracts\Events\Dispatcher;
16
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Support\Collection;
18
19
abstract class Operator
20
{
21
    private $event;
22
23 34
    public function __construct(Dispatcher $event)
24
    {
25 34
        $this->event = $event;
26 34
    }
27
28
    abstract public function permissions(PermissionsOwner $owner, Collection $permissions): void;
29
30
    abstract public function roles(RolesOwner $owner, Collection $roles): void;
31
32 34
    protected function castToModel(Grantable $grantable): Model
33
    {
34 34
        if (! $grantable instanceof Model) {
35
            throw GrantableIsNotValidModelException::make($grantable);
36
        }
37
38 34
        return $grantable;
39
    }
40
41 34
    protected function dispatchEvent(Operation $operation): void
42
    {
43 34
        $this->event->dispatch($operation);
44 34
    }
45
}
46