HasApiTokens   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A clients() 0 4 1
A tokens() 0 4 1
A token() 0 4 1
A createToken() 0 4 1
A withAccessToken() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Traits;
6
7
use Illuminate\Database\Eloquent\Relations\MorphMany;
8
use Rinvex\Oauth\Factories\PersonalAccessTokenFactory;
9
10
trait HasApiTokens
11
{
12
    /**
13
     * The current access token for the authentication user.
14
     *
15
     * @var \Rinvex\Oauth\Models\AccessToken
16
     */
17
    protected $accessToken;
18
19
    /**
20
     * Get all of the user's registered OAuth clients.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
23
     */
24
    public function clients(): MorphMany
25
    {
26
        return $this->morphMany(config('rinvex.oauth.models.client'), 'user', 'user_type', 'user_id');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
27
    }
28
29
    /**
30
     * Get all of the access tokens for the user.
31
     *
32
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
33
     */
34
    public function tokens(): MorphMany
35
    {
36
        return $this->morphMany(config('rinvex.oauth.models.client'), 'user', 'user_type', 'user_id')->orderBy('created_at', 'desc');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
37
    }
38
39
    /**
40
     * Get the current access token being used by the user.
41
     *
42
     * @return \Rinvex\Oauth\Models\AccessToken|null
43
     */
44
    public function token()
45
    {
46
        return $this->accessToken;
47
    }
48
49
    /**
50
     * Create a new personal access token for the user.
51
     *
52
     * @param string $name
53
     * @param array  $scopes
54
     *
55
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
56
     *
57
     * @return \Rinvex\Oauth\PersonalAccessTokenResult
58
     */
59
    public function createToken($name, array $scopes = [])
60
    {
61
        return app(PersonalAccessTokenFactory::class)->make($this, $name, $scopes);
62
    }
63
64
    /**
65
     * Set the current access token for the user.
66
     *
67
     * @param \Rinvex\Oauth\Models\AccessToken $accessToken
68
     *
69
     * @return $this
70
     */
71
    public function withAccessToken($accessToken)
72
    {
73
        $this->accessToken = $accessToken;
74
75
        return $this;
76
    }
77
}
78