88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MySocket;
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
use App\Models\Cloudflare\AuthModel;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use App\Libraries\CommonLibrary;
|
|
use App\Entities\Cloudflare\AuthEntity;
|
|
|
|
abstract class CloudflareSocket extends CommonLibrary
|
|
{
|
|
private static int $_request = 0;
|
|
private static int $_request_max = 100;
|
|
private static int $_request_timewait = 60;
|
|
private $_authModel = null;
|
|
private $_accountModel = null;
|
|
private $_client = null;
|
|
private $_auth_entity = null;
|
|
protected function __construct(AuthEntity $auth_entity)
|
|
{
|
|
parent::__construct();
|
|
$this->_auth_entity = $auth_entity;
|
|
self::$_request_max = getenv("cfmgr.request.max");
|
|
}
|
|
abstract protected function getArrayByResult($result, array $formDatas = []): array;
|
|
final protected function getAuthModel(): AuthModel
|
|
{
|
|
if ($this->_authModel === null) {
|
|
$this->_authModel = new AuthModel();
|
|
}
|
|
return $this->_authModel;
|
|
}
|
|
final protected function getAccountModel(): AccountModel
|
|
{
|
|
if ($this->_accountModel === null) {
|
|
$this->_accountModel = new AccountModel();
|
|
}
|
|
return $this->_accountModel;
|
|
}
|
|
final protected function getClient(): Client
|
|
{
|
|
if ($this->_client === null) {
|
|
// Guzzle HTTP 클라이언트를 설정하면서 Cloudflare API 토큰 사용
|
|
$this->_client = new Client([
|
|
'base_uri' => 'https://api.cloudflare.com/client/v4/',
|
|
'headers' => [
|
|
'X-Auth-Email' => $this->_auth_entity->getID(), // 인증 토큰 사용
|
|
'X-Auth-Key' => $this->_auth_entity->getAuthKey(), // 인증 토큰 사용
|
|
'Content-Type' => 'application/json',
|
|
]
|
|
]);
|
|
}
|
|
if (self::$_request >= self::$_request_max) {
|
|
log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait));
|
|
sleep(intval(getenv("cf.mgr.request.time.wait")));
|
|
self::$_request = 0;
|
|
log_message('warning', sprintf("--Cloudflare API Call %s초 대기 종료--", self::$_request_timewait));
|
|
}
|
|
self::$_request++;
|
|
return $this->_client;
|
|
}
|
|
|
|
final protected function reload_procedure($uri): array
|
|
{
|
|
$page = 1; //Page는 1부터 시작해야함
|
|
$perpage_max = getenv("cfmgr.request.perpage.max");
|
|
$results = [];
|
|
do {
|
|
$query = [
|
|
'page' => $page,
|
|
'per_page' => $perpage_max,
|
|
'match' => 'all',
|
|
];
|
|
$cf = $this->getClient()->get($uri, $query);
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success) {
|
|
throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
|
|
}
|
|
$results[] = $cf->result;
|
|
//Loop 제한 : 한페이지에서 가져온 갯수가 perpage_max보다 적다는것은 더이상 다음페이지기 없으므로 0로 종료시키기 위함
|
|
$page = count($cf->result) < $perpage_max ? 0 : $page + 1;
|
|
} while (0 < $page);
|
|
return $results;
|
|
}
|
|
}
|