Completed
Push — master ( 280d59...d4e593 )
by Iman
10:04
created

YouShouldHave::yourRequestShouldBeValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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