Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
10 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| StockOutputReleaseService | |
71.43% |
10 / 14 |
|
0.00% |
0 / 2 |
6.84 | |
0.00% |
0 / 1 |
| release | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| fetchCustomer | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
4.84 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\StockIO\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Customer\Customer; |
| 7 | use App\Models\StockIO\StockIO; |
| 8 | use App\Models\User; |
| 9 | use Domain\Customer\Exceptions\CustomerStatusException; |
| 10 | use Domain\StockIO\Exceptions\StockStatusException; |
| 11 | use Domain\StockIO\Exceptions\StockWithOutCustomerException; |
| 12 | |
| 13 | readonly class StockOutputReleaseService |
| 14 | { |
| 15 | /** |
| 16 | * @param StockIO $stockIO |
| 17 | * @param User $user |
| 18 | * @return void |
| 19 | */ |
| 20 | public function release(StockIO $stockIO, User $user): void |
| 21 | { |
| 22 | if (!$stockIO->isWaiting()) { |
| 23 | throw new StockStatusException(__('Output is not in waiting status.')); |
| 24 | } |
| 25 | |
| 26 | $customer = $this->fetchCustomer($stockIO->fetchCustomerCode()); |
| 27 | |
| 28 | $stockIO->releaseForTeam($customer->team); |
| 29 | $stockIO->save(); |
| 30 | |
| 31 | ServiceProcessed::dispatch(self::class, $user); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param int|string|null $customerCode |
| 36 | * @return Customer |
| 37 | */ |
| 38 | private function fetchCustomer(int|string|null $customerCode): Customer |
| 39 | { |
| 40 | if (!$customerCode) { |
| 41 | throw new StockWithOutCustomerException(__('Customer code not found or empty.')); |
| 42 | } |
| 43 | |
| 44 | /** @var ?Customer $customer */ |
| 45 | $customer = Customer::whereCustomerCode($customerCode)->first(); |
| 46 | |
| 47 | if (!$customer) { |
| 48 | throw new StockWithOutCustomerException(__('Customer not found.')); |
| 49 | } |
| 50 | |
| 51 | if ($customer->isPartial()) { |
| 52 | throw new CustomerStatusException(__('Customer :customer has incomplete registration. Please update.', ['customer' => $customerCode])); |
| 53 | } |
| 54 | |
| 55 | return $customer; |
| 56 | } |
| 57 | } |