Files
Gnuboard7/app/Repositories/NotificationRepository.php
T
HeuJung b10e89bef0 fix(core,board,ecommerce,page,gdpr,ckeditor5,kginicis,admin_basic,basic): 목록 조회·검증 계층 정비 및 감사 지적 전건 처리
목록 조회 성능 축(깊은 OFFSET·정렬·색인)과 그 검증 계층에서 계획서 전수검수와 3회에 걸친
감사가 지적한 항목을 처리했다. 세부 경위는 의 각 회차 문서에 있다.

검증 계층 — 컨트롤러가 base Request 를 직접 주입받던 확장 13곳을 전용 FormRequest 로 옮겼다.
옮기면서 기존 동작 계약은 그대로 두었다: 상한 초과 limit 을 거부하지 않고 상한까지 반환하던
공개 API, per_page 를 범위로 조정하던 관리자 목록, 미지원 period 를 year 로 해석하던 인기글
목록 모두 종전과 같은 응답을 낸다. 상한/폴백을 rules 로 승격시키면 200 이던 응답이 422 가
되어 기존 링크가 깨지므로, 규칙은 타입만 닫고 클램프·폴백은 접근자가 맡는다. period 는
접근자가 닫힌 집합만 반환해 캐시 키 공간도 함께 닫힌다.

ckeditor5 이미지 업로드만 ResponseHelper 봉투를 쓰지 않는다. 응답을 파싱하는 주체가 CDN 으로
로드되는 상위 CKEditor5 43.3.1 의 SimpleUploadAdapter 라 규약을 바꿀 수 없어, 각 응답 지점에
사유를 명시한 면제를 부착하고 근거를 API 문서에 남겼다.

하네스 — 룰 5개의 대상 경로 패턴이 매처와 맞지 않아 번들 확장 컨트롤러가 검사 대상에서
통째로 빠져 있었다. 패턴을 고치자 확장 위반 17건이 드러나 전건 처리했다. severity 오타가
요약 집계 양쪽에 안 잡혀 "0 error" 로 보고되던 문제도 런너 사전 검증으로 막았다.

정렬 게이트 대조 하네스는 관계 정렬 변형만 쓰는 저장소를 탐지하지 못한 채 통과시키고 있었다.
탐지·제외·인자 파싱을 함께 고치고 단위 테스트를 신설했다.
2026-08-02 15:29:29 +09:00

157 lines
5.0 KiB
PHP

<?php
namespace App\Repositories;
use App\Contracts\Repositories\NotificationRepositoryInterface;
use App\Models\User;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Pagination\LengthAwarePaginator;
class NotificationRepository implements NotificationRepositoryInterface
{
/**
* 사용자의 알림 목록을 페이지네이션으로 조회합니다.
*
* @param User $user 대상 사용자
* @param array $filters 필터 조건 (read: unread|read)
* @param int $perPage 페이지당 항목 수
* @return LengthAwarePaginator 페이지네이션된 알림 목록
*/
public function getByUser(User $user, array $filters = [], int $perPage = 20): LengthAwarePaginator
{
$query = $user->notifications();
if (isset($filters['read']) && $filters['read'] === 'unread') {
$query->whereNull('read_at');
} elseif (isset($filters['read']) && $filters['read'] === 'read') {
$query->whereNotNull('read_at');
}
// audit:allow repository-paginate-column-pruning reason: 사용자 1명에 종속된 알림함 —
// $user->notifications() 로 이미 좁혀지고, 목록이 표시하는 본문 자체가 data(JSON) 라
// 걷어낼 넓은 컬럼이 없다
// 정렬 마지막의 기본키는 전순서 보장용이다 — created_at 동률에서 페이지 경계가
// 흔들려 인접 페이지가 같은 알림을 중복 노출하고 다른 알림을 누락하는 것을 막는다.
return $query->orderBy('created_at', 'desc')->orderBy('id', 'desc')->paginate($perPage);
}
/**
* 사용자의 미읽음 알림 수를 반환합니다.
*
* @param User $user 대상 사용자
* @return int 미읽음 알림 수
*/
public function getUnreadCount(User $user): int
{
return $user->unreadNotifications()->count();
}
/**
* 특정 알림을 읽음 처리합니다.
*
* @param User $user 대상 사용자
* @param string $notificationId 알림 ID
* @return DatabaseNotification|null 읽음 처리된 알림 (없으면 null)
*/
public function markAsRead(User $user, string $notificationId): ?DatabaseNotification
{
$notification = $user->notifications()->find($notificationId);
if (! $notification) {
return null;
}
$notification->markAsRead();
return $notification;
}
/**
* 지정된 알림들을 일괄 읽음 처리합니다.
*
* @param User $user 대상 사용자
* @param array $ids 알림 ID 배열
* @return int 읽음 처리된 알림 수
*/
public function markBatchAsRead(User $user, array $ids): int
{
return $user->unreadNotifications()
->whereIn('id', $ids)
->update(['read_at' => now()]);
}
/**
* 사용자의 모든 미읽음 알림을 읽음 처리합니다.
*
* @param User $user 대상 사용자
* @return int 읽음 처리된 알림 수
*/
public function markAllAsRead(User $user): int
{
$count = $user->unreadNotifications()->count();
$user->unreadNotifications->markAsRead();
return $count;
}
/**
* 알림을 삭제합니다.
*
* @param User $user 대상 사용자
* @param string $notificationId 알림 ID
* @return bool 삭제 성공 여부 (알림이 없으면 false)
*/
public function delete(User $user, string $notificationId): bool
{
$notification = $user->notifications()->find($notificationId);
if (! $notification) {
return false;
}
return (bool) $notification->delete();
}
/**
* 사용자의 모든 알림을 삭제합니다.
*
* @param User $user
* @return int 삭제된 알림 수
*/
public function deleteAll(User $user): int
{
return $user->notifications()->delete();
}
/**
* 오래된 알림을 정리합니다.
*
* @param int $readRetentionDays 읽은 알림 보관 일수 (0 이하면 정리하지 않음)
* @param int $unreadRetentionDays 미읽음 알림 보관 일수 (0 이하면 정리하지 않음)
* @return array{deleted_read: int, deleted_unread: int}
*/
public function cleanup(int $readRetentionDays, int $unreadRetentionDays): array
{
$deletedRead = 0;
$deletedUnread = 0;
if ($readRetentionDays > 0) {
$deletedRead = DatabaseNotification::whereNotNull('read_at')
->where('read_at', '<', now()->subDays($readRetentionDays))
->delete();
}
if ($unreadRetentionDays > 0) {
$deletedUnread = DatabaseNotification::whereNull('read_at')
->where('created_at', '<', now()->subDays($unreadRetentionDays))
->delete();
}
return [
'deleted_read' => $deletedRead,
'deleted_unread' => $deletedUnread,
];
}
}