Automation/app/Libraries/MySocket/CloudflareSocket.php
2024-09-20 19:51:36 +09:00

57 lines
1.6 KiB
PHP

<?php
namespace App\Libraries\MySocket;
use App\Libraries\CommonLibrary;
use App\Models\Cloudflare\AccountModel;
use Cloudflare\API\Adapter\Guzzle;
use Cloudflare\API\Auth\APIKey;
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 = [];
private $_auth_uid = "";
public function __construct(string $auth_uid)
{
parent::__construct();
$this->initClients();
$this->_auth_uid = $auth_uid;
self::$_request_max = getenv("cfmgr.request.max");
}
final protected function getAccountModel(): AccountModel
{
if ($this->_accountModel === null) {
$this->_accountModel = new AccountModel();
}
return $this->_accountModel;
}
final public function initClients(): void
{
foreach ($this->getAccountModel()->getEntitys() as $entity) {
$this->_clients[$entity->getKey()] = new Guzzle(
new APIKey($entity->getTitle(), $entity->getKey())
);
}
}
final public function getClient(): Guzzle
{
if (!array_key_exists($this->_auth_uid, $this->_clients)) {
throw new \Exception(+__FUNCTION__ . " => {$this->_auth_uid}에 해당하는 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->_client;
}
}