Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| StockIOStartService | |
69.23% |
9 / 13 |
|
0.00% |
0 / 1 |
4.47 | |
0.00% |
0 / 1 |
| start | |
69.23% |
9 / 13 |
|
0.00% |
0 / 1 |
4.47 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\StockIO\StockIO; |
| 7 | use App\Models\User; |
| 8 | use Carbon\Carbon; |
| 9 | use Domain\StockIO\Enums\StockStatusEnum; |
| 10 | use Illuminate\Support\Facades\DB; |
| 11 | use Throwable; |
| 12 | |
| 13 | readonly class StockIOStartService |
| 14 | { |
| 15 | /** |
| 16 | * @param StockIO $stockIO |
| 17 | * @param User $user |
| 18 | * @return void |
| 19 | * @throws Throwable |
| 20 | */ |
| 21 | public function start(StockIO $stockIO, User $user): void |
| 22 | { |
| 23 | if ($stockIO->getAttribute('status') != StockStatusEnum::PENDING) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | try { |
| 28 | DB::beginTransaction(); |
| 29 | |
| 30 | $stockIO->setAttribute('status', StockStatusEnum::STARTED); |
| 31 | $stockIO->setAttribute('start_date', Carbon::now()); |
| 32 | $stockIO->responsible()->associate($user); |
| 33 | if ($stockIO->isDirty()) { |
| 34 | $stockIO->save(); |
| 35 | } |
| 36 | |
| 37 | DB::commit(); |
| 38 | } catch (Throwable $throwable) { |
| 39 | DB::rollBack(); |
| 40 | throw $throwable; |
| 41 | } |
| 42 | |
| 43 | ServiceProcessed::dispatch(self::class, $user); |
| 44 | } |
| 45 | } |