cfmgrv4/app/Libraries/MySocket/CloudflareSocket.php
2024-09-24 19:18:05 +09:00

70 lines
2.1 KiB
PHP

<?php
namespace App\Libraries\MySocket;
use Cloudflare\API\Auth\APIKey;
use Cloudflare\API\Adapter\Guzzle;
use App\Models\Cloudflare\AccountModel;
use App\Libraries\CommonLibrary;
// use App\Entities\Cloudflare\AccountEntity;
// use Cloudflare\API\Endpoints\Zones;
// use Cloudflare\API\Endpoints\DNS;
// use Cloudflare\API\Endpoints\Accounts;
class CloudflareSocket extends CommonLibrary
{
private static int $_request = 0;
private static int $_request_max = 100;
private static int $_request_timewait = 60;
private $_accountModel = null;
private $_clients = [];
public function __construct()
{
parent::__construct();
$this->initAdapters();
self::$_request_max = getenv("cfmgr.request.max");
}
private function getAccountModel(): AccountModel
{
if ($this->_accountModel === null) {
$this->_accountModel = new AccountModel();
}
return $this->_accountModel;
}
private function initAdapters(): void
{
foreach ($this->getAccountModel()->getEntitys() as $entity) {
$this->_clients[$entity->getAuthKey()] = new Guzzle(
new APIKey($entity->getTitle(), $entity->getAuthKey())
);
}
}
final public function request(string $authkey): Guzzle
{
if (!array_key_exists($authkey, $this->_clients)) {
throw new \Exception(+__FUNCTION__ . " => {$authkey}에 해당하는 Adapter를 찾을수 없습니다.");
}
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->_clients[$authkey];
}
// public function getAccount(string $authkey): Accounts
// {
// return new Accounts($this->request($authkey));
// }
// public function getZone(string $authkey): Zones
// {
// return new Zones($this->request($authkey));
// }
// public function getRecord(string $authkey): DNS
// {
// return new DNS($this->request($authkey));
// }
}