Gate::defineNewGate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Imanghafoori\HeyMan\Plugins\Conditions;
4
5
use Illuminate\Support\Facades\Gate as GateFacade;
6
use Illuminate\Support\Str;
7
8
class Gate
9
{
10 6
    public function thisGateShouldAllow($gate, ...$parameters)
11
    {
12 6
        $gate = $this->defineNewGate($gate);
13
14 6
        return function (...$payload) use ($gate, $parameters) {
15 6
            return GateFacade::allows($gate, (array_merge($parameters, ...$payload)));
16 6
        };
17
    }
18
19 6
    private function defineNewGate($gate)
20
    {
21
        // Define a Gate for inline closures passed as gate
22 6
        if (is_callable($gate)) {
23 1
            $closure = $gate;
24 1
            $gate = Str::random(10);
25 1
            GateFacade::define($gate, $closure);
26
        }
27
28
        // Define a Gate for "class@method" gates
29 6
        if (is_string($gate) && Str::contains($gate, '@')) {
30 1
            GateFacade::define($gate, $gate);
31
        }
32
33 6
        return $gate;
34
    }
35
36 3
    public function youShouldHaveRole(string $role)
37
    {
38 3
        return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role);
39
    }
40
}
41