Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
10 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
StockOutputReleaseService
71.43% covered (warning)
71.43%
10 / 14
0.00% covered (danger)
0.00%
0 / 2
6.84
0.00% covered (danger)
0.00%
0 / 1
 release
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 fetchCustomer
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
4.84
1<?php
2
3namespace Domain\StockIO\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\Customer\Customer;
7use App\Models\StockIO\StockIO;
8use App\Models\User;
9use Domain\Customer\Exceptions\CustomerStatusException;
10use Domain\StockIO\Exceptions\StockStatusException;
11use Domain\StockIO\Exceptions\StockWithOutCustomerException;
12
13readonly 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}