HasMultiAuthApiTokens::tokens()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Laravel\Passport\Token;
6
use Illuminate\Container\Container;
7
use Laravel\Passport\PersonalAccessTokenResult;
8
use Laravel\Passport\PersonalAccessTokenFactory;
9
use Laravel\Passport\HasApiTokens as BaseHasApiTokens;
10
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
11
use SMartins\PassportMultiauth\Exceptions\MissingConfigException;
12
13
trait HasMultiAuthApiTokens
14
{
15
    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 2
    public function tokens()
23
    {
24 2
        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 2
                $join->on(
27 2
                    'oauth_access_tokens.id', '=', 'oauth_access_token_id'
28 2
                )->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 2
            })->orderBy('created_at', 'desc')
30 1
            ->select('oauth_access_tokens.*')
31 1
            ->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
     */
42 4
    public function createToken($name, array $scopes = [])
43
    {
44
        // Backup default provider
45 4
        $defaultProvider = config('auth.guards.api.provider');
46
47 4
        $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

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

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