Skip to content

Commit 612f896

Browse files
committed
refactor: extract method
1 parent aa034b3 commit 612f896

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/Models/UserModel.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeIgniter\Model;
77
use CodeIgniter\Shield\Authentication\Authenticators\Session;
88
use CodeIgniter\Shield\Entities\User;
9+
use CodeIgniter\Shield\Entities\UserIdentity;
910
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
1011
use CodeIgniter\Shield\Exceptions\ValidationException;
1112
use Faker\Generator;
@@ -82,11 +83,26 @@ protected function fetchIdentities(array $data): array
8283
return $data;
8384
}
8485

85-
// Map our users by ID to make assigning simpler
86+
$mappedUsers = $this->assignIdentities($data, $identities);
87+
88+
$data['data'] = $data['singleton'] ? $mappedUsers[$data['id']] : $mappedUsers;
89+
90+
return $data;
91+
}
92+
93+
/**
94+
* Map our users by ID to make assigning simpler
95+
*
96+
* @param UserIdentity[] $identities
97+
*
98+
* @return User[] UserId => User object
99+
* @phpstan-return array<int|string, User> UserId => User object
100+
*/
101+
private function assignIdentities(array $data, array $identities): array
102+
{
86103
$mappedUsers = [];
87-
$users = $data['singleton']
88-
? [$data['data']]
89-
: $data['data'];
104+
105+
$users = $data['singleton'] ? [$data['data']] : $data['data'];
90106

91107
foreach ($users as $user) {
92108
$mappedUsers[$user->id] = $user;
@@ -95,14 +111,13 @@ protected function fetchIdentities(array $data): array
95111

96112
// Now assign the identities to the user
97113
foreach ($identities as $id) {
98-
$array = $mappedUsers[$id->user_id]->identities;
99-
$array[] = $id;
114+
$array = $mappedUsers[$id->user_id]->identities;
115+
$array[] = $id;
116+
100117
$mappedUsers[$id->user_id]->identities = $array;
101118
}
102119

103-
$data['data'] = $data['singleton'] ? $mappedUsers[$id->user_id] : $mappedUsers;
104-
105-
return $data;
120+
return $mappedUsers;
106121
}
107122

108123
/**

0 commit comments

Comments
 (0)