Passed
Push — master ( 58d207...d4720a )
by Paul
07:20
created

HasApiTokens::hasApiAccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Devpri\Tinre\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait HasApiTokens
8
{
9
    public $accessToken;
10
11
    public function tokens()
12
    {
13
        return $this->hasMany('Devpri\Tinre\Models\AccessToken');
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

13
        return $this->/** @scrutinizer ignore-call */ hasMany('Devpri\Tinre\Models\AccessToken');
Loading history...
14
    }
15
16
    public function createToken(string $name, $permissions = [])
17
    {
18
        $plainTextToken = Str::random(100);
19
20
        $token = $this->tokens()->create([
21
            'name' => $name,
22
            'token' => hash('sha256', $plainTextToken),
23
            'permissions' => $permissions,
24
        ]);
25
26
        $token->plain_text_token = $plainTextToken;
27
28
        return $token;
29
    }
30
31
    public function withAccessToken($accessToken)
32
    {
33
        $this->accessToken = $accessToken;
34
35
        return $this;
36
    }
37
38
    public function hasApiAccess()
39
    {
40
        if(in_array($this->role, config('tinre.api_roles', []))) {
41
            return true;
42
        }
43
        
44
        return false;
45
46
    }
47
}
48