Completed
Push — master ( 032f70...bff1e6 )
by Iman
03:00 queued 21s
created

YouShouldHave::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
     */
22 87
    public function __construct(Chain $chain)
23
    {
24 87
        $this->chain = $chain;
25 87
    }
26
27 56
    public function youShouldHaveRole(string $role): Otherwise
28
    {
29 56
        return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role);
30
    }
31
32 60
    public function thisGateShouldAllow($gate, ...$parameters): Otherwise
33
    {
34 60
        $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 60
        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 9
    public function thisValueShouldAllow($value): Otherwise
58
    {
59
        $this->chain->predicate = function () use ($value) {
60 5
            return (bool) $value;
61
        };
62
63 9
        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(): Actions
94
    {
95
        $this->chain->predicate = function () {
96 1
            return false;
97
        };
98
99 1
        return app(Actions::class);
100
    }
101
102
    /**
103
     * @param $rules
104
     *
105
     * @return null
106
     */
107 3
    public function yourRequestShouldBeValid($rules)
108
    {
109
        $this->chain->predicate = function () use ($rules) {
110 2
            if (is_callable($rules)) {
111 1
                $rules = $rules();
112
            }
113 2
            $validator = Validator::make(request()->all(), $rules);
114 2
            $validator->validate();
115
        };
116
117 3
        app(Chain::class)->submitChainConfig();
118 3
    }
119
120
    /**
121
     * @param $gate
122
     *
123
     * @return string
124
     */
125 60
    private function defineNewGate($gate): string
126
    {
127
        // Define a Gate for inline closures passed as gate
128 60
        if (is_callable($gate)) {
129 1
            $closure = $gate;
130 1
            $gate = str_random(10);
131 1
            Gate::define($gate, $closure);
132
        }
133
134
        // Define a Gate for "class@method" gates
135 60
        if (is_string($gate) && str_contains($gate, '@')) {
136 1
            Gate::define($gate, $gate);
137
        }
138
139 60
        return $gate;
140
    }
141
}
142