Files
ntspi-app/app/Services/Vicon/EducationalProgram/EducationalProgramService.php
T
F4ilji 7748b05547 feat(vikon): move API token from .env to integration_credentials
- Rename VICON_TOKEN → VIKON_API_TOKEN in .env and config
- Add ViconApiToken service reading from integration_credentials table
- Update 3 Vicon services to use ViconApiToken instead of config()
- Config/env kept as fallback for clean migration
2026-07-10 16:43:09 +05:00

62 lines
1.9 KiB
PHP
Executable File

<?php
namespace App\Services\Vicon\EducationalProgram;
use App\Jobs\CreateDirectionStudy;
use App\Jobs\CreateEducationalProgram;
use App\Services\Vicon\ViconApiToken;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class EducationalProgramService
{
public function __construct(
private readonly ViconApiToken $viconApiToken,
) {}
const EDU_LEVELS = [1,2,3,4,5,6];
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) {
$programsUuid[] = $program->uuid;
}
return $programsUuid;
}
public function getPrograms(int $edu_level) : object
{
$data = $this->callAPI("https://db-nica.ru/api/v1/programs?filter_edu_level=$edu_level&perPage=200", $this->viconApiToken->get());
return $data;
}
public function getProgram(string $uuid) : object
{
$data = $this->callAPI("https://db-nica.ru/api/v1/program/$uuid", $this->viconApiToken->get());
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; // Перебрасываем исключение
}
}
}