<?php

namespace App\Controllers\CLI\Cloudflare\API;

use App\Entities\Cloudflare\API\AccountEntity;

class Account extends API
{
    private function getAccounts(int $auth_uid): array
    {
        if ($auth_uid) {
            $entitys = $this->getAccountModel()->asObject(AccountEntity::class)
                ->where(['status' => 'use', 'auth_uid' => $auth_uid])
                ->findAll();
        } else {
            $entitys = $this->getAccountModel()->asObject(AccountEntity::class)
                ->where('status', 'use')
                ->findAll();
        }
        echo __FUNCTION__ . "에서 호출:" . $this->getAccountModel()->getLastQuery();
        return $entitys;
    }
    final public function execute($page_limit = 0, int $auth_uid = 0)
    {
        try {
            //transation처리
            // $this->getAccountModel()->db->transBegin();
            //Zone 처리
            foreach ($this->getAccounts($auth_uid) as $entity) {
                $api = new \App\Libraries\Cloudflare\API\Zone($entity, true);
                $api->reload((int)$page_limit);
            }
            //transation 완료
            // $this->getAccountModel()->db->transCommit();
            echo __METHOD__ . "에서 Zone Reload 완료";
        } catch (\Exception $e) {
            // transaction 오류복구
            // $this->getAccountModel()->db->transRollback();
            $message = __METHOD__ . "에서 Zone Reload 오류\n" . $e->getMessage();
            log_message("error", $message);
            echo $message;
        }
    }
}
