Completed
Push — 7.x ( cc723d...f47664 )
by Samuel
07:48 queued 10s
created

PassportMultiauth   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 68
ccs 17
cts 20
cp 0.85
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ignoreMigrations() 0 5 1
A userActing() 0 4 3
A actingAs() 0 27 5
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Exception;
6
use Illuminate\Support\Facades\App;
7
use Laravel\Passport\HasApiTokens;
8
use Laravel\Passport\Token;
9
use Mockery;
10
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
11
12
class PassportMultiauth
13
{
14
    /**
15
     * Indicates if MultiAuth migrations will be run.
16
     *
17
     * @var bool
18
     */
19
    public static $runsMigrations = true;
20
21
    /**
22
     * Configure MultiAuth to not register its migrations.
23
     *
24
     * @return static
25
     */
26
    public static function ignoreMigrations()
27
    {
28
        static::$runsMigrations = false;
29
30
        return new static;
31
    }
32
33
    /**
34
     * Set the current user for the application with the given scopes.
35
     *
36
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
37
     * @param array $scopes
38
     * @return \Illuminate\Contracts\Auth\Authenticatable
39
     * @throws Exception
40
     */
41 4
    public static function actingAs($user, $scopes = [])
42
    {
43 4
        $token = Mockery::mock(Token::class)->shouldIgnoreMissing(false);
44
45 4
        foreach ($scopes as $scope) {
46 1
            $token->shouldReceive('can')->with($scope)->andReturn(true);
47
        }
48
49 4
        $uses = array_flip(class_uses_recursive($user));
50
51 4
        if (! isset($uses[HasApiTokens::class])) {
52 1
            throw new Exception('The model ['.get_class($user).'] must uses the trait '.HasApiTokens::class);
53
        }
54
55 3
        $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

55
        $user->/** @scrutinizer ignore-call */ 
56
               withAccessToken($token);
Loading history...
56
57 3
        if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
0 ignored issues
show
Bug introduced by
Accessing wasRecentlyCreated on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58 3
            $user->wasRecentlyCreated = false;
59
        }
60
61 3
        $guard = AuthConfigHelper::getUserGuard($user);
62
63 3
        app('auth')->guard($guard)->setUser($user);
64
65 3
        app('auth')->shouldUse($guard);
66
67 3
        return $user;
68
    }
69
70
    /**
71
     * If running unit test and try authenticate an user with actingAs($user)
72
     * check the guards on request to authenticate or not the user.
73
     *
74
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
75
     */
76 3
    public static function userActing()
77
    {
78 3
        if (App::runningUnitTests() && $user = app('auth')->user()) {
79 2
            return $user;
80
        }
81 1
    }
82
}
83