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

HasMultiAuthApiTokens   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 46
ccs 16
cts 16
cp 1
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 Laravel\Passport\Token;
6
use Laravel\Passport\HasApiTokens as BaseHasApiTokens;
7
use Laravel\Passport\PersonalAccessTokenFactory;
8
use Illuminate\Container\Container;
9
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
10
11
trait HasMultiAuthApiTokens
12
{
13
    use BaseHasApiTokens;
14
15
    /**
16
     * Get all of the access tokens for the user relating with Provider.
17
     *
18
     * @return \Illuminate\Database\Eloquent\Collection
19
     */
20 1
    public function tokens()
21
    {
22 1
        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

22
        return $this->/** @scrutinizer ignore-call */ hasMany(Token::class, 'user_id')
Loading history...
23
            ->join('oauth_access_token_providers', function ($join) {
24 1
                $join->on(
25 1
                    'oauth_access_tokens.id', '=', 'oauth_access_token_id'
26 1
                )->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

26
                )->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider(/** @scrutinizer ignore-type */ $this));
Loading history...
27 1
            })->orderBy('created_at', 'desc')
28 1
            ->select('oauth_access_tokens.*')
29 1
            ->get();
30
    }
31
32
    /**
33
     * Create a new personal access token for the user and create .
34
     *
35
     * @param  string  $name
36
     * @param  array  $scopes
37
     * @return \Laravel\Passport\PersonalAccessTokenResult
38
     */
39 3
    public function createToken($name, array $scopes = [])
40
    {
41
        // Backup default provider
42 3
        $defaultProvider = config('auth.guards.api.provider');
43
44 3
        $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

44
        $userProvider = AuthConfigHelper::getUserProvider(/** @scrutinizer ignore-type */ $this);
Loading history...
45
46
        // Change config to when the token is created set the provider from model creating the token.
47 3
        config(['auth.guards.api.provider' => $userProvider]);
48
49 3
        $token = Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
50 3
            $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

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