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