HasCallback::orNotFound()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace LaraCrafts\GeoRoutes\Concerns;
4
5
use Illuminate\Support\Str;
6
7
trait HasCallback
8
{
9
    /**
10
     * The callback to execute if the visitor
11
     * is not allowed.
12
     *
13
     * @var array
14
     */
15
    protected $callback;
16
17
    /**
18
     * The callbacks' proxies.
19
     *
20
     * @var array
21
     */
22
    protected static $proxies;
23
24
    /**
25
     * Load the available proxies.
26
     */
27 112
    protected static function loadProxies()
28
    {
29 112
        if (static::$proxies !== null) {
0 ignored issues
show
introduced by
The condition static::proxies !== null is always true.
Loading history...
30 96
            return;
31
        }
32
33 16
        static::$proxies = [];
34 16
        $callbacks = config('geo-routes.routes.callbacks');
35
36 16
        foreach ($callbacks as $key => $callback) {
37
            static::$proxies['or' . Str::studly($key)] = $callback;
38
        }
39 16
    }
40
41
    /**
42
     * Return a HTTP 404 error if access is denied.
43
     *
44
     * @return $this
45
     */
46 32
    public function orNotFound()
47
    {
48 32
        return $this->setCallback('LaraCrafts\GeoRoutes\Callbacks::notFound', func_get_args());
49
    }
50
51
    /**
52
     * Redirect to given route if access is denied.
53
     *
54
     * @param string $routeName
55
     *
56
     * @return $this
57
     */
58 32
    public function orRedirectTo(string $routeName)
59
    {
60 32
        return $this->setCallback('LaraCrafts\GeoRoutes\Callbacks::redirectTo', func_get_args());
61
    }
62
63
    /**
64
     * Return a HTTP 401 error if access is denied (this is the default behavior).
65
     *
66
     * @return $this
67
     */
68
    public function orUnauthorized()
69
    {
70
        $this->callback = null;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Set the callback.
77
     *
78
     * @param callable $callback
79
     * @param array $arguments
80
     *
81
     * @return $this
82
     */
83 64
    protected function setCallback(callable $callback, array $arguments)
84
    {
85 64
        if (is_string($callback) && Str::contains($callback, '@')) {
86
            $callback = Str::parseCallback($callback, '__invoke');
87
            $callback[0] = resolve($callback[0]);
88
        }
89
90 64
        $this->callback = [$callback, $arguments];
91
92 64
        return $this;
93
    }
94
95
    /**
96
     * Determine if a callback exists by a given name.
97
     *
98
     * @param string $name
99
     *
100
     * @return boolean
101
     */
102 16
    protected function callbackExists(string $name)
103
    {
104 16
        return array_key_exists($name, static::$proxies);
105
    }
106
}
107