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

GuardChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 48
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthGuards() 0 14 3
A getGuardsProviders() 0 6 1
A defaultGuardProvider() 0 4 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