Skip to content

Commit 90f8508

Browse files
committed
fix: Property getters
1 parent acc919d commit 90f8508

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/User.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace Maicol07\SSO;
33

44
use GuzzleHttp\Exception\ClientException;
5+
use Maicol07\Flarum\Api\Resource\Collection;
6+
use Maicol07\Flarum\Api\Resource\Item;
57
use Maicol07\SSO\User\Attributes;
68
use Maicol07\SSO\User\Relationships;
79
use Maicol07\SSO\User\Traits\Auth;
@@ -118,6 +120,7 @@ public function fetch(): bool
118120
{
119121
try {
120122
$user = $this->flarum->api->users($this->attributes->username)->request();
123+
assert($user instanceof Item);
121124
} catch (ClientException $e) {
122125
if ($e->getCode() === 404 && $e->getResponse()->getReasonPhrase() === "Not Found") {
123126
// User doesn't exists in Flarum
@@ -128,20 +131,22 @@ public function fetch(): bool
128131
throw $e;
129132
}
130133

131-
$this->id = $user->id;
134+
$this->id = $user->id ?? null;
132135

133136
// Set attributes
134-
foreach ($user->attributes as $attribute => $value) {
137+
foreach ($user->attributes ?? [] as $attribute => $value) {
135138
$this->attributes->$attribute = $value;
136139
}
137140

141+
$groups = $user->relationships['groups'] ?? [];
142+
138143
// Admin?
139-
if (array_key_exists(1, $user->relationships['groups'])) {
144+
if (array_key_exists(1, $groups)) {
140145
$this->isAdmin = true;
141146
}
142147

143148
// Set groups
144-
foreach ($user->relationships['groups'] as $group) {
149+
foreach ($groups as $group) {
145150
$this->relationships->groups[] = $group->attributes['nameSingular'];
146151
}
147152

src/User/Attributes.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ public function __set(string $name, mixed $value): void
4646
$this->attrs[$name] = $value;
4747
}
4848

49-
public function __get(string $name): mixed
49+
public function &__get(string $name): mixed
5050
{
51-
return $this->dirty[$name] ?? $this->attrs[$name];
51+
if (isset($this->dirty[$name])) {
52+
return $this->dirty[$name];
53+
}
54+
55+
if (isset($this->relationships[$name])) {
56+
return $this->relationships[$name];
57+
}
58+
59+
$null = null;
60+
return $null;
5261
}
5362

5463
public function __isset(string $name): bool

src/User/Relationships.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Maicol07\SSO\User;
44

5-
use Maicol07\SSO\Flarum;
6-
75
/**
86
* Class Relationships
97
* @package Maicol07\SSO\User
@@ -23,9 +21,18 @@ public function __set(string $name, mixed $value): void
2321
$this->relationships[$name] = $value;
2422
}
2523

26-
public function __get(string $name): mixed
24+
public function &__get(string $name): mixed
2725
{
28-
return $this->dirty[$name] ?? $this->relationships[$name];
26+
if (isset($this->dirty[$name])) {
27+
return $this->dirty[$name];
28+
}
29+
30+
if (isset($this->relationships[$name])) {
31+
return $this->relationships[$name];
32+
}
33+
34+
$null = null;
35+
return $null;
2936
}
3037

3138
public function __isset(string $name): bool
@@ -38,7 +45,7 @@ public function toArray(): array
3845
return $this->relationships;
3946
}
4047

41-
public function dirtyToArray(Flarum $flarum): array
48+
public function dirtyToArray(): array
4249
{
4350
return $this->dirty;
4451
}

0 commit comments

Comments
 (0)