Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StockIOStartService
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
4.47
0.00% covered (danger)
0.00%
0 / 1
 start
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
4.47
1<?php
2
3namespace Domain\StockIO\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\StockIO\StockIO;
7use App\Models\User;
8use Carbon\Carbon;
9use Domain\StockIO\Enums\StockStatusEnum;
10use Illuminate\Support\Facades\DB;
11use Throwable;
12
13readonly 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}