dbms/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php
2025-04-29 16:56:58 +09:00

81 lines
2.4 KiB
PHP

<?php
namespace App\Libraries\MySocket\GoogleSocket;
use App\Entities\UserSNSEntity as Entity;
use App\Libraries\MySocket\MySocket;
use App\Models\UserSNSModel as Model;
use App\Services\UserSNSService as Service;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Session\Session;
use Config\Services;
abstract class GoogleSocket extends MySocket
{
private string $_site = "GOOGLE";
private ?Service $_service = null;
protected $_client = null;
private ?Session $_session = null;
protected string $_access_token = "";
protected string $_token_name = "access_token";
public function __construct() {}
abstract public function createAuthUrl(): string;
abstract public function setToken(string $access_code): void;
abstract public function getEntity(): Entity;
final public function getSession(): Session
{
if ($this->_session == null) {
$this->_session = Services::session();
}
return $this->_session;
}
final public function getToken(): string
{
return $this->getSession()->get($this->_token_name);
}
final public function getSite(): string
{
return $this->_site;
}
public function getService(): Service
{
if ($this->_service === null) {
$this->_service = new Service();
}
return $this->_service;
}
final protected function setEntity(string $id, string $name, string $email, string $detail): Entity
{
$this->getService()->getModel()->where(Model::SITE, $this->getSite());
$this->getService()->getModel()->where('id', $id);
$entity = $this->getEntity();
if ($entity === null) {
//Transaction Start
$this->getService()->getModel()->transStart();
try {
//없다면 새로 등록
$formDatas = [
'site' => $this->getSite(),
'id' => $id,
'name' => $name,
'email' => $email,
'detail' => $detail,
'status' => 'unuse',
];
$entity = $this->getService()->getModel()->create($formDatas);
$this->getService()->getModel()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getService()->getModel()->transRollback();
log_message("error", $e->getMessage());
throw new \Exception(__FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
}
}
//상태가 use(승인완료)가 아니라면
if ($entity->status !== DEFAULTS['STATUS']) {
throw new PageNotFoundException("{$entity->getSite()}{$entity->getEmail()}:{$entity->getTitle()}님은 {$entity->status}입니다 ");
}
return $entity;
}
}