Automation/app/Libraries/MySocket/Web/GoogleLibrary.php
2024-09-15 04:59:22 +09:00

62 lines
1.8 KiB
PHP

<?php
namespace App\Libraries\MySocket\Web;
use App\Libraries\MySocket\WebLibrary as MySocketLibrary;
class GoogleLibrary extends MySocketLibrary
{
private $_client = null;
private $_session = null;
private $_access_code = "";
public function __construct(string $host)
{
parent::__construct($host);
$this->_session = \Config\Services::session();
}
//Override
public function getClient()
{
if (is_null($this->_client)) {
$this->_client = new \Google_Client();
$this->_client->setClientId(getenv("socket.google.client.id"));
$this->_client->setClientSecret(getenv("socket.google.client.key"));
$this->_client->setRedirectUri(base_url() . getenv("socket.google.client.callback_url"));
$this->_client->addScope('email');
$this->_client->addScope('profile');
}
return $this->_client;
}
public function getAccessCode(): string
{
if ($this->_access_code === "") {
throw new \Exception("access_code가 지정되지 않았습니다.");
}
return $this->_access_code;
}
public function setAccessCode(string $access_code)
{
$this->_access_code = $access_code;
}
public function setAccessToken()
{
//2.토큰정보 가져오기
$tokenInfo = $this->getClient()->fetchAccessTokenWithAuthCode($this->getAccessCode());
if (isset($tokenInfo['error'])) {
throw new \Exception($tokenInfo['error']);
}
$token = $tokenInfo[getenv("socket.google.client.token_name")];
//3. Google Service에 접근하기위해 Access Token을 설정
$this->getClient()->setAccessToken($token);
//4. Google에 로그인이 했으므로 세션에 Token값 설정
$this->_session->set(getenv("socket.google.client.token_name"), $token);
}
public function getAccessToken(): ?string
{
return $this->_session->get(getenv("socket.google.client.token_name"));
}
}