You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.8 KiB
82 lines
2.8 KiB
<?php
|
|
|
|
namespace App\Domains\Academic\Integration\Mappers;
|
|
|
|
use App\Domains\Academic\Actions\Curriculum\AdjustStudyProgramDetails;
|
|
use App\Domains\Academic\Actions\Curriculum\RegisterStudyProgram;
|
|
use App\Domains\Academic\DTOs\Curriculum\AdjustStudyProgramDTO;
|
|
use App\Domains\Academic\DTOs\Curriculum\RegisterStudyProgramDTO;
|
|
use App\Domains\Academic\Models\Faculty;
|
|
use App\Domains\Academic\Models\StudyProgram;
|
|
use App\Domains\System\Enums\LifecycleStatus;
|
|
use App\Domains\System\Support\Integration\DataPayloadMapper;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class StudyProgramDataMapper implements DataPayloadMapper
|
|
{
|
|
private static array $facultyCache = [];
|
|
|
|
public function __construct(
|
|
protected RegisterStudyProgram $registerStudyProgram,
|
|
protected AdjustStudyProgramDetails $adjustStudyProgramDetails,
|
|
)
|
|
{
|
|
// Inject required Domain and Cross-Domain Actions via constructor composition
|
|
}
|
|
|
|
public function getLookupKey(): string
|
|
{
|
|
// Return the unique string key used to identify existing records (e.g., 'email')
|
|
return 'external_id';
|
|
}
|
|
|
|
public function transform(array $rawData): array
|
|
{
|
|
// Normalize incoming data array formats into an internal domain-safe layout
|
|
return [
|
|
'name' => $rawData['nama_program_studi'],
|
|
'code' => $rawData['kode_program_studi'],
|
|
'level' => $rawData['nama_jenjang_pendidikan'],
|
|
'faculty_id' => self::resolveFacultyId($rawData['id_fakultas']),
|
|
'external_id' => $rawData['id_prodi'],
|
|
];
|
|
}
|
|
|
|
public function updateOrCreateDomainState(array $payload, ?Model $existingModel = null): void
|
|
{
|
|
/** @var StudyProgram $studyProgram */
|
|
$studyProgram = $existingModel;
|
|
|
|
if ($studyProgram) {
|
|
$this->adjustStudyProgramDetails->execute($studyProgram, new AdjustStudyProgramDTO(
|
|
level: $payload['level'],
|
|
status: LifecycleStatus::ACTIVE,
|
|
));
|
|
} else {
|
|
$this->registerStudyProgram->execute(new RegisterStudyProgramDTO(
|
|
name: $payload['name'],
|
|
code: $payload['code'],
|
|
level: $payload['level'],
|
|
status: LifecycleStatus::ACTIVE,
|
|
facultyId: $payload['faculty_id'],
|
|
externalId: $payload['external_id'],
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return class-string
|
|
*/
|
|
public function getModelClass(): string
|
|
{
|
|
return StudyProgram::class;
|
|
}
|
|
|
|
private function resolveFacultyId(string $externalId): int {
|
|
if (!isset(self::$facultyCache[$externalId])) {
|
|
self::$facultyCache[$externalId] = Faculty::where('external_id', $externalId)->first()->id;
|
|
}
|
|
|
|
return self::$facultyCache[$externalId];
|
|
}
|
|
}
|
|
|