63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\GlobalSetting;
|
|
use GuzzleHttp\Client;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ServicePbgTask
|
|
{
|
|
private $client;
|
|
private $simbg_host;
|
|
private $fetch_per_page;
|
|
private $pbg_task_url;
|
|
private $service_token;
|
|
|
|
public function __construct(Client $client, ServiceTokenSIMBG $service_token)
|
|
{
|
|
$settings = GlobalSetting::whereIn('key', ['SIMBG_HOST', 'FETCH_PER_PAGE'])
|
|
->pluck('value', 'key');
|
|
|
|
$this->simbg_host = trim((string) ($settings['SIMBG_HOST'] ?? ""));
|
|
// $this->fetch_per_page = trim((string) ($settings['FETCH_PER_PAGE'] ?? "10"));
|
|
$this->fetch_per_page = 10;
|
|
$this->client = $client;
|
|
$this->service_token = $service_token;
|
|
$this->pbg_task_url = "{$this->simbg_host}/api/pbg/v1/list/?page=1&size={$this->fetch_per_page}&sort=ASC";
|
|
}
|
|
|
|
public function run_service()
|
|
{
|
|
$this->fetch_pbg_task();
|
|
}
|
|
|
|
private function fetch_pbg_task()
|
|
{
|
|
try {
|
|
$token = $this->service_token->get_token();
|
|
$options = [
|
|
'headers' => [
|
|
'Authorization' => "Bearer {$token['access']}",
|
|
'Accept' => 'application/json'
|
|
]
|
|
];
|
|
|
|
$fetch_data = $this->client->get($this->pbg_task_url, $options);
|
|
$response_status_code = $fetch_data->getStatusCode();
|
|
$response = json_decode($fetch_data->getBody()->getContents(), true);
|
|
$data = $response['data'];
|
|
$total_page = $response['total_page'];
|
|
|
|
Log::info("Successfully fetched PBG tasks", ['data' => $data]);
|
|
Log::info("Status code response", ['code' => $response_status_code]);
|
|
Log::info("Success", ['total_page' => $total_page]);
|
|
return $response;
|
|
} catch (Exception $e) {
|
|
Log::error("Failed to fetch PBG tasks", ['error' => $e->getMessage()]);
|
|
return null; // Return null if an error occurs
|
|
}
|
|
}
|
|
}
|