Files
F4ilji 73a0a6fe9c feat(vikon): extract API URL from hardcoded to integration_credentials
- Rename ViconApiToken → ViconApiConfig with token() and apiUrl()
- Replace 9 hardcoded db-nica.ru URLs with dynamic apiUrl()
- Payload now stores both token and api_url with fallback
2026-07-10 16:47:18 +05:00

93 lines
2.9 KiB
PHP
Executable File

<?php
namespace App\Services\Vicon\DirectionStudy;
use App\Services\Vicon\ViconApiConfig;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class DirectionStudyService
{
public function __construct(
private readonly ViconApiConfig $viconApiConfig,
) {}
const EDU_LEVELS = [1, 2, 3, 4, 5, 6];
public function getAllNaprsUuid() : array
{
$naprs = [];
$naprsUuid = [];
foreach (self::EDU_LEVELS as $level) {
$data = $this->getNaprs($level);
$naprs = array_merge($naprs, $data->rows);
}
foreach ($naprs as $napr) {
$naprUuid = $napr->uuid;
array_push($naprsUuid, $naprUuid);
}
return $naprsUuid;
}
public function getAllProgramsUuid(): array
{
$programs = [];
$programsUuid = [];
foreach (self::EDU_LEVELS as $level) {
$data = $this->getPrograms($level);
$programs = array_merge($programs, $data->rows);
}
foreach ($programs as $program) {
array_push($programsUuid, $program->uuid);
}
return $programsUuid;
}
public function getNaprs(int $edu_level): object
{
$data = $this->callAPI("{$this->viconApiConfig->apiUrl()}/naprs?perPage=200&filter_edu_level=$edu_level", $this->viconApiConfig->token());
return $data;
}
public function getNapr(string $uuid): object
{
$data = $this->callAPI("{$this->viconApiConfig->apiUrl()}/napr/$uuid", $this->viconApiConfig->token());
return $data;
}
public function getPrograms(int $edu_level): object
{
$data = $this->callAPI("{$this->viconApiConfig->apiUrl()}/programs?filter_edu_level=$edu_level&perPage=200", $this->viconApiConfig->token());
return $data;
}
public function getProgram(string $uuid): object
{
$data = $this->callAPI("{$this->viconApiConfig->apiUrl()}/program/$uuid", $this->viconApiConfig->token());
return $data;
}
public function getProgramDocs(string $uuid): object
{
$data = $this->callAPI("{$this->viconApiConfig->apiUrl()}/program/$uuid/edu-docs?perPage=200", $this->viconApiConfig->token());
return $data;
}
private function callAPI(string $endpoint, string $token = null): object
{
try {
$response = Http::withToken($token)->get($endpoint);
$data = $response->object();
// Проверяем наличие сообщения об ошибке в ответе
if (isset($data->message)) {
throw new \Exception($data->message);
}
return $data;
} catch (\Exception $e) {
Log::channel('app')->error('API call failed', ['error' => $e->getMessage()]);
throw $e; // Перебрасываем исключение
}
}
}