125 lines
4.3 KiB
PHP
125 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Machines\Application\UseCases;
|
|
|
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
|
use App\Modules\Machines\Application\DTOs\MachineData;
|
|
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
|
use App\Modules\Machines\Domain\Entities\Machine;
|
|
use App\Modules\Machines\Domain\Enums\CounterUnit;
|
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
|
use App\Modules\Machines\Domain\Exceptions\InvalidCounter;
|
|
use App\Modules\Machines\Domain\Exceptions\InvalidMachineName;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineNameTaken;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
|
|
use App\Modules\Machines\Domain\ValueObjects\MachineName;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Adds or changes a machine (management, spec 19.4). Its service units are
|
|
* chosen among those that use stock; a unit it already has may stay after
|
|
* the setting is turned off.
|
|
*/
|
|
final class SaveMachine
|
|
{
|
|
public function __construct(
|
|
private MachineRepository $machines,
|
|
private CatalogQueries $catalog,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws InvalidMachineName
|
|
* @throws InvalidCounter
|
|
* @throws MachineNameTaken
|
|
* @throws MachineNeedsServiceUnit
|
|
*/
|
|
public function add(MachineData $data): int
|
|
{
|
|
[$name, $type, $counters] = $this->parse($data);
|
|
$unitIds = $this->allowedUnits($data->serviceUnitIds, []);
|
|
|
|
return $this->transactions->run(function () use ($data, $name, $type, $counters, $unitIds): int {
|
|
if ($this->machines->isNameTaken($name->value)) {
|
|
throw new MachineNameTaken;
|
|
}
|
|
|
|
$machine = Machine::create($name, $type, $data->description, $unitIds, $data->responsibleUserId, $counters);
|
|
|
|
return (int) $this->machines->save($machine)->id();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @throws MachineNotFound
|
|
* @throws InvalidMachineName
|
|
* @throws InvalidCounter
|
|
* @throws MachineNameTaken
|
|
* @throws MachineNeedsServiceUnit
|
|
*/
|
|
public function update(int $machineId, MachineData $data): void
|
|
{
|
|
[$name, $type, $counters] = $this->parse($data);
|
|
|
|
$this->transactions->run(function () use ($machineId, $data, $name, $type, $counters): void {
|
|
$machine = $this->machines->findById($machineId) ?? throw new MachineNotFound;
|
|
|
|
if ($this->machines->isNameTaken($name->value, $machineId)) {
|
|
throw new MachineNameTaken;
|
|
}
|
|
|
|
$machine->change($name, $type, $data->description, $this->allowedUnits($data->serviceUnitIds, $machine->serviceUnitIds()), $data->responsibleUserId, $counters);
|
|
$this->machines->save($machine);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @throws MachineNotFound
|
|
*/
|
|
public function setActive(int $machineId, bool $active): void
|
|
{
|
|
$this->transactions->run(function () use ($machineId, $active): void {
|
|
$machine = $this->machines->findById($machineId) ?? throw new MachineNotFound;
|
|
$active ? $machine->activate() : $machine->deactivate();
|
|
$this->machines->save($machine);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return array{0: MachineName, 1: MachineType, 2: list<MachineCounter>}
|
|
*
|
|
* @throws InvalidMachineName
|
|
* @throws InvalidCounter
|
|
*/
|
|
private function parse(MachineData $data): array
|
|
{
|
|
$counters = [];
|
|
|
|
foreach ($data->counters as $counter) {
|
|
$unit = CounterUnit::tryFrom($counter['unit']) ?? throw new InvalidCounter;
|
|
$counters[] = new MachineCounter($counter['id'], $counter['name'], $unit);
|
|
}
|
|
|
|
return [new MachineName($data->name), MachineType::tryFrom($data->type) ?? MachineType::Other, $counters];
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $requested
|
|
* @param list<int> $current
|
|
* @return list<int>
|
|
*/
|
|
private function allowedUnits(array $requested, array $current): array
|
|
{
|
|
$stockUnitIds = $this->catalog->stockServiceUnitIds();
|
|
|
|
return array_values(array_filter(
|
|
array_unique($requested),
|
|
fn (int $id): bool => in_array($id, $stockUnitIds, true) || in_array($id, $current, true),
|
|
));
|
|
}
|
|
}
|