Files
ntspi-app/app/Services/Vicon/DirectionStudy/AdmissionPlanService.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

149 lines
4.6 KiB
PHP
Executable File

<?php
namespace App\Services\Vicon\DirectionStudy;
use App\Jobs\CreateDirectionStudy;
use App\Jobs\CreateEducationalProgram;
use App\Services\Vicon\ViconApiToken;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class AdmissionPlanService
{
public function __construct(
private readonly ViconApiToken $viconApiToken,
) {}
public function getLevelEducationCodes(object $campaign) : array
{
return array_map(function ($item) {
return (int)$item->campaign_levels_code;
}, $campaign->groups);
}
public function findActiveCampaign(array $campaigns): object|null
{
return array_reduce($campaigns, function($carry, $item) {
return $carry ?? ($item->status == 1 ? $item : null);
});
}
public function getCampaigns(): array
{
try {
$response = $this->callAPI(
"https://db-nica.ru/api/v1/campaigns",
$this->viconApiToken->get()
);
if (!is_array($response)) {
Log::channel('app')->warning('Unexpected response type in getCampaigns', [
'type' => gettype($response),
'response' => $response
]);
}
return $response;
} catch (\Exception $e) {
Log::channel('app')->error('API call failed in getCampaigns', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return []; // или вернуть пустой объект (object)[]
}
}
public function getAdmissionPlans(int $campaign_levels_code): object|array
{
try {
$response = $this->callAPI(
"https://db-nica.ru/api/v1/planPriema/$campaign_levels_code",
$this->viconApiToken->get()
);
if (!is_object($response)) {
Log::channel('app')->warning('Unexpected response type in getAdmissionPlans', [
'expected' => 'object',
'actual' => gettype($response),
'campaign_levels_code' => $campaign_levels_code,
'response' => $response
]);
return [];
}
return $response;
} catch (\Exception $e) {
Log::channel('app')->error('API call failed in getAdmissionPlans', [
'error' => $e->getMessage(),
'campaign_levels_code' => $campaign_levels_code,
'trace' => $e->getTraceAsString()
]);
return [];
}
}
public function filterEmptyNaprOrProg(array $array): array
{
return array_values(array_filter($array, function ($item) {
return !empty($item->napr_or_prog);
}));
}
public function transformData(array $data) : array
{
return array_map(function ($item) {
$array = [];
$array['id'] = $item->napr_or_prog[0]->inner_code;
$array['plan'] = $item->groups_condition[0];
return $array;
}, $data);
}
public function convertToSaveExamData(array $data) : array
{
return array_map(function ($item) {
$array = [];
$array['title'] = $item->exam_name;
$array['priority'] = $item->priority;
$array['types'] = array_map(function ($i) {
return [
'type' => $i->type,
'min_ball' => $i->min_ball,
];
}, $item->types);
return $array;
}, $data);
}
public function convertToSaveContestData(array $data) : array
{
return array_map(function ($item) {
$array = [];
$array['form_education'] = $item->form_obuch;
$array['places'] = [
'form_budget' => $item->source,
'count' => $item->count_places
];
return $array;
}, $data);
}
private function callAPI(string $endpoint, string $token = null): object|array
{
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: ' . $e->getMessage());
throw $e; // Перебрасываем исключение
}
}
}