forked from fa/breadcrumb-the-shire
refactor(bookmarks): remove factory chain, interfaces, and duplicated sort logic
Remove BookmarkRepositoryFactory, BookmarkServicesFactory, and all 3 repository interfaces — single-consumer abstractions with no polymorphism. Simplify container registrar to wire directly. Extract duplicated updateSortOrders() (60 identical lines in two repos) into SortOrderTrait. Consolidate icon labels from template into BookmarkService::allowedGroupIconLabels() single source of truth. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,8 +6,15 @@ use MintyPHP\DB;
|
||||
use MintyPHP\Repository\Support\RepoQuery;
|
||||
use MintyPHP\Repository\Support\RepositoryArrayHelper;
|
||||
|
||||
class BookmarkRepository implements BookmarkRepositoryInterface
|
||||
class BookmarkRepository
|
||||
{
|
||||
use SortOrderTrait;
|
||||
|
||||
protected function sortOrderTableName(): string
|
||||
{
|
||||
return 'user_bookmarks';
|
||||
}
|
||||
|
||||
private function unwrapList(mixed $rows): array
|
||||
{
|
||||
return RepositoryArrayHelper::unwrapList($rows, 'user_bookmarks');
|
||||
@@ -182,68 +189,6 @@ class BookmarkRepository implements BookmarkRepositoryInterface
|
||||
return $deleted !== false && (int) $deleted > 0;
|
||||
}
|
||||
|
||||
public function updateSortOrders(int $userId, array $idOrderPairs): bool
|
||||
{
|
||||
if ($userId <= 0 || $idOrderPairs === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$idList = [];
|
||||
foreach ($idOrderPairs as $pair) {
|
||||
$id = (int) $pair['id'];
|
||||
if ($id <= 0) {
|
||||
return false;
|
||||
}
|
||||
$idList[] = $id;
|
||||
}
|
||||
$idList = array_values(array_unique($idList));
|
||||
|
||||
$existingCount = DB::selectValue(
|
||||
'select count(*) from user_bookmarks where user_id = ? and id in (???)',
|
||||
(string) $userId,
|
||||
array_map('strval', $idList)
|
||||
);
|
||||
|
||||
if ((int) $existingCount !== count($idList)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$db = DB::handle();
|
||||
try {
|
||||
$db->begin_transaction();
|
||||
|
||||
foreach ($idOrderPairs as $pair) {
|
||||
$id = (int) $pair['id'];
|
||||
$sortOrder = (int) $pair['sort_order'];
|
||||
if ($id <= 0 || $sortOrder <= 0) {
|
||||
$db->rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
$updated = DB::update(
|
||||
'update user_bookmarks set sort_order = ? where id = ? and user_id = ?',
|
||||
(string) $sortOrder,
|
||||
(string) $id,
|
||||
(string) $userId
|
||||
);
|
||||
if ($updated === false) {
|
||||
$db->rollback();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$db->commit();
|
||||
return true;
|
||||
} catch (\Throwable) {
|
||||
try {
|
||||
$db->rollback();
|
||||
} catch (\Throwable) {
|
||||
// no-op
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
public function listByGroup(int $userId, int $groupId): array
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user