Passed
Pull Request — master (#22)
by Samuel
04:56 queued 02:08
created

PassportMultiauth::getUserProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

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 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
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\Support\Facades\App;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
10
class PassportMultiauth
11
{
12
    /**
13
     * Set the current user for the application with the given scopes.
14
     *
15
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
16
     * @param  array  $scopes
17
     * @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...
18
     * @return void
19
     */
20 7
    public static function actingAs($user, $scopes = [])
21
    {
22 7
        $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false);
23
24 7
        foreach ($scopes as $scope) {
25 1
            $token->shouldReceive('can')->with($scope)->andReturn(true);
26
        }
27
28 7
        $guard = self::getUserGuard($user);
29
30 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...
31
32 7
        app('auth')->guard($guard)->setUser($user);
33
34 7
        app('auth')->shouldUse($guard);
35 7
    }
36
37
    /**
38
     * Get the user provider on configs.
39
     *
40
     * @todo Move to class specialized in check auth configs.
41
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
42
     * @return string|null
43
     */
44 8
    public static function getUserProvider(Authenticatable $user)
45
    {
46 8
        foreach (config('auth.providers') as $provider => $config) {
47 8
            if ($user instanceof $config['model']) {
48 8
                return $provider;
49
            }
50
        }
51 1
    }
52
53
    /**
54
     * Get the guard of specific provider to `passport` driver.
55
     *
56
     * @todo Move to class specialized in check auth configs.
57
     * @param  string $provider
58
     * @return string
59
     */
60 8
    public static function getProviderGuard($provider)
61
    {
62 8
        foreach (config('auth.guards') as $guard => $content) {
63 8
            if ($content['driver'] == 'passport' && $content['provider'] == $provider) {
64 8
                return $guard;
65
            }
66
        }
67 1
    }
68
69
    /**
70
     * Get the user guard on provider with `passport` driver.
71
     *
72
     * @todo Move to class specialized in check auth configs.
73
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
74
     * @return string|null
75
     */
76 7
    public static function getUserGuard(Authenticatable $user)
77
    {
78 7
        $provider = self::getUserProvider($user);
79
80 7
        return self::getProviderGuard($provider);
81
    }
82
83
    /**
84
     * If running unit test and try authenticate an user with actingAs($user)
85
     * check the guards on request to authenticate or not the user.
86
     *
87
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
88
     */
89 7
    public static function hasUserActing()
90
    {
91 7
        if (App::runningUnitTests() && $user = app('auth')->user()) {
92 6
            return $user;
93
        }
94 1
    }
95
}
96