- 모든 서비스를 인스턴스구조로 변경예정 * 규모가 커질수록 → 인스턴스(=DI) 설계가 유리 * 작고 단순한 유틸/순수 함수만 스태틱으로 유지 * DI/모킹/테스트 쉬움 (서비스 교체·부분 테스트 가능) * 의존성 교체 쉬움 (Repo/캐시/로거…) * 상태·컨텍스트 주입 명확 (테넌트/유저/트랜잭션)
38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
abstract class Service
|
|
{
|
|
/** 활성 테넌트 ID(없으면 null) */
|
|
protected function tenantIdOrNull(): ?int
|
|
{
|
|
$id = app('tenant_id');
|
|
return $id ? (int) $id : null;
|
|
}
|
|
|
|
/** 활성 테넌트 ID(없으면 400 Bad Request + i18n 메시지로 예외) */
|
|
protected function tenantId(): int
|
|
{
|
|
$id = $this->tenantIdOrNull();
|
|
if (!$id) {
|
|
// ko/error.php 의 'tenant_id' 키 사용
|
|
throw new BadRequestHttpException(__('error.tenant_id'));
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
/** (선택) API 사용자 ID 필요할 때 401로 던지고 싶다면 */
|
|
protected function apiUserIdOrFail(): int
|
|
{
|
|
$uid = app('api_user');
|
|
if (!$uid) {
|
|
// Handler에서 AuthenticationException은 401로 처리 중
|
|
throw new AuthenticationException(__('auth.unauthenticated'));
|
|
}
|
|
return (int) $uid;
|
|
}
|
|
}
|