cfmgrv3/app/Libraries/Cloudflare/API/Account.php
2023-06-21 13:43:53 +09:00

80 lines
2.7 KiB
PHP

<?php
namespace App\Libraries\Cloudflare\API;
use App\Libraries\Log\Log;
class Account extends API
{
private $_endPoint = null;
private $_entity = null;
public function __construct(\App\Entities\Cloudflare\API\AuthEntity $parent)
{
parent::__construct($parent);
$this->_model = new \App\Models\Cloudflare\API\AccountModel();
}
protected function setAdapter()
{
if (!is_null($this->_adapter)) {
throw new \Exception("Adapter가 이미 지정되었습니다.");
}
$apikey = new \Cloudflare\API\Auth\APIKey(
$this->getParent()->getAuthId(),
$this->getParent()->getAuthKey()
);
$this->_adapter = new \Cloudflare\API\Adapter\Guzzle($apikey);
// throw new \Exception(var_export($this->_adapter, true));
}
public function getClassName()
{
return 'Account';
}
protected function getEntityByResult(\stdClass $cfResult): \App\Entities\Cloudflare\API\AccountEntity
{
// dd($cfResult);exit;
// [
// {"id":"078e88a7735965b661715af13031ecb0",
// "name":"Cloudwin002@idcjp.jp's Account",
// "type":"standard",
// "settings":{
// "enforce_twofactor":false,
// "api_access_enabled":null,
// "access_approval_expiry":null,
// "use_account_custom_ns_by_default":false
// },
// "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}},
// "created_on":"2017-06-26T05:44:49.470184Z"}
// ]
$entity = is_null($this->_entity) ? new \App\Entities\Cloudflare\API\AccountEntity() : $this->_entity;
$entity->uid = $cfResult->id;
$entity->auth_uid = $this->getParent()->getPrimaryKey();
$entity->title = $cfResult->name;
$entity->type = $cfResult->type;
$entity->status = $this->getParent()->status;
$entity->updated_at = $cfResult->created_on;
$entity->created_at = $cfResult->created_on;
return $entity;
}
public function insert(array $fieldDatas): \App\Entities\Cloudflare\API\AccountEntity
{
$options = [
'name' => $fieldDatas['name'],
'type' => isset($fieldDatas['type']) ? $fieldDatas['type'] : 'standard',
];
$cfResult = $this->getAdapter()->post('accounts', $options);
$cfResult = json_decode($cfResult->getBody());
if (!$cfResult->success) {
throw new \Exception(var_export($cfResult, true));
}
$entity = $this->getEntityByResult($cfResult->result);
Log::add("warning", "API {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.");
return $entity;
}
protected function getCFResults_List(int $page): array
{
$this->_endPoint = is_null($this->_endPoint) ? new \Cloudflare\API\Endpoints\Accounts($this->getAdapter()) : $this->_endPoint;
return $this->_endPoint->listAccounts($page, CF_ADAPTER_PERPAGE_MAX)->result;
}
}