Passed
Pull Request — 2.0 (#45)
by Samuel
10:16
created

AuthConfigHelper::getUserProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SMartins\PassportMultiauth\Config;
4
5
use Illuminate\Contracts\Auth\Authenticatable;
6
7
class AuthConfigHelper
8
{
9
    /**
10
     * Get the user provider on configs.
11
     *
12
     * @param  Authenticatable $user
13
     * @return string|null
14
     */
15 13
    public static function getUserProvider(Authenticatable $user)
16
    {
17 13
        foreach (config('auth.providers') as $provider => $config) {
18 13
            if ($user instanceof $config['model']) {
19 13
                return $provider;
20
            }
21
        }
22 1
    }
23
24
    /**
25
     * Get the guard of specific provider to `passport` driver.
26
     *
27
     * @param  string $provider
28
     * @return string
29
     */
30 11
    public static function getProviderGuard($provider)
31
    {
32 11
        foreach (config('auth.guards') as $guard => $content) {
33 11
            if ($content['driver'] == 'passport' && $content['provider'] == $provider) {
34 11
                return $guard;
35
            }
36
        }
37 1
    }
38
39
    /**
40
     * Get the user guard on provider with `passport` driver.
41
     *
42
     * @param  Authenticatable $user
43
     * @return string|null
44
     */
45 9
    public static function getUserGuard(Authenticatable $user)
46
    {
47 9
        $provider = self::getUserProvider($user);
48
49 9
        return self::getProviderGuard($provider);
50
    }
51
}
52