Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

ClientHasGrantsTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 68.97%

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
ccs 20
cts 29
cp 0.6897
wmc 12
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A firstParty() 0 4 1
A addGrants() 0 5 2
A removeGrants() 0 7 2
A getGrants() 0 4 1
A setGrants() 0 5 1
A handlesGrant() 0 4 1
A hasGrant() 0 4 1
A updateGrantsDbField() 0 4 1
A initGrantsFromDb() 0 8 2
1
<?php
2
3
namespace ByTIC\Hello\Models\Clients\Traits;
4
5
use ByTIC\Hello\Utility\GrantsHelper;
6
7
/**
8
 * Trait ClientHasGrantsTrait
9
 * @package ByTIC\Hello\Models\Clients\Traits
10
 */
11
trait ClientHasGrantsTrait
12
{
13
    protected $grants = [];
14
15
    /**
16
     * @return bool
17
     */
18
    public function firstParty(): bool
19
    {
20
        return $this->hasGrant(GrantsHelper::GRANT_TYPE_USER_CREDENTIALS);
21
    }
22
23
    /**
24
     * @param string|array $grants
25
     * @see GrantHelper::GRANT_TYPE_*
26
     */
27 7
    public function addGrants($grants)
28
    {
29 7
        $grants = is_array($grants) ? $grants : [$grants];
30 7
        $this->setGrants(array_unique(array_merge($this->grants, $grants)));
31 7
    }
32
33
    /**
34
     * @param string|array $grants
35
     * @see GrantHelper::GRANT_TYPE_*
36
     */
37
    public function removeGrants($grants)
38
    {
39
        if (($key = array_search($grants, $this->grants)) !== false) {
40
            unset($this->grants[$key]);
41
        }
42
        $this->updateGrantsDbField();
43
    }
44
45
    /**
46
     * @return array
47
     */
48 11
    public function getGrants(): array
49
    {
50 11
        return $this->grants;
51
    }
52
53
    /**
54
     * @param array $grants
55
     */
56 7
    public function setGrants(array $grants)
57
    {
58 7
        $this->grants = $grants;
59 7
        $this->updateGrantsDbField();
60 7
    }
61
62
    /**
63
     * @param $grant
64
     * @return bool
65
     */
66
    public function handlesGrant($grant)
67
    {
68
        return $this->hasGrant($grant);
69
    }
70
71
    /**
72
     * @param string $grant
73
     * @return bool
74
     * @see GrantHelper::GRANT_TYPE_*
75
     */
76 1
    public function hasGrant(string $grant): bool
77
    {
78 1
        return array_search($grant, $this->grants) !== false;
79
    }
80
81 7
    protected function updateGrantsDbField()
82
    {
83 7
        $this->_data['grant_types'] = implode(',', $this->getGrants());
0 ignored issues
show
Bug introduced by
The property _data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
84 7
    }
85
86 10
    protected function initGrantsFromDb()
87
    {
88 10
        if (!empty($this->grant_types)) {
89 2
            $this->grants = explode(',', $this->grant_types);
0 ignored issues
show
Bug introduced by
The property grant_types does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
        } else {
91 8
            $this->grants = [];
92
        }
93 10
    }
94
}
95