Test Failed
Pull Request — 7.x (#140)
by
unknown
04:46
created

HasMultiAuthApiTokens   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 5.56%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 48
ccs 1
cts 18
cp 0.0556
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createToken() 0 18 1
A tokens() 0 10 1
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Illuminate\Container\Container;
6
use Laravel\Passport\HasApiTokens as BaseHasApiTokens;
7
use Laravel\Passport\PersonalAccessTokenFactory;
8
use Laravel\Passport\PersonalAccessTokenResult;
9
use Laravel\Passport\Token;
10
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
11
use SMartins\PassportMultiauth\Exceptions\MissingConfigException;
12
13
trait HasMultiAuthApiTokens
14
{
15 1
    use BaseHasApiTokens;
16
17
    /**
18
     * Get all of the access tokens for the user relating with Provider.
19
     *
20
     * @return \Illuminate\Database\Eloquent\Collection
21
     */
22
    public function tokens()
23
    {
24
        return $this->hasMany(Token::class, 'user_id')
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        return $this->/** @scrutinizer ignore-call */ hasMany(Token::class, 'user_id')
Loading history...
25
            ->join('oauth_access_token_providers', function ($join) {
26
                $join->on(
27
                    'oauth_access_tokens.id', '=', 'oauth_access_token_id'
28
                )->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider($this));
0 ignored issues
show
Bug introduced by
$this of type SMartins\PassportMultiauth\HasMultiAuthApiTokens is incompatible with the type Illuminate\Contracts\Auth\Authenticatable expected by parameter $user of SMartins\PassportMultiau...lper::getUserProvider(). ( Ignorable by Annotation )

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

28
                )->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider(/** @scrutinizer ignore-type */ $this));
Loading history...
29
            })->orderBy('created_at', 'desc')
30
            ->select('oauth_access_tokens.*')
31
            ->get();
32
    }
33
34
    /**
35
     * Create a new personal access token for the user and create .
36
     *
37
     * @param string $name
38
     * @param array $scopes
39
     * @return PersonalAccessTokenResult
40
     * @throws MissingConfigException
41
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
42
     */
43
    public function createToken($name, array $scopes = [])
44
    {
45
        // Backup default provider
46
        $defaultProvider = config('auth.guards.api.provider');
47
48
        $userProvider = AuthConfigHelper::getUserProvider($this);
0 ignored issues
show
Bug introduced by
$this of type SMartins\PassportMultiauth\HasMultiAuthApiTokens is incompatible with the type Illuminate\Contracts\Auth\Authenticatable expected by parameter $user of SMartins\PassportMultiau...lper::getUserProvider(). ( Ignorable by Annotation )

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

48
        $userProvider = AuthConfigHelper::getUserProvider(/** @scrutinizer ignore-type */ $this);
Loading history...
49
50
        // Change config to when the token is created set the provider from model creating the token.
51
        config(['auth.guards.api.provider' => $userProvider]);
52
53
        $token = Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
54
            $this->getKey(), $name, $scopes
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
            $this->/** @scrutinizer ignore-call */ 
55
                   getKey(), $name, $scopes
Loading history...
55
        );
56
57
        // Reset config to defaults
58
        config(['auth.guards.api.provider' => $defaultProvider]);
59
60
        return $token;
61
    }
62
}
63