Completed
Push — master ( 3de69c...6400db )
by Iman
05:21
created

YouShouldHave   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 126
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A thisValueShouldAllow() 0 7 1
A thisClosureShouldAllow() 0 3 1
A youShouldHaveRole() 0 3 1
A thisGateShouldAllow() 0 9 1
A thisMethodShouldAllow() 0 7 1
A sessionShouldHave() 0 7 1
A __construct() 0 3 1
A youShouldAlways() 0 7 1
A youShouldBeLoggedIn() 0 7 1
A youShouldBeGuest() 0 7 1
A defineNewGate() 0 15 4
A yourRequestShouldBeValid() 0 10 2
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Illuminate\Support\Facades\Gate;
6
use Illuminate\Support\Facades\Validator;
7
8
class YouShouldHave
9
{
10
    public $predicate;
11
12
    /**
13
     * @var \Imanghafoori\HeyMan\Chain
14
     */
15
    private $chain;
16
17
    /**
18
     * YouShouldHave constructor.
19
     *
20
     * @param \Imanghafoori\HeyMan\Chain $chain
21
     */
22 83
    public function __construct(Chain $chain)
23
    {
24 83
        $this->chain = $chain;
25 83
    }
26
27 55
    public function youShouldHaveRole(string $role): Otherwise
28
    {
29 55
        return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role);
30
    }
31
32 59
    public function thisGateShouldAllow($gate, ...$parameters): Otherwise
33
    {
34 59
        $gate = $this->defineNewGate($gate);
35
36
        $this->chain->predicate = function (...$payload) use ($gate, $parameters) {
37 44
            return Gate::allows($gate, (array_merge($parameters, ...$payload)));
38
        };
39
40 59
        return app(Otherwise::class);
41
    }
42
43 4
    public function thisClosureShouldAllow($callback, array $parameters = []): Otherwise
44
    {
45 4
        return $this->thisMethodShouldAllow($callback, $parameters);
46
    }
47
48 6
    public function thisMethodShouldAllow($callback, array $parameters = []): Otherwise
49
    {
50
        $this->chain->predicate = function (...$payload) use ($callback, $parameters) {
51 6
            return (bool) app()->call($callback, array_merge($parameters, ...$payload));
52
        };
53
54 6
        return app(Otherwise::class);
55
    }
56
57 7
    public function thisValueShouldAllow($value): Otherwise
58
    {
59
        $this->chain->predicate = function () use ($value) {
60 5
            return (bool) $value;
61
        };
62
63 7
        return app(Otherwise::class);
64
    }
65
66 6
    public function youShouldBeGuest($guard = null): Otherwise
67
    {
68
        $this->chain->predicate = function () use ($guard) {
69 6
            return auth($guard)->guest();
70
        };
71
72 6
        return app(Otherwise::class);
73
    }
74
75 1
    public function sessionShouldHave($key): Otherwise
76
    {
77
        $this->chain->predicate = function () use ($key) {
78 1
            return session()->has($key);
79
        };
80
81 1
        return app(Otherwise::class);
82
    }
83
84 3
    public function youShouldBeLoggedIn($guard = null): Otherwise
85
    {
86
        $this->chain->predicate = function () use ($guard) {
87 2
            return auth($guard)->check();
88
        };
89
90 3
        return app(Otherwise::class);
91
    }
92
93 1
    public function youShouldAlways()
94
    {
95
        $this->chain->predicate = function () {
96 1
            return false;
97
        };
98
99 1
        return app(Actions::class);
100
    }
101
102 2
    public function yourRequestShouldBeValid($rules)
103
    {
104
        $this->chain->predicate = function () use ($rules) {
105 1
            if(is_callable($rules))
106
                $rules = $rules();
107 1
            $validator = Validator::make(request()->all(), $rules);
108 1
            $validator->validate();
109
        };
110
111 2
        app(Chain::class)->submitChainConfig();
112 2
    }
113
114
    /**
115
     * @param $gate
116
     *
117
     * @return string
118
     */
119 59
    private function defineNewGate($gate): string
120
    {
121
        // Define a Gate for inline closures passed as gate
122 59
        if (is_callable($gate)) {
123 1
            $closure = $gate;
124 1
            $gate = str_random(10);
125 1
            Gate::define($gate, $closure);
126
        }
127
128
        // Define a Gate for "class@method" gates
129 59
        if (is_string($gate) && str_contains($gate, '@')) {
130 1
            Gate::define($gate, $gate);
131
        }
132
133 59
        return $gate;
134
    }
135
}
136