Files
breadcrumb-the-shire/core/Repository/CustomField/TenantCustomFieldOptionRepository.php
fs 6b05bbfe9c refactor(customfield): interface-based DI for repositories
Align CustomField-Schicht mit core/Repository/Access-Muster. Vier
Repositories (Tenant-Definition, Tenant-Option, User-Value, User-Value-Option)
bekommen je ein Interface, werden von static auf instance umgestellt und
ueber CustomFieldRepositoryFactory injiziert.

TenantCustomFieldService und UserCustomFieldValueService injizieren die
Repository-Interfaces statt statisch aufzurufen. CustomFieldServicesFactory
reicht Instanzen durch. AddressBookService (Modul) bekommt
TenantCustomFieldOptionRepositoryInterface per Container — Interface-Kopplung
verbessert Modul-Isolation gegenueber dem vorherigen statischen Aufruf.

Kein Verhaltenswechsel, keine SQL-Aenderung, keine Service-Signatur-Aenderung.
Alle tenant_id-Parameter erhalten (GR-SEC-009, Security-Review SR-002).

Oeffnet den Weg fuer Cluster B1 (TenantCustomFieldService-Tests) und B2
(UserCustomFieldValueService-Tests), die nun mit createMock() moeglich sind.

Nebenbei zwei PHPStan-Folge-Findings korrigiert:
- Veralteter Ignore-Eintrag fuer findById aus phpstan-baseline.neon entfernt
- Redundanter is_array-Check in TenantCustomFieldOptionRepository entfernt
  (durch typisierte Interface-Signatur abgedeckt)

Gates: QG-001 (1945 Tests, 28581 Assertions) / QG-002 / QG-003 / QG-006 pass.

Workflow: .agents/runs/CUSTOMFIELD-DI-REFACTOR-001/

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 19:32:14 +02:00

126 lines
4.3 KiB
PHP

<?php
namespace MintyPHP\Repository\CustomField;
use MintyPHP\DB;
/** Retrieves selectable options for tenant-scoped custom field definitions. */
class TenantCustomFieldOptionRepository implements TenantCustomFieldOptionRepositoryInterface
{
private static function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$item = $row['tenant_custom_field_options'] ?? null;
if (is_array($item)) {
$list[] = $item;
}
}
return $list;
}
public function listByDefinitionIds(array $definitionIds, bool $onlyActive = true): array
{
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
if (!$definitionIds) {
return [];
}
$query = 'select id, definition_id, option_key, label, active, sort_order, created, modified ' .
'from tenant_custom_field_options where definition_id in (???)';
$params = [$definitionIds];
if ($onlyActive) {
$query .= ' and active = 1';
}
$query .= ' order by definition_id asc, sort_order asc, label asc, id asc';
return self::unwrapList(call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $params)));
}
public function replaceForDefinition(int $definitionId, array $options): bool
{
if ($definitionId <= 0) {
return false;
}
$existing = $this->listByDefinitionIds([$definitionId], false);
$existingByKey = [];
foreach ($existing as $row) {
$key = (string) ($row['option_key'] ?? '');
if ($key !== '') {
$existingByKey[$key] = $row;
}
}
$keepIds = [];
foreach ($options as $option) {
$optionKey = trim((string) ($option['option_key'] ?? ''));
$label = trim((string) ($option['label'] ?? ''));
if ($optionKey === '' || $label === '') {
continue;
}
$active = !empty($option['active']) ? 1 : 0;
$sortOrder = (int) ($option['sort_order'] ?? 100);
if (isset($existingByKey[$optionKey]['id'])) {
$optionId = (int) $existingByKey[$optionKey]['id'];
$keepIds[] = $optionId;
$updated = DB::update(
'update tenant_custom_field_options set label = ?, active = ?, sort_order = ? where id = ?',
$label,
(string) $active,
(string) $sortOrder,
(string) $optionId
);
if ($updated === false) {
return false;
}
continue;
}
$insertId = DB::insert(
'insert into tenant_custom_field_options (definition_id, option_key, label, active, sort_order, created) values (?,?,?,?,?,NOW())',
(string) $definitionId,
$optionKey,
$label,
(string) $active,
(string) $sortOrder
);
if ($insertId === false) {
return false;
}
if ($insertId) {
$keepIds[] = (int) $insertId;
}
}
if (!$keepIds) {
$deleted = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
if ($deleted === false) {
return false;
}
return true;
}
$deleted = DB::delete(
'delete from tenant_custom_field_options where definition_id = ? and id not in (???)',
(string) $definitionId,
$keepIds
);
if ($deleted === false) {
return false;
}
return true;
}
public function deleteByDefinitionId(int $definitionId): bool
{
if ($definitionId <= 0) {
return false;
}
$result = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
return $result !== false;
}
}