Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.29% |
18 / 28 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TransactionDTO | |
64.29% |
18 / 28 |
|
66.67% |
2 / 3 |
3.41 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| fromRequest | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Transaction\DTOs; |
| 4 | |
| 5 | use Illuminate\Foundation\Http\FormRequest; |
| 6 | |
| 7 | use App\Models\Item\Item; |
| 8 | |
| 9 | use Domain\Item\Interfaces\HasItemsInterface; |
| 10 | use Domain\Shared\Interfaces\DataTransferObjectInterface; |
| 11 | use Domain\Transaction\Enums\TransactionTypeEnum; |
| 12 | |
| 13 | readonly final class TransactionDTO implements DataTransferObjectInterface |
| 14 | { |
| 15 | |
| 16 | public Item $item; |
| 17 | public HasItemsInterface $from; |
| 18 | public HasItemsInterface $to; |
| 19 | public float $amount; |
| 20 | public TransactionTypeEnum $type; |
| 21 | public ?int $reason; |
| 22 | public ?string $observation; |
| 23 | public int $sector; |
| 24 | |
| 25 | public function __construct(array $args) |
| 26 | { |
| 27 | $this->item = $args['item']; |
| 28 | $this->from = $args['from']; |
| 29 | $this->to = $args['to']; |
| 30 | $this->amount = $args['amount']; |
| 31 | $this->type = $args['type']; |
| 32 | $this->reason = $args['reason'] ?? null; |
| 33 | $this->observation = $args['observation'] ?? null; |
| 34 | $this->sector = $args['sector']; |
| 35 | } |
| 36 | |
| 37 | public function toArray(): array |
| 38 | { |
| 39 | return [ |
| 40 | 'item' => $this->item->toArray(), |
| 41 | 'from_transferable_id' => $this->from->transferableKey(), |
| 42 | 'to_transferable_id' => $this->to->transferableKey(), |
| 43 | 'amount' => $this->amount, |
| 44 | 'type' => $this->type, |
| 45 | 'reason' => $this->reason, |
| 46 | 'observation' => $this->observation, |
| 47 | 'sector' => $this->sector, |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | public static function fromRequest(FormRequest $request): DataTransferObjectInterface |
| 52 | { |
| 53 | return new self([ |
| 54 | 'item' => Item::find($request->validated('item_id')), |
| 55 | 'from' => $request->validated('sender'), |
| 56 | 'to' => $request->validated('recipient'), |
| 57 | 'amount' => $request->validated('amount'), |
| 58 | 'type' => $request->validated('type'), |
| 59 | 'reason' => $request->validated('reason'), |
| 60 | 'observation' => $request->validated('observation'), |
| 61 | 'sector' => $request->validated('sector') |
| 62 | ]); |
| 63 | } |
| 64 | } |