forked from fa/breadcrumb-the-shire
refactor(repository): deduplicate unwrap and id array helpers
This commit is contained in:
@@ -3,23 +3,15 @@
|
||||
namespace MintyPHP\Repository\Access;
|
||||
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\Repository\Support\RepositoryArrayHelper;
|
||||
|
||||
class UserRoleRepository implements UserRoleRepositoryInterface
|
||||
{
|
||||
public function listRoleIdsByUserId(int $userId): array
|
||||
{
|
||||
$rows = DB::select('select role_id from user_roles where user_id = ?', (string) $userId);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
$ids = [];
|
||||
foreach ($rows as $row) {
|
||||
$data = $row['user_roles'] ?? $row;
|
||||
if (is_array($data) && isset($data['role_id'])) {
|
||||
$ids[] = (int) $data['role_id'];
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
|
||||
return RepositoryArrayHelper::extractIds($rows, 'user_roles', 'role_id');
|
||||
}
|
||||
|
||||
public function replaceForUser(int $userId, array $roleIds): bool
|
||||
@@ -42,8 +34,7 @@ class UserRoleRepository implements UserRoleRepositoryInterface
|
||||
|
||||
public function listUserIdsByRoleIds(array $roleIds): array
|
||||
{
|
||||
$ids = array_values(array_unique(array_map('intval', $roleIds)));
|
||||
$ids = array_values(array_filter($ids, static fn ($id) => $id > 0));
|
||||
$ids = RepositoryArrayHelper::sanitizePositiveIds($roleIds);
|
||||
if (!$ids) {
|
||||
return [];
|
||||
}
|
||||
@@ -52,20 +43,10 @@ class UserRoleRepository implements UserRoleRepositoryInterface
|
||||
'select distinct user_id from user_roles where role_id in (???)',
|
||||
array_map('strval', $ids)
|
||||
);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$userIds = [];
|
||||
foreach ($rows as $row) {
|
||||
$data = $row['user_roles'] ?? $row;
|
||||
if (!is_array($data) || !isset($data['user_id'])) {
|
||||
continue;
|
||||
}
|
||||
$userIds[] = (int) $data['user_id'];
|
||||
}
|
||||
|
||||
return array_values(array_unique(array_filter($userIds, static fn ($id) => $id > 0)));
|
||||
return RepositoryArrayHelper::sanitizePositiveIds(
|
||||
RepositoryArrayHelper::extractIds($rows, 'user_roles', 'user_id')
|
||||
);
|
||||
}
|
||||
|
||||
public function countUsersByRoleIds(array $roleIds): array
|
||||
@@ -80,8 +61,7 @@ class UserRoleRepository implements UserRoleRepositoryInterface
|
||||
|
||||
private function countByRoleIds(array $roleIds, bool $activeOnly): array
|
||||
{
|
||||
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
|
||||
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
|
||||
$roleIds = RepositoryArrayHelper::sanitizePositiveIds($roleIds);
|
||||
if (!$roleIds) {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user