78 lines
3.1 KiB
PHP
78 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
|
use App\Modules\Machines\Domain\Entities\Machine;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineName;
|
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class EloquentMachineRepository implements MachineRepository
|
|
{
|
|
public function findById(int $id): ?Machine
|
|
{
|
|
$model = MachineModel::query()->with('counters')->find($id);
|
|
|
|
return $model === null ? null : $this->toEntity($model);
|
|
}
|
|
|
|
public function save(Machine $machine): Machine
|
|
{
|
|
$model = $machine->id() === null ? new MachineModel : MachineModel::query()->findOrFail($machine->id());
|
|
|
|
$model->fill([
|
|
'name' => $machine->name()->value,
|
|
'type' => $machine->type(),
|
|
'description' => $machine->description(),
|
|
'responsible_user_id' => $machine->responsibleUserId(),
|
|
'is_active' => $machine->isActive(),
|
|
])->save();
|
|
|
|
DB::table('machine_service_units')->where('machine_id', $model->id)->delete();
|
|
DB::table('machine_service_units')->insert(array_map(
|
|
fn (int $unitId): array => ['machine_id' => $model->id, 'service_unit_id' => $unitId],
|
|
$machine->serviceUnitIds(),
|
|
));
|
|
|
|
$keptIds = [];
|
|
|
|
foreach ($machine->counters() as $index => $counter) {
|
|
$counterModel = $counter->id === null
|
|
? new MachineCounterModel(['machine_id' => $model->id])
|
|
: MachineCounterModel::query()->where('machine_id', $model->id)->findOrFail($counter->id);
|
|
|
|
$counterModel->fill(['name' => $counter->name, 'unit' => $counter->unit, 'sort_order' => $index + 1])->save();
|
|
$keptIds[] = $counterModel->id;
|
|
}
|
|
|
|
MachineCounterModel::query()->where('machine_id', $model->id)->whereNotIn('id', $keptIds)->delete();
|
|
|
|
return $this->toEntity($model->load('counters'));
|
|
}
|
|
|
|
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
|
{
|
|
return MachineModel::query()
|
|
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
|
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
|
->exists();
|
|
}
|
|
|
|
private function toEntity(MachineModel $model): Machine
|
|
{
|
|
return Machine::reconstitute(
|
|
$model->id,
|
|
new MachineName($model->name),
|
|
$model->type,
|
|
$model->description,
|
|
DB::table('machine_service_units')->where('machine_id', $model->id)->orderBy('service_unit_id')->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->values()->all(),
|
|
$model->responsible_user_id,
|
|
$model->counters->map(fn (MachineCounterModel $counter): MachineCounter => new MachineCounter($counter->id, $counter->name, $counter->unit))->values()->all(),
|
|
$model->is_active,
|
|
);
|
|
}
|
|
}
|