trafficmonitor/app/Services/Auth/GoogleService.php
2025-11-06 16:51:52 +09:00

78 lines
2.6 KiB
PHP

<?php
namespace App\Services\Auth;
use App\DTOs\Auth\GoogleDTO;
use App\DTOs\AuthDTO;
use App\Entities\UserEntity;
use App\Forms\Auth\GoogleForm;
use App\Libraries\MySocket\GoogleSocket\CURL;
use App\Models\UserModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Validation\Exceptions\ValidationException;
use RuntimeException;
class GoogleService extends AuthService
{
public function __construct(UserModel $model, private CURL $socket)
{
parent::__construct($model);
$this->addClassPaths('Google');
}
public function getFormService(): GoogleForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new GoogleForm();
$this->formServiceInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
}
public function getFormFields(): array
{
return ["access_code"];
}
public function getFormFilters(): array
{
return ["access_code"];
}
public function getBatchjobButtons(): array
{
return [];
}
//기본기능
public function login(mixed $dto): UserEntity
{
try {
if (!$dto instanceof GoogleDTO) {
throw new RuntimeException(__METHOD__ . "에서 오류발생: GoogleDTO만 사용하실수 있습니다");
}
// DTO 객체를 배열로 변환하여 검증기에 전달
$formDatas = (array) $dto;
if (!service('validation')->setRules($this->getFormRules(__FUNCTION__))->run($formDatas)) {
// 검증 실패 시, ValidationException을 던집니다.
throw new ValidationException(service('validation')->getErrors());
}
$this->socket->setToken($dto->access_code);
$sns_entity = $this->socket->signup();
// local db 사용와의 연결 확인
$entity = $this->getEntity($sns_entity->getParent());
if (!$entity instanceof UserEntity) {
throw new PageNotFoundException("회원[{$sns_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다.");
}
return $this->login_process($entity);;
} catch (\Google_Service_Exception $e) {
log_message('error', '구글 서비스 예외발생: ' . $e->getMessage());
throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요.");
} catch (\Exception $e) {
log_message('error', $e->getMessage());
throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.\n{$e->getMessage()}");
}
}
}