Passed
Pull Request — master (#22)
by Samuel
02:05
created

PassportMultiauth   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 77
ccs 22
cts 22
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A actingAs() 0 16 2
A getUserProvider() 0 10 3
A getProviderGuard() 0 10 4
A getUserGuard() 0 6 1
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
17
     * @return void
18
     */
19 7
    public static function actingAs($user, $scopes = [], $guard = 'api')
0 ignored issues
show
Unused Code introduced by
The parameter $guard is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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