Passed
Push — master ( f82e79...44b9b8 )
by Samuel
02:12
created

PassportMultiauth::actingAs()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 2
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\Tests\Fixtures\Models\Customer;
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
     */
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
        $guard = self::getUserGuard($user);
31
32 8
        if (! in_array(HasApiTokens::class, class_uses($user))) {
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 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...
37
38 7
        app('auth')->guard($guard)->setUser($user);
39
40 7
        app('auth')->shouldUse($guard);
41 7
    }
42
43
    /**
44
     * Get the user provider on configs.
45
     *
46
     * @todo Move to class specialized in check auth configs.
47
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
48
     * @return string|null
49
     */
50 9
    public static function getUserProvider(Authenticatable $user)
51
    {
52 9
        foreach (config('auth.providers') as $provider => $config) {
53 9
            if ($user instanceof $config['model']) {
54 9
                return $provider;
55
            }
56
        }
57 2
    }
58
59
    /**
60
     * Get the guard of specific provider to `passport` driver.
61
     *
62
     * @todo Move to class specialized in check auth configs.
63
     * @param  string $provider
64
     * @return string
65
     */
66 9
    public static function getProviderGuard($provider)
67
    {
68 9
        foreach (config('auth.guards') as $guard => $content) {
69 9
            if ($content['driver'] == 'passport' && $content['provider'] == $provider) {
70 9
                return $guard;
71
            }
72
        }
73 2
    }
74
75
    /**
76
     * Get the user guard on provider with `passport` driver.
77
     *
78
     * @todo Move to class specialized in check auth configs.
79
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
80
     * @return string|null
81
     */
82 8
    public static function getUserGuard(Authenticatable $user)
83
    {
84 8
        $provider = self::getUserProvider($user);
85
86 8
        return self::getProviderGuard($provider);
87
    }
88
89
    /**
90
     * If running unit test and try authenticate an user with actingAs($user)
91
     * check the guards on request to authenticate or not the user.
92
     *
93
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
94
     */
95 7
    public static function userActing()
96
    {
97 7
        if (App::runningUnitTests() && $user = app('auth')->user()) {
98 6
            return $user;
99
        }
100 1
    }
101
}
102