41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Inventory\Application\UseCases;
|
|
|
|
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
|
|
use App\Modules\Inventory\Domain\Entities\StockItemGroup;
|
|
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
|
|
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
|
|
use App\Modules\Inventory\Domain\ValueObjects\StockName;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Adds a stock item group last in order.
|
|
*/
|
|
final class AddStockItemGroup
|
|
{
|
|
public function __construct(
|
|
private StockItemGroupRepository $groups,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws InvalidStockName
|
|
* @throws StockNameTaken
|
|
*/
|
|
public function execute(string $name): int
|
|
{
|
|
$name = new StockName($name);
|
|
|
|
return $this->transactions->run(function () use ($name): int {
|
|
if ($this->groups->isNameTaken($name->value)) {
|
|
throw new StockNameTaken;
|
|
}
|
|
|
|
return (int) $this->groups->save(StockItemGroup::create($name, $this->groups->nextSortOrder()))->id();
|
|
});
|
|
}
|
|
}
|