37 lines
1.2 KiB
PHP
37 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Catalog\Application\UseCases;
|
|
|
|
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
|
|
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
|
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
|
use App\Modules\Catalog\Domain\Exceptions\SharedUnitCannotUseStock;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Turns the stock and machine features on or off in a service unit. Turning
|
|
* them off hides the features; recorded movements and work lists stay.
|
|
*/
|
|
final class SetServiceUnitUsesStock
|
|
{
|
|
public function __construct(
|
|
private ServiceUnitRepository $serviceUnits,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws CatalogItemNotFound
|
|
* @throws SharedUnitCannotUseStock
|
|
*/
|
|
public function execute(int $serviceUnitId, bool $usesStock): void
|
|
{
|
|
$this->transactions->run(function () use ($serviceUnitId, $usesStock): void {
|
|
$unit = $this->serviceUnits->findById($serviceUnitId) ?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $serviceUnitId);
|
|
$unit->useStock($usesStock);
|
|
$this->serviceUnits->save($unit);
|
|
});
|
|
}
|
|
}
|