Test Failed
Push — new-feature-manage-tokens ( 625fd7 )
by Samuel
03:31
created

HasApiTokens::tokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SMartins\PassportMultiauth;
4
5
use Laravel\Passport\Token;
6
use Laravel\Passport\PersonalAccessTokenFactory;
7
use Illuminate\Container\Container;
8
use SMartins\PassportMultiauth\Config\AuthConfigHelper;
9
10
trait HasApiTokens
11
{
12
    use \Laravel\Passport\HasApiTokens;
13
14
    /**
15
     * Get all of the access tokens for the user relating with Provider.
16
     *
17
     * @return \Illuminate\Database\Eloquent\Collection
18
     */
19
    public function tokens()
20
    {
21
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
            ->join('oauth_access_token_providers', function ($join) {
23
                $join->on(
24
                    'oauth_access_tokens.id', '=', 'oauth_access_token_id'
25
                )->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider($this));
26
            })->orderBy('created_at', 'desc')
27
            ->select('oauth_access_tokens.*')
28
            ->get();
29
    }
30
31
    /**
32
     * Create a new personal access token for the user and create .
33
     *
34
     * @param  string  $name
35
     * @param  array  $scopes
36
     * @return \Laravel\Passport\PersonalAccessTokenResult
37
     */
38
    public function createToken($name, array $scopes = [])
39
    {
40
        // Backup default provider
41
        $defaultProvider = config('auth.guards.api.provider');
42
43
        $userProvider = AuthConfigHelper::getUserProvider($this);
44
45
        // Change config to when the token is created set the provider from model creating the token.
46
        config(['auth.guards.api.provider' => $userProvider]);
47
48
        $token = Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
49
            $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
        );
51
52
        // Reset config to defaults
53
        config(['auth.guards.api.provider' => $defaultProvider]);
54
55
        return $token;
56
    }
57
}
58