Files
sam-manage/app/Models/PushDeviceToken.php

209 lines
5.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Models\Tenants\Tenant;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class PushDeviceToken extends Model
{
use BelongsToTenant;
use SoftDeletes;
protected $fillable = [
'tenant_id',
'user_id',
'token',
'platform',
'device_name',
'app_version',
'is_active',
'last_used_at',
'last_error',
'last_error_at',
];
protected $casts = [
'is_active' => 'boolean',
'last_used_at' => 'datetime',
'last_error_at' => 'datetime',
];
/**
* User-Agent에서 파싱된 기기명
*/
public function getParsedDeviceNameAttribute(): string
{
if (empty($this->device_name)) {
return '-';
}
// User-Agent 문자열인 경우 파싱
if (str_contains($this->device_name, 'Mozilla/') || str_contains($this->device_name, 'AppleWebKit')) {
return $this->parseUserAgent($this->device_name);
}
// 이미 간단한 기기명인 경우 그대로 반환
return $this->device_name;
}
/**
* User-Agent에서 파싱된 OS 버전
*/
public function getParsedOsVersionAttribute(): ?string
{
if (empty($this->device_name)) {
return null;
}
// Android 버전 추출
if (preg_match('/Android\s+([\d.]+)/', $this->device_name, $matches)) {
return 'Android ' . $matches[1];
}
// iOS 버전 추출
if (preg_match('/iPhone\s+OS\s+([\d_]+)/', $this->device_name, $matches)) {
return 'iOS ' . str_replace('_', '.', $matches[1]);
}
// iPad 버전 추출
if (preg_match('/CPU\s+OS\s+([\d_]+)/', $this->device_name, $matches)) {
return 'iOS ' . str_replace('_', '.', $matches[1]);
}
return null;
}
/**
* User-Agent 문자열에서 기기명 추출
*/
private function parseUserAgent(string $userAgent): string
{
// Android 기기명 추출: (Linux; Android 10; SM-N960N Build/...)
if (preg_match('/;\s*([A-Za-z0-9\-_]+(?:\s+[A-Za-z0-9\-_]+)*)\s+Build\//', $userAgent, $matches)) {
return trim($matches[1]);
}
// Android 기기명 대체 패턴: Android X; MODEL)
if (preg_match('/Android\s+[\d.]+;\s*([^)]+)\)/', $userAgent, $matches)) {
$model = trim($matches[1]);
// Build/ 이전까지만 추출
if (($pos = strpos($model, ' Build')) !== false) {
$model = substr($model, 0, $pos);
}
return $model ?: 'Android Device';
}
// iPhone 추출
if (str_contains($userAgent, 'iPhone')) {
return 'iPhone';
}
// iPad 추출
if (str_contains($userAgent, 'iPad')) {
return 'iPad';
}
// 기타 - 너무 긴 경우 축약
if (strlen($userAgent) > 30) {
return 'Unknown Device';
}
return $userAgent;
}
/**
* 플랫폼 상수
*/
public const PLATFORM_IOS = 'ios';
public const PLATFORM_ANDROID = 'android';
public const PLATFORM_WEB = 'web';
/**
* 사용자 관계
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* 테넌트 관계
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class, 'tenant_id');
}
/**
* Scope: 활성 토큰만
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Scope: 플랫폼별 필터
*/
public function scopePlatform($query, string $platform)
{
return $query->where('platform', $platform);
}
/**
* Scope: 특정 사용자의 토큰
*/
public function scopeForUser($query, int $userId)
{
return $query->where('user_id', $userId);
}
/**
* Scope: 특정 테넌트의 토큰 (global scope 무시)
*/
public function scopeForTenant($query, int $tenantId)
{
return $query->where('tenant_id', $tenantId);
}
/**
* Scope: 에러가 있는 토큰
*/
public function scopeHasError($query)
{
return $query->whereNotNull('last_error');
}
/**
* 에러 정보 기록
*/
public function recordError(string $errorCode): void
{
$this->update([
'last_error' => $errorCode,
'last_error_at' => now(),
]);
}
/**
* 토큰 비활성화 (에러와 함께)
*/
public function deactivate(?string $errorCode = null): void
{
$data = ['is_active' => false];
if ($errorCode) {
$data['last_error'] = $errorCode;
$data['last_error_at'] = now();
}
$this->update($data);
}
}