42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class MotorTokenModel extends Model
|
|
{
|
|
protected $table = 'motor_token';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'environment', 'access_token', 'refresh_token', 'expires_at', 'created_at',
|
|
];
|
|
protected $useTimestamps = false;
|
|
|
|
public function getByEnvironment(string $environment): ?array
|
|
{
|
|
return $this->where('environment', $environment)->first();
|
|
}
|
|
|
|
public function upsertToken(string $environment, string $accessToken, ?string $refreshToken, string $expiresAt): void
|
|
{
|
|
$existing = $this->getByEnvironment($environment);
|
|
$row = [
|
|
'environment' => $environment,
|
|
'access_token' => $accessToken,
|
|
'refresh_token' => $refreshToken,
|
|
'expires_at' => $expiresAt,
|
|
];
|
|
|
|
if ($existing) {
|
|
$this->update($existing['id'], $row);
|
|
} else {
|
|
$this->insert($row);
|
|
}
|
|
}
|
|
}
|