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
![]() |
|||||||
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
|
|||||||
56 | 7 | $this->populateRelation('clientRelation', $client); |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
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 |