146 lines
5.2 KiB
PHP
146 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
use App\Entities\Cloudflare\AuthEntity;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use stdClass;
|
|
use App\Traits\MylogTrait;
|
|
|
|
class AccountService extends CloudflareService
|
|
{
|
|
use MylogTrait;
|
|
private ?AuthEntity $_parent_entity = null;
|
|
private ?AccountModel $_model = null;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
final public function getClassName(): string
|
|
{
|
|
return "Account";
|
|
}
|
|
final public function getClassPath(): string
|
|
{
|
|
return parent::getClassPath() . $this->getClassName();
|
|
}
|
|
private function getParentEntity(): AuthEntity
|
|
{
|
|
if ($this->_parent_entity === null) {
|
|
throw new \Exception(__FUNCTION__ . "에서 부모정보가 없습니다.");
|
|
}
|
|
return $this->_parent_entity;
|
|
}
|
|
|
|
private function setParentEntity(AuthEntity $parent_entity): void
|
|
{
|
|
$this->_parent_entity = $parent_entity;
|
|
$this->setAuthEntity($this->getParentEntity());
|
|
}
|
|
|
|
public function getModel(): AccountModel
|
|
{
|
|
if ($this->_model === null) {
|
|
$this->_model = new AccountModel();
|
|
}
|
|
return $this->_model;
|
|
}
|
|
|
|
public function create(AuthEntity $parent_entity, array $formDatas): AccountEntity
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
$entity = $this->getModel()->create($formDatas);
|
|
//생성값 formDatas Log남기기
|
|
$this->add_MylogTrait(__FUNCTION__, $formDatas, $entity);
|
|
|
|
return $entity;
|
|
}
|
|
|
|
public function modify(AuthEntity $parent_entity, AccountEntity $entity, array $formDatas): AccountEntity
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
//변경전 entity 값, 변경값 formDatas Log남기기
|
|
$this->add_MylogTrait(__FUNCTION__, $formDatas, $entity);
|
|
return $this->getModel()->modify($entity, $formDatas);
|
|
}
|
|
|
|
public function delete(AuthEntity $parent_entity, AccountEntity $entity): void
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
//삭제전 entity 값 Log남기기
|
|
$this->add_MylogTrait(__FUNCTION__, [], $entity);
|
|
$this->getModel()->delete($entity->getPK());
|
|
}
|
|
|
|
//Result 형태
|
|
// [
|
|
// {"id":"078e88a7735965b661715af13031ecb0",
|
|
// "name":"Cloudwin002@idcjp.jp's Auth",
|
|
// "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"}
|
|
// ]
|
|
|
|
protected function getArrayByResult(stdClass $result, array $formDatas = []): array
|
|
{
|
|
$formDatas[AccountModel::PK] = $result->id;
|
|
$formDatas[AccountModel::PARENT] = $this->getAuthEntity()->getPK();
|
|
$formDatas[AccountModel::TITLE] = $result->name;
|
|
$formDatas['type'] = $result->type;
|
|
$formDatas['status'] = $this->getAuthEntity()->status;
|
|
$formDatas['updated_at'] = date("Y-m-d H:i:s");
|
|
$formDatas['created_at'] = $result->created_on;
|
|
return $formDatas;
|
|
}
|
|
|
|
public function reload(AuthEntity $parent_entity): array
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
log_message("notice", "\n----------Auth {$this->getParentEntity()->getTitle()}의 Account 처리 시작-----------");
|
|
|
|
$entities = [];
|
|
try {
|
|
$results = $this->reload_procedure("accounts");
|
|
$total = count($results);
|
|
|
|
if ($total > 0) {
|
|
foreach ($results as $index => $result) {
|
|
if (!is_object($result) || get_class($result) !== 'stdClass') {
|
|
log_message("error", "Account: result is not a stdClass:\n" . var_export($result, true) . "\n");
|
|
continue;
|
|
}
|
|
|
|
$formDatas = $this->getArrayByResult($result);
|
|
$entity = $this->getModel()->modify(new AccountEntity(), $formDatas);
|
|
log_message("debug", ($index + 1) . "/{$total} => {$entity->getTitle()} Account 처리,[{$this->getMySocket()::$_request}]");
|
|
$entities[$entity->getPK()] = $entity;
|
|
}
|
|
|
|
//부모키를 기준으로 CF에 존재하지 않는 데이터 삭제용
|
|
$this->getModel()->where(AccountModel::PARENT, $this->getParentEntity()->getPK());
|
|
$this->getModel()->whereNotIn(AccountModel::PK, array_keys($entities));
|
|
$this->getModel()->delete();
|
|
// log_message("debug", $this->getModel()->getLastQuery());
|
|
}
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
|
|
log_message("notice", "\n-----------Auth {$this->getParentEntity()->getTitle()}의 Account 처리[" . count($entities) . "개] 완료-----------");
|
|
return $entities;
|
|
}
|
|
}
|