Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.82% |
20 / 34 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TransferItemsDTO | |
58.82% |
20 / 34 |
|
33.33% |
1 / 3 |
8.51 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| fromRequest | |
65.22% |
15 / 23 |
|
0.00% |
0 / 1 |
4.67 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Transferable\DTOs; |
| 4 | |
| 5 | use function array_map; |
| 6 | |
| 7 | use Carbon\Carbon; |
| 8 | use Illuminate\Foundation\Http\FormRequest; |
| 9 | |
| 10 | use App\Models\Item\Item; |
| 11 | |
| 12 | use Domain\ReadingLog\DTOs\ReadingLogDTO; |
| 13 | use Domain\ReadingLog\Enums\ReadingLogFormatEnum; |
| 14 | use Domain\ReadingLog\Enums\ReadingLogTypeEnum; |
| 15 | use Domain\Shared\Interfaces\DataTransferObjectInterface; |
| 16 | use Domain\Transaction\Enums\TransactionTypeEnum; |
| 17 | |
| 18 | readonly final class TransferItemsDTO implements DataTransferObjectInterface |
| 19 | { |
| 20 | /** @var TransferItemDTO[] $items */ |
| 21 | public array $items; |
| 22 | public TransactionTypeEnum $transactionTypeEnum; |
| 23 | public int $reason; |
| 24 | public ?string $observation; |
| 25 | |
| 26 | /** @var ReadingLogDTO[] $readingLogDTOs */ |
| 27 | public array $readingLogDTOs; |
| 28 | |
| 29 | public function __construct(array $args) |
| 30 | { |
| 31 | $this->items = $args['items']; |
| 32 | $this->transactionTypeEnum = TransactionTypeEnum::tryFrom($args['type']); |
| 33 | $this->reason = $args['reason']; |
| 34 | $this->readingLogDTOs = $args['reading_logs'] ?? []; |
| 35 | $this->observation = $args['observation'] ?? null; |
| 36 | } |
| 37 | |
| 38 | public function toArray(): array |
| 39 | { |
| 40 | return [ |
| 41 | 'items' => array_map(fn(TransferItemDTO $item) => $item->item->toArray(), $this->items), |
| 42 | 'type' => $this->transactionTypeEnum->value, |
| 43 | 'reason' => $this->reason, |
| 44 | 'observation' => $this->observation, |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | public static function fromRequest(FormRequest $request): TransferItemsDTO |
| 49 | { |
| 50 | $items = []; |
| 51 | foreach ($request->validated('items') as $item) { |
| 52 | $items[] = new TransferItemDTO([ |
| 53 | 'item' => Item::find($item['id']), |
| 54 | 'amount' => $item['amount'] |
| 55 | ]); |
| 56 | } |
| 57 | |
| 58 | $readingLogs = []; |
| 59 | if ($request->validated('reading_logs')) { |
| 60 | foreach ($request->validated('reading_logs') as $readingLog) { |
| 61 | $readingLogs[] = new ReadingLogDTO([ |
| 62 | 'format' => ReadingLogFormatEnum::tryFrom($readingLog['format']), |
| 63 | 'type' => ReadingLogTypeEnum::tryFrom($readingLog['type']), |
| 64 | 'read_at' => Carbon::createFromFormat('d/m/Y H:i:s', $readingLog['read_at']), |
| 65 | 'data' => $readingLog['data'], |
| 66 | 'is_valid' => $readingLog['is_valid'], |
| 67 | ]); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return new self([ |
| 72 | 'items' => $items, |
| 73 | 'type' => $request->validated('type'), |
| 74 | 'reason' => $request->validated('reason'), |
| 75 | 'reading_logs' => $readingLogs, |
| 76 | 'observation' => $request->validated('observation'), |
| 77 | ]); |
| 78 | } |
| 79 | } |