Automation/app/Controllers/Cloudflare/AccountController.php
2024-09-20 19:51:36 +09:00

118 lines
4.4 KiB
PHP

<?php
namespace App\Controllers\Cloudflare;
use App\Entities\Cloudflare\AccountEntity;
use App\Libraries\MySocket\Cloudflare\AccountSocket;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\Cloudflare\AccountModel;
use App\Traits\AccountTrait;
class AccountController extends MyCloudflare
{
use AccountTrait;
private $_mySocket = null;
private $_model = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->session = $this->session_AccountTrait();
$this->class_name = 'Account';
helper($this->class_name);
}
final protected function getMySocket(): AccountSocket
{
if ($this->_mySocket === null) {
$this->_mySocket = new AccountSocket($auth_uid);
}
return $this->_mySocket;
}
final protected function getModel(): AccountModel
{
if ($this->_model === null) {
$this->_model = new AccountModel();
}
return $this->_model;
}
protected function create_form_process(): void
{
$this->fields = ['id', 'authkey'];
$this->filter_fields = ['status'];
parent::create_form_process();
}
public function create_form(): RedirectResponse|string
{
try {
$this->fields = ['id', 'authkey'];
$this->filter_fields = ['status'];
$this->action = 'create';
$this->getModel()->setAction($this->action);
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return view("/{$this->class_name}/insert", ['attributes' => $this->getAttributes]);
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage());
}
}
protected function create_process($model): AccountEntity
{
$datas = [
'name' => $name . "'s Account",
'type' => 'standard',
];
$result = $this->getClient()->post("accounts", $datas);
$result = json_decode($result->getBody());
if (!$result->success) {
throw new \Exception(var_export($result, true));
}
//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"}
// ]
$formDatas[$this->getAuthModel()->PK()] = $result->id;
$formDatas[$this->getAuthModel()->getTitleField()] = $result->name;
$formDatas['type'] = $result->type;
$formDatas['status'] = 'use';
$formDatas['updated_at'] = $result->created_on;
$formDatas['created_at'] = $result->created_on;
return parent::create_process($model);
}
public function create(): RedirectResponse
{
$this->getModel()->transStart();
try {
$this->fields = ['id', 'authkey'];
$this->filter_fields = ['status'];
$this->action = 'create';
$this->getModel()->setAction($this->action);
$entity = $this->create_process($this->getModel());
log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다.");
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
} catch (\Exception $e) {
//Transaction Rollback
$this->getModel()->transRollback();
log_message("error", $e->getMessage());
$this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return redirect()->back()->withInput();
}
}
}