Completed
Pull Request — master (#22)
by Samuel
11:08 queued 06:21
created

PassportMultiauth::getUserProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Mockery;
6
use Laravel\Passport\Token;
7
use Illuminate\Contracts\Auth\Authenticatable;
8
9
class PassportMultiauth
10
{
11
    /**
12
     * Set the current user for the application with the given scopes.
13
     *
14
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
15
     * @param  array  $scopes
16
     * @param  string  $guard
0 ignored issues
show
Bug introduced by
There is no parameter named $guard. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
17
     * @return void
18
     */
19 7
    public static function actingAs($user, $scopes = [])
20
    {
21 7
        $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false);
22
23 7
        foreach ($scopes as $scope) {
24 1
            $token->shouldReceive('can')->with($scope)->andReturn(true);
25
        }
26
27 7
        $guard = self::getUserGuard($user);
28
29 7
        $user->withAccessToken($token);
0 ignored issues
show
Bug introduced by
The method withAccessToken() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31 7
        app('auth')->guard($guard)->setUser($user);
32
33 7
        app('auth')->shouldUse($guard);
34 7
    }
35
36
    /**
37
     * Get the user provider on configs.
38
     *
39
     * @todo Move to class specialized in check auth configs.
40
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
41
     * @return string|null
42
     */
43 8
    public static function getUserProvider(Authenticatable $user)
44
    {
45 8
        foreach (config('auth.providers') as $provider => $config) {
46 8
            if ($user instanceof $config['model']) {
47 8
                return $provider;
48
            }
49
        }
50
51 1
        return null;
52
    }
53
54
    /**
55
     * Get the guard of specific provider to `passport` driver.
56
     *
57
     * @todo Move to class specialized in check auth configs.
58
     * @param  string $provider
59
     * @return string|null
60
     */
61 8
    public static function getProviderGuard($provider)
62
    {
63 8
        foreach (config('auth.guards') as $guard => $content) {
64 8
            if ($content['driver'] == 'passport' && $content['provider'] == $provider) {
65 8
                return $guard;
66
            }
67
        }
68
69 1
        return null;
70
    }
71
72
    /**
73
     * Get the user guard on provider with `passport` driver;
74
     *
75
     * @todo Move to class specialized in check auth configs.
76
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
77
     * @return string|null
78
     */
79 7
    public static function getUserGuard(Authenticatable $user)
80
    {
81 7
        $provider = self::getUserProvider($user);
82
83 7
        return self::getProviderGuard($provider);
84
    }
85
}
86