Passed
Push — new-feature-manage-tokens ( 228753...089f0c )
by Samuel
02: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
<?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 Illuminate\Contracts\Auth\Authenticatable;
11
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
12
13
class PassportMultiauth
14
{
15
    /**
16
     * Set the current user for the application with the given scopes.
17
     *
18
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
19
     * @param  array $scopes
20
     * @return void
21
     * @throws Exception
22
     */
23 8
    public static function actingAs($user, $scopes = [])
24
    {
25 8
        $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false);
26
27 8
        foreach ($scopes as $scope) {
28 1
            $token->shouldReceive('can')->with($scope)->andReturn(true);
29
        }
30
31 8
        $uses = array_flip(class_uses_recursive($user));
32
33 8
        if (! isset($uses[HasApiTokens::class])) {
34 1
            throw new Exception('The model ['.get_class($user).'] must uses the trait '.HasApiTokens::class);
35
        }
36
37 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...
38
39 7
        $guard = AuthConfigHelper::getUserGuard($user);
40
41 7
        app('auth')->guard($guard)->setUser($user);
42
43 7
        app('auth')->shouldUse($guard);
44 7
    }
45
46
    /**
47
     * If running unit test and try authenticate an user with actingAs($user)
48
     * check the guards on request to authenticate or not the user.
49
     *
50
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
51
     */
52 7
    public static function userActing()
53
    {
54 7
        if (App::runningUnitTests() && $user = app('auth')->user()) {
55 6
            return $user;
56
        }
57 1
    }
58
}
59