Gate   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 31
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A youShouldHaveRole() 0 3 1
A thisGateShouldAllow() 0 6 1
A defineNewGate() 0 15 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