Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.33% covered (warning)
53.33%
16 / 30
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PickItemDTO
53.33% covered (warning)
53.33%
16 / 30
33.33% covered (danger)
33.33%
1 / 3
7.54
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 toArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 fromRequest
52.94% covered (warning)
52.94%
9 / 17
0.00% covered (danger)
0.00%
0 / 1
3.94
1<?php
2
3namespace Domain\PickingOrder\DTOs;
4
5use App\Models\StockIO\StockIOProduct;
6use Carbon\Carbon;
7use Domain\Integration\DTOs\IntegrationItemDTO;
8use Domain\ReadingLog\DTOs\ReadingLogDTO;
9use Domain\ReadingLog\Enums\ReadingLogFormatEnum;
10use Domain\ReadingLog\Enums\ReadingLogTypeEnum;
11use Domain\Shared\Interfaces\DataTransferObjectInterface;
12use Domain\Shared\Support\DecimalNormalizer;
13use Illuminate\Foundation\Http\FormRequest;
14
15final readonly class PickItemDTO implements DataTransferObjectInterface
16{
17    public StockIOProduct $stockIOProduct;
18
19    public float $amount;
20
21    public IntegrationItemDTO $integrationItemDTO;
22
23    public ?string $observation;
24
25    /** @var ReadingLogDTO[] */
26    public array $readingLogDTOs;
27
28    public function __construct(array $args)
29    {
30        $this->stockIOProduct = $args['stock_io_product'];
31        $this->amount = DecimalNormalizer::toFloat($args['amount']);
32        $this->integrationItemDTO = new IntegrationItemDTO([
33            'production_order' => $args['production_order'],
34        ]);
35        $this->observation = $args['observation'] ?? null;
36        $this->readingLogDTOs = $args['reading_logs'] ?? [];
37    }
38
39    public function toArray(): array
40    {
41        return [
42            'stock_io_product_id' => $this->stockIOProduct->getKey(),
43            'amount' => $this->amount,
44            'observation' => $this->observation,
45            'production_order' => $this->integrationItemDTO->productionOrder,
46        ];
47    }
48
49    public static function fromRequest(FormRequest $request): PickItemDTO
50    {
51        $readingLogs = [];
52        if ($request->validated('reading_logs')) {
53            foreach ($request->validated('reading_logs') as $readingLog) {
54                $readingLogs[] = new ReadingLogDTO([
55                    'format' => ReadingLogFormatEnum::tryFrom($readingLog['format']),
56                    'type' => ReadingLogTypeEnum::tryFrom($readingLog['type']),
57                    'read_at' => Carbon::createFromFormat('d/m/Y H:i:s', $readingLog['read_at']),
58                    'data' => $readingLog['data'],
59                    'is_valid' => $readingLog['is_valid'],
60                ]);
61            }
62        }
63
64        return new self([
65            'stock_io_product' => StockIOProduct::find($request->validated('stock_io_product_id')),
66            'amount' => $request->validated('amount'),
67            'production_order' => $request->validated('production_order'),
68            'observation' => $request->validated('observation'),
69            'reading_logs' => $readingLogs,
70        ]);
71    }
72}