Passed
Push — master ( f82e79...44b9b8 )
by Samuel
02:12
created

GuardChecker::defaultGuardProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SMartins\PassportMultiauth\Guards;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
8
class GuardChecker
9
{
10
    /**
11
     * Get guards passed as parameters to `auth` middleware.
12
     *
13
     * @param  \Illuminate\Http\Request $request
14
     * @return array
15
     */
16 4
    public static function getAuthGuards(Request $request)
17
    {
18 4
        $middlewares = $request->route()->middleware();
19
20 4
        $guards = [];
21 4
        foreach ($middlewares as $middleware) {
22 4
            if (Str::startsWith($middleware, 'auth')) {
23 3
                $explodedGuards = explode(',', Str::after($middleware, ':'));
24 4
                $guards = array_unique(array_merge($guards, $explodedGuards));
25
            }
26
        }
27
28 4
        return $guards;
29
    }
30
31
    /**
32
     * Get guards provider returning a assoc array with provider on key and
33
     * guard on value.
34
     *
35
     * @param  array $guards
36
     * @return array
37
     */
38 2
    public static function getGuardsProviders($guards)
39
    {
40
        return collect($guards)->mapWithKeys(function ($guard) {
41 2
            return [GuardChecker::defaultGuardProvider($guard) => $guard];
42 2
        });
43
    }
44
45
    /**
46
     * Get default provider from guard.
47
     *
48
     * @param  string $guard
49
     * @return string|null
50
     */
51 3
    public static function defaultGuardProvider($guard)
52
    {
53 3
        return config('auth.guards.'.$guard.'.provider');
54
    }
55
}
56