HasApiTokens::hasApiAccess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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 52
    public function tokens()
12
    {
13 52
        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 52
    public function createToken(string $name, $permissions = [])
17
    {
18 52
        $plainTextToken = Str::random(100);
19
20 52
        $token = $this->tokens()->create([
21 52
            'name' => $name,
22 52
            'token' => hash('sha256', $plainTextToken),
23 52
            'permissions' => is_array($permissions) ? array_intersect($permissions, $this->apiPermissions()) : null,
0 ignored issues
show
Bug introduced by
It seems like apiPermissions() 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

23
            'permissions' => is_array($permissions) ? array_intersect($permissions, $this->/** @scrutinizer ignore-call */ apiPermissions()) : null,
Loading history...
24
        ]);
25
26 52
        $token->plain_text_token = $plainTextToken;
27
28 52
        return $token;
29
    }
30
31 36
    public function withAccessToken($accessToken)
32
    {
33 36
        $this->accessToken = $accessToken;
34
35 36
        return $this;
36
    }
37
38 7
    public function hasApiAccess()
39
    {
40 7
        if (in_array($this->role, config('tinre.api_roles', []))) {
41 5
            return true;
42
        }
43
44 2
        return false;
45
    }
46
}
47