Completed
Push — master ( e4a954...682c69 )
by Iman
03:08
created

YouShouldHave   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 100
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A thisClosureShouldAllow() 0 3 1
A youShouldHaveRole() 0 3 1
A thisGateShouldAllow() 0 9 1
A thisMethodShouldAllow() 0 7 1
A thisValueShouldAllow() 0 7 1
A youShouldBeGuest() 0 7 1
A youShouldBeLoggedIn() 0 7 1
A immediately() 0 7 1
A sessionShouldHave() 0 7 1
A defineNewGate() 0 15 4
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Illuminate\Support\Facades\Gate;
6
7
class YouShouldHave
8
{
9
    public $predicate;
10
11 43
    public function youShouldHaveRole(string $role)
12
    {
13 43
        return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role);
14
    }
15
16 47
    public function thisGateShouldAllow($gate, ...$args)
17
    {
18 47
        $gate = $this->defineNewGate($gate);
19
20
        $this->predicate = function () use ($gate, $args) {
21 43
            return Gate::allows($gate, $args);
22
        };
23
24 47
        return new Otherwise();
25
    }
26
27 3
    public function thisClosureShouldAllow($callback, array $parameters = [])
28
    {
29 3
        return $this->thisMethodShouldAllow($callback, $parameters);
30
    }
31
32 5
    public function thisMethodShouldAllow($callback, array $parameters = [])
33
    {
34
        $this->predicate = function () use ($callback, $parameters) {
35 5
            return (bool) app()->call($callback, $parameters);
36
        };
37
38 5
        return new Otherwise();
39
    }
40
41 3
    public function thisValueShouldAllow($value)
42
    {
43
        $this->predicate = function () use ($value) {
44 3
            return (bool) $value;
45
        };
46
47 3
        return new Otherwise();
48
    }
49
50 6
    public function youShouldBeGuest()
51
    {
52
        $this->predicate = function () {
53 6
            return auth()->guest();
54
        };
55
56 6
        return new Otherwise();
57
    }
58
59
60 1
    public function sessionShouldHave($key)
61
    {
62
        $this->predicate = function () use ($key) {
63 1
            return session()->has($key);
64
        };
65
66 1
        return new Otherwise();
67
    }
68
69 1
    public function youShouldBeLoggedIn()
70
    {
71
        $this->predicate = function () {
72 1
            return auth()->check();
73
        };
74
75 1
        return new Otherwise();
76
    }
77
78 3
    public function immediately()
79
    {
80
        $this->predicate = function () {
81 3
            return false;
82
        };
83
84 3
        return new Responder();
85
    }
86
87
    /**
88
     * @param $gate
89
     *
90
     * @return string
91
     */
92 47
    private function defineNewGate($gate): string
93
    {
94
        // Define a Gate for inline closures passed as gate
95 47
        if (is_callable($gate)) {
96 1
            $closure = $gate;
97 1
            $gate = str_random(10);
98 1
            Gate::define($gate, $closure);
99
        }
100
101
        // Define a Gate for "class@method" gates
102 47
        if (is_string($gate) && str_contains($gate, '@')) {
103 1
            Gate::define($gate, $gate);
104
        }
105
106 47
        return $gate;
107
    }
108
}
109