Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.27% covered (warning)
77.27%
17 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemsAdjustmentDTO
77.27% covered (warning)
77.27%
17 / 22
66.67% covered (warning)
66.67%
2 / 3
4.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 toArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 fromRequest
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Domain\Item\DTOs;
4
5use App\Models\Product\Product;
6use Domain\Shared\Interfaces\DataTransferObjectInterface;
7use Domain\StockIO\Enums\StockItemAdjustmentReasonEnum;
8use Illuminate\Foundation\Http\FormRequest;
9
10readonly final class ItemsAdjustmentDTO implements DataTransferObjectInterface
11{
12    /** @var ItemAdjustmentDTO[] $items */
13    public array $items;
14    public StockItemAdjustmentReasonEnum $reason;
15    public ?string $observation;
16
17    public function __construct(array $args)
18    {
19        $this->items = $args['items'];
20        $this->reason = $args['reason'];
21        $this->observation = $args['observation'] ?? null;
22    }
23
24    public function toArray(): array
25    {
26        return [
27            'items' => $this->items,
28            'reason' => $this->reason->value,
29            'observation' => $this->observation,
30        ];
31    }
32
33    public static function fromRequest(FormRequest $request): ItemsAdjustmentDTO
34    {
35        $items = [];
36        foreach ($request->validated('items') as $item) {
37            $items[] = new ItemAdjustmentDTO([
38                'product' => Product::find($item['product_id']),
39                'amount' => $item['amount'],
40                'old_amount' => $item['old_amount'],
41                'production_order' => $item['production_order'],
42                'movement_date' => $item['movement_date'],
43            ]);
44        }
45        return new self([
46            'items' => $items,
47            'reason' => StockItemAdjustmentReasonEnum::from($request->validated('reason')),
48            'observation' => $request->validated('observation'),
49        ]);
50    }
51}