63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Inventory\Application\UseCases;
|
|
|
|
use App\Modules\Inventory\Application\DTOs\StockItemData;
|
|
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
|
|
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
|
|
use App\Modules\Inventory\Domain\Enums\StockUnit;
|
|
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
|
|
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
|
|
use App\Modules\Inventory\Domain\Exceptions\StockItemGroupNotFound;
|
|
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
|
|
use App\Modules\Inventory\Domain\ValueObjects\StockName;
|
|
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
|
|
|
/**
|
|
* Checks a stock item's entered details, shared by adding and updating.
|
|
*/
|
|
final class StockItemFields
|
|
{
|
|
public function __construct(
|
|
private StockItemRepository $items,
|
|
private StockItemGroupRepository $groups,
|
|
) {}
|
|
|
|
/**
|
|
* @return array{name: StockName, unit: StockUnit, reorderLevelMilli: ?int}
|
|
*
|
|
* @throws InvalidStockName
|
|
* @throws InvalidStockQuantity
|
|
*/
|
|
public function parse(StockItemData $data): array
|
|
{
|
|
return [
|
|
'name' => new StockName($data->name),
|
|
'unit' => StockUnit::tryFrom($data->unit) ?? StockUnit::Piece,
|
|
'reorderLevelMilli' => StockQuantity::parse($data->reorderLevel)?->milli,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* A new item needs a group in use; an existing item may stay in its
|
|
* group after the group is taken out of use.
|
|
*
|
|
* @throws StockItemGroupNotFound
|
|
* @throws StockNameTaken
|
|
*/
|
|
public function check(StockItemData $data, StockName $name, ?int $exceptItemId = null): void
|
|
{
|
|
$group = $this->groups->findById($data->groupId);
|
|
|
|
if ($group === null || ($exceptItemId === null && ! $group->isActive())) {
|
|
throw new StockItemGroupNotFound;
|
|
}
|
|
|
|
if ($this->items->isNameTaken($name->value, $exceptItemId)) {
|
|
throw new StockNameTaken;
|
|
}
|
|
}
|
|
}
|