Oauth2ClientRelationTrait::setAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\models\traits;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
7
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2ClientQueryInterface;
8
use yii\base\InvalidConfigException;
9
10
trait Oauth2ClientRelationTrait
11
{
12
    /**
13
     * Wrapper for parent's getClient() relation to avoid name conflicts
14
     * @return Oauth2ClientQueryInterface
15
     * @since 1.0.0
16
     */
17 6
    public function getClientRelation()
18
    {
19 6
        return parent::getClient();
20
    }
21
22
    /**
23
     * @inheritDoc
24
     */
25 35
    public function __set($name, $value)
26
    {
27 35
        if ($name === 'client_id' && $this->isRelationPopulated('clientRelation')) {
0 ignored issues
show
Bug introduced by
It seems like isRelationPopulated() 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

27
        if ($name === 'client_id' && $this->/** @scrutinizer ignore-call */ isRelationPopulated('clientRelation')) {
Loading history...
28
            //@phpstan-ignore-next-line "Cannot unset offset", but checked with `isRelationPopulated`
29 4
            unset($this['clientRelation']);
30
        }
31 35
        parent::__set($name, $value);
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 35
    public function setAttribute($name, $value)
38
    {
39 35
        if ($name === 'client_id' && $this->isRelationPopulated('clientRelation')) {
40
            //@phpstan-ignore-next-line "Cannot unset offset", but checked with `isRelationPopulated`
41 2
            unset($this['clientRelation']);
42
        }
43 35
        parent::setAttribute($name, $value);
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 9
    public function setClient(ClientEntityInterface $client)
50
    {
51 9
        if (!($client instanceof Oauth2ClientInterface)) {
52 2
            throw new InvalidConfigException(get_class($client) . ' must implement ' . Oauth2ClientInterface::class);
53
        }
54
55 7
        $this->client_id = $client->getPrimaryKey();
0 ignored issues
show
Bug Best Practice introduced by
The property client_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56 7
        $this->populateRelation('clientRelation', $client);
0 ignored issues
show
Bug introduced by
It seems like populateRelation() 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

56
        $this->/** @scrutinizer ignore-call */ 
57
               populateRelation('clientRelation', $client);
Loading history...
57
    }
58
59
    /**
60
     * Get the client for this model.
61
     * @return Oauth2ClientInterface
62
     * @since 1.0.0
63
     */
64 7
    public function getClient()
65
    {
66 7
        return $this->clientRelation;
67
    }
68
}
69