1
0
Files
breadcrumb-the-shire/core/Repository/CustomField/UserCustomFieldValueRepository.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

122 lines
4.6 KiB
PHP

<?php
namespace MintyPHP\Repository\CustomField;
use MintyPHP\DB;
/** Reads and writes custom field values attached to individual users. */
class UserCustomFieldValueRepository implements UserCustomFieldValueRepositoryInterface
{
private static function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$item = $row['user_custom_field_values'] ?? null;
if (is_array($item)) {
$list[] = $item;
}
}
return $list;
}
public function listByUserAndDefinitionIds(int $userId, array $definitionIds): array
{
if ($userId <= 0) {
return [];
}
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
if (!$definitionIds) {
return [];
}
$rows = DB::select(
'select id, user_id, definition_id, value_text, value_bool, value_date, option_id, created, modified ' .
'from user_custom_field_values where user_id = ? and definition_id in (???)',
(string) $userId,
$definitionIds
);
return self::unwrapList($rows);
}
public function upsertScalarValue(int $userId, int $definitionId, array $typedValue): int|false
{
if ($userId <= 0 || $definitionId <= 0) {
return false;
}
$existingId = DB::selectValue(
'select id from user_custom_field_values where user_id = ? and definition_id = ? limit 1',
(string) $userId,
(string) $definitionId
);
$valueText = array_key_exists('value_text', $typedValue) ? $typedValue['value_text'] : null;
$valueBool = array_key_exists('value_bool', $typedValue) ? $typedValue['value_bool'] : null;
$valueDate = array_key_exists('value_date', $typedValue) ? $typedValue['value_date'] : null;
$optionId = array_key_exists('option_id', $typedValue) ? $typedValue['option_id'] : null;
if ($existingId) {
$updated = DB::update(
'update user_custom_field_values set value_text = ?, value_bool = ?, value_date = ?, option_id = ? where id = ?',
$valueText,
$valueBool !== null ? (string) ((int) $valueBool) : null,
$valueDate,
$optionId !== null ? (string) ((int) $optionId) : null,
(string) ((int) $existingId)
);
return $updated !== false ? (int) $existingId : false;
}
return DB::insert(
'insert into user_custom_field_values (user_id, definition_id, value_text, value_bool, value_date, option_id, created) values (?,?,?,?,?,?,NOW())',
(string) $userId,
(string) $definitionId,
$valueText,
$valueBool !== null ? (string) ((int) $valueBool) : null,
$valueDate,
$optionId !== null ? (string) ((int) $optionId) : null
);
}
public function deleteByUserAndDefinitionIds(int $userId, array $definitionIds): bool
{
if ($userId <= 0) {
return false;
}
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
if (!$definitionIds) {
return true;
}
$result = DB::delete(
'delete from user_custom_field_values where user_id = ? and definition_id in (???)',
(string) $userId,
$definitionIds
);
return $result !== false;
}
public function deleteByUserOutsideTenantIds(int $userId, array $tenantIds): bool
{
if ($userId <= 0) {
return false;
}
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
if (!$tenantIds) {
$result = DB::delete('delete from user_custom_field_values where user_id = ?', (string) $userId);
return $result !== false;
}
$result = DB::delete(
'delete ucfv from user_custom_field_values ucfv ' .
'join tenant_custom_field_definitions d on d.id = ucfv.definition_id ' .
'where ucfv.user_id = ? and d.tenant_id not in (???)',
(string) $userId,
$tenantIds
);
return $result !== false;
}
}