giftcon_dev/app/Repositories/Member/MemberAuthRepository.php
2026-01-19 10:19:09 +09:00

111 lines
3.3 KiB
PHP

<?php
namespace App\Repositories\Member;
use App\Models\Member\MemAuth;
use App\Models\Member\MemAuthInfo;
use App\Models\Member\MemAuthLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
class MemberAuthRepository
{
/**
* mem_auth (mem_no, auth_type) 업서트
* - auth_date는 date 컬럼: Y-m-d
*/
public function upsertState(
int $memNo,
string $authType,
string $authState,
?string $authDate = null
): void {
$authDate = $authDate ?: Carbon::now()->toDateString();
DB::table('mem_auth')->updateOrInsert(
['mem_no' => $memNo, 'auth_type' => $authType],
['auth_state' => $authState, 'auth_date' => $authDate]
);
}
public function markRequested(int $memNo, string $authType, array $logInfo = []): void
{
$this->setStateWithLog($memNo, $authType, MemAuth::STATE_R, MemAuthLog::STATE_P, $logInfo);
}
public function markProcessing(int $memNo, string $authType, array $logInfo = []): void
{
$this->setStateWithLog($memNo, $authType, MemAuth::STATE_P, MemAuthLog::STATE_P, $logInfo);
}
public function markSuccess(int $memNo, string $authType, array $logInfo = []): void
{
$this->setStateWithLog($memNo, $authType, MemAuth::STATE_Y, MemAuthLog::STATE_S, $logInfo);
}
public function markFail(int $memNo, string $authType, array $logInfo = []): void
{
$this->setStateWithLog($memNo, $authType, MemAuth::STATE_N, MemAuthLog::STATE_F, $logInfo);
}
/**
* mem_auth_info.auth_info JSON에 타입별로 병합 저장
* - 예: ["email" => [...], "cell" => [...]]
*/
public function mergeAuthInfo(int $memNo, string $authType, array $payload): void
{
DB::transaction(function () use ($memNo, $authType, $payload) {
/** @var MemAuthInfo $row */
$row = MemAuthInfo::query()->find($memNo);
if (!$row) {
$row = new MemAuthInfo();
$row->mem_no = $memNo;
$row->auth_info = [];
}
$data = $row->auth_info ?: [];
$data[$authType] = array_merge($data[$authType] ?? [], $payload);
$row->auth_info = $data;
$row->save();
});
}
/**
* mem_auth 상태 변경 + mem_auth_log 기록을 한 트랜잭션으로
*/
private function setStateWithLog(
int $memNo,
string $authType,
string $authState,
string $logState,
array $logInfo
): void {
DB::transaction(function () use ($memNo, $authType, $authState, $logState, $logInfo) {
$this->upsertState($memNo, $authType, $authState);
MemAuthLog::query()->create([
'mem_no' => $memNo,
'type' => $authType,
'state' => $logState,
'info' => $logInfo,
'rgdate' => Carbon::now()->toDateTimeString(),
]);
});
}
public function getState(int $memNo, string $authType): ?string
{
return DB::table('mem_auth')
->where('mem_no', $memNo)
->where('auth_type', $authType)
->value('auth_state');
}
public function isVerified(int $memNo, string $authType): bool
{
return $this->getState($memNo, $authType) === MemAuth::STATE_Y;
}
}