Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.64% |
3 / 22 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TransferItemDTO | |
13.64% |
3 / 22 |
|
33.33% |
1 / 3 |
21.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| fromRequest | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Transferable\DTOs; |
| 4 | |
| 5 | use Illuminate\Foundation\Http\FormRequest; |
| 6 | use Carbon\Carbon; |
| 7 | |
| 8 | use App\Models\Item\Item; |
| 9 | |
| 10 | use Domain\ReadingLog\DTOs\ReadingLogDTO; |
| 11 | use Domain\ReadingLog\Enums\ReadingLogFormatEnum; |
| 12 | use Domain\ReadingLog\Enums\ReadingLogTypeEnum; |
| 13 | use Domain\Shared\Interfaces\DataTransferObjectInterface; |
| 14 | |
| 15 | readonly final class TransferItemDTO implements DataTransferObjectInterface |
| 16 | { |
| 17 | public Item $item; |
| 18 | public float $amount; |
| 19 | /** @var ReadingLogDTO[] $readingLogDTOs */ |
| 20 | public array $readingLogDTOs; |
| 21 | |
| 22 | public function __construct(array $args) |
| 23 | { |
| 24 | $this->item = $args['item']; |
| 25 | $this->amount = $args['amount']; |
| 26 | $this->readingLogDTOs = $args['reading_logs'] ?? []; |
| 27 | } |
| 28 | |
| 29 | public function toArray(): array |
| 30 | { |
| 31 | return [ |
| 32 | 'item_id' => $this->item->getKey(), |
| 33 | 'amount' => $this->amount, |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | public static function fromRequest(FormRequest $request): TransferItemDTO |
| 38 | { |
| 39 | |
| 40 | $readingLogs = []; |
| 41 | if ($request->validated('reading_logs')) { |
| 42 | foreach ($request->validated('reading_log') as $readingLog) { |
| 43 | $readingLogs[] = new ReadingLogDTO([ |
| 44 | 'format' => ReadingLogFormatEnum::tryFrom($readingLog['format']), |
| 45 | 'type' => ReadingLogTypeEnum::tryFrom($readingLog['type']), |
| 46 | 'read_at' => Carbon::createFromFormat('d/m/Y H:i:s', $readingLog['read_at']), |
| 47 | 'data' => $readingLog['data'], |
| 48 | 'is_valid' => $readingLog['is_valid'], |
| 49 | ]); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return new self([ |
| 54 | 'item' => Item::find($request->validated('id')), |
| 55 | 'amount' => $request->validated('amount'), |
| 56 | 'reading_logs' => $readingLogs, |
| 57 | ]); |
| 58 | } |
| 59 | } |