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

PassportMultiauth::getUserProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A PassportMultiauth::userActing() 0 4 3
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Mockery;
6
use Exception;
7
use Laravel\Passport\Token;
8
use Laravel\Passport\HasApiTokens;
9
use Illuminate\Support\Facades\App;
10
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
11
12
class PassportMultiauth
13
{
14
    /**
15
     * Set the current user for the application with the given scopes.
16
     *
17
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
18
     * @param  array $scopes
19
     * @return void
20
     * @throws Exception
21
     */
22 8
    public static function actingAs($user, $scopes = [])
23
    {
24 8
        $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false);
25
26 8
        foreach ($scopes as $scope) {
27 1
            $token->shouldReceive('can')->with($scope)->andReturn(true);
28
        }
29
30 8
        $uses = array_flip(class_uses_recursive($user));
31
32 8
        if (! isset($uses[HasApiTokens::class])) {
33 1
            throw new Exception('The model ['.get_class($user).'] must uses the trait '.HasApiTokens::class);
34
        }
35
36 7
        $user->withAccessToken($token);
0 ignored issues
show
Bug introduced by
The method withAccessToken() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $user->/** @scrutinizer ignore-call */ 
37
               withAccessToken($token);
Loading history...
37
38 7
        $guard = AuthConfigHelper::getUserGuard($user);
39
40 7
        app('auth')->guard($guard)->setUser($user);
41
42 7
        app('auth')->shouldUse($guard);
43 7
    }
44
45
    /**
46
     * If running unit test and try authenticate an user with actingAs($user)
47
     * check the guards on request to authenticate or not the user.
48
     *
49
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
50
     */
51 7
    public static function userActing()
52
    {
53 7
        if (App::runningUnitTests() && $user = app('auth')->user()) {
54 6
            return $user;
55
        }
56 1
    }
57
}
58