Passed
Push — master ( 4ba325...f96733 )
by Iman
03:38
created

YouShouldHave::youShouldAlways()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
ccs 3
cts 3
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
7
class YouShouldHave
8
{
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 65
    public function __construct(Chain $chain)
23
    {
24 65
        $this->chain = $chain;
25 65
    }
26
27 43
    public function youShouldHaveRole(string $role): Otherwise
28
    {
29 43
        return $this->thisGateShouldAllow('heyman.youShouldHaveRole', $role);
30
    }
31
32 47
    public function thisGateShouldAllow($gate, ...$parameters): Otherwise
33
    {
34 47
        $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 47
        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 3
    public function thisValueShouldAllow($value): Otherwise
58
    {
59
        $this->chain->predicate = function () use ($value) {
60 3
            return (bool) $value;
61
        };
62
63 3
        return app(Otherwise::class);
64
    }
65
66 6
    public function youShouldBeGuest(): Otherwise
67
    {
68
        $this->chain->predicate = function () {
69 6
            return auth()->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 1
    public function youShouldBeLoggedIn(): Otherwise
85
    {
86
        $this->chain->predicate = function () {
87 1
            return auth()->check();
88
        };
89
90 1
        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
    /**
103
     * @param $gate
104
     *
105
     * @return string
106
     */
107 47
    private function defineNewGate($gate): string
108
    {
109
        // Define a Gate for inline closures passed as gate
110 47
        if (is_callable($gate)) {
111 1
            $closure = $gate;
112 1
            $gate = str_random(10);
113 1
            Gate::define($gate, $closure);
114
        }
115
116
        // Define a Gate for "class@method" gates
117 47
        if (is_string($gate) && str_contains($gate, '@')) {
118 1
            Gate::define($gate, $gate);
119
        }
120
121 47
        return $gate;
122
    }
123
}
124