ClientHasGrantsTrait::setGrants()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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->setDataValue('grant_types', implode(',', $this->getGrants()));
0 ignored issues
show
Bug introduced by
It seems like setDataValue() 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

83
        $this->/** @scrutinizer ignore-call */ 
84
               setDataValue('grant_types', implode(',', $this->getGrants()));
Loading history...
84 7
    }
85
86
    /**
87
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
88
     */
89 2
    protected function initGrantsFromDb($data = null)
90
    {
91 2
        $data = $data === null ? $this->grant_types : $data;
0 ignored issues
show
introduced by
The condition $data === null is always true.
Loading history...
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
92 2
        if (!empty($this->grant_types)) {
93 2
            $this->grants = explode(',', $this->grant_types);
94
        } else {
95
            $this->grants = [];
96
        }
97 2
    }
98
}
99