AuthConfigHelper::getProviderGuard()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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