Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransactionSimpleResource
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 toArray
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 convertToResource
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Domain\Transaction\Resources;
4
5use App\Models\Address\Address;
6use App\Models\StockIO\StockIO;
7use Carbon\Carbon;
8use Domain\Address\Resources\AddressPrintResource;
9use Domain\StockIO\Resources\StockOIResource;
10use Domain\Transaction\Services\LocaleArtefactService;
11use Domain\User\Resources\UserResource;
12use Illuminate\Http\Request;
13use Illuminate\Http\Resources\Json\JsonResource;
14
15/**
16 * @property array $item
17 * @property float $amount
18 * @property Carbon $created_at
19 * @property mixed $type
20 * @property int $reason
21 * @property int $from_transferable_id
22 * @property string $from_type
23 * @property int $to_transferable_id
24 * @property string $to_type
25 */
26class TransactionSimpleResource extends JsonResource
27{
28    public function toArray(Request $request): array
29    {
30        $fromTransferable = (new LocaleArtefactService())->locale($this->from_transferable_id);
31        $toTransferable = (new LocaleArtefactService())->locale($this->to_transferable_id);
32
33        $from = $this->convertToResource($fromTransferable->transferable) ?? $fromTransferable;
34        $to = $this->convertToResource($toTransferable->transferable) ?? $toTransferable;
35
36        return [
37            'type' => $this->type,
38            'from' => $from,
39            'to' => $to,
40            'amount' => $this->amount,
41            'reason' => $this->reason,
42            'item' => $this->item,
43            'created_at' => (string)$this->created_at,
44            'user' => UserResource::make($this->whenLoaded('user')),
45            'observation' => $this->observation
46        ];
47
48    }
49
50    private function convertToResource($transferable): StockOIResource|AddressPrintResource|null
51    {
52        if ($transferable instanceof Address) {
53            return AddressPrintResource::make($transferable);
54        } elseif ($transferable instanceof StockIO) {
55            return StockOIResource::make($transferable);
56        }
57
58        return null;
59    }
60
61}