refactor(repository): deduplicate unwrap and id array helpers

This commit is contained in:
2026-03-13 13:12:51 +01:00
parent efa031ea5f
commit d28f85ac9e
13 changed files with 465 additions and 240 deletions

View File

@@ -5,60 +5,28 @@ namespace MintyPHP\Repository\Tenant;
use MintyPHP\DB;
use MintyPHP\Domain\Taxonomy\TenantStatus;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class TenantRepository implements TenantRepositoryInterface
{
private function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['tenants'] ?? null;
}
private function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$tenant = $row['tenants'] ?? null;
if (is_array($tenant)) {
$list[] = $tenant;
}
}
return $list;
}
public function list(): array
{
$rows = DB::select(
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants order by id desc'
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'tenants');
}
public function listIds(): array
{
$rows = DB::select('select id from tenants');
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['tenants'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'tenants');
}
public function listByIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -68,12 +36,12 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where id in (' . $placeholders . ')',
...array_map('strval', $tenantIds)
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'tenants');
}
public function listActiveIdsByIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -86,17 +54,7 @@ class TenantRepository implements TenantRepositoryInterface
$params
)
);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['tenants'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'tenants');
}
public function listPaged(array $options): array
@@ -146,7 +104,7 @@ class TenantRepository implements TenantRepositoryInterface
return [
'total' => $total,
'rows' => $this->unwrapList($rows),
'rows' => RepositoryArrayHelper::unwrapList($rows, 'tenants'),
];
}
@@ -156,7 +114,7 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where id = ? limit 1',
(string) $id
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'tenants');
}
public function findByUuid(string $uuid): ?array
@@ -165,7 +123,7 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where uuid = ? limit 1',
$uuid
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'tenants');
}
public function create(array $data): mixed