Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
32 / 34
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransactionResource
94.12% covered (success)
94.12%
32 / 34
50.00% covered (danger)
50.00%
1 / 2
6.01
0.00% covered (danger)
0.00%
0 / 1
 toArray
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 convertToResource
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
5.09
1<?php
2
3namespace Domain\Transaction\Resources;
4
5use Carbon\Carbon;
6
7use Illuminate\Http\Request;
8use Illuminate\Http\Resources\Json\JsonResource;
9
10use App\Models\Address\Address;
11use App\Models\StockIO\StockIO;
12
13use Domain\Address\Resources\AddressResource;
14use Domain\StockIO\Enums\StockOutputReasonEnum;
15use Domain\StockIO\Resources\StockOIResource;
16use Domain\Transaction\Services\LocaleArtefactService;
17use Domain\User\Resources\UserSensitiveResource;
18
19/**
20 * @property array $item
21 * @property float $amount
22 * @property Carbon $created_at
23 * @property mixed $type
24 * @property int $reason
25 * @property int $from_transferable_id
26 * @property string $from_type
27 * @property int $to_transferable_id
28 * @property string $to_type
29 */
30class TransactionResource extends JsonResource
31{
32    public function toArray(Request $request): array
33    {
34        $fromTransferable = (
35            new LocaleArtefactService()
36        )->locale($this->from_transferable_id);
37
38        $toTransferable = (
39            new LocaleArtefactService()
40        )->locale($this->to_transferable_id);
41
42        $from = $this->convertToResource($fromTransferable->transferable) ?? $fromTransferable;
43        $to   = $this->convertToResource($toTransferable->transferable) ?? $toTransferable;
44
45        return [
46            'id'          => $this->id,
47            'type'        => $this->type,
48            'from'        => $from,
49            'to'          => $to,
50            'amount'      => $this->amount,
51            'reason'      => $this->reason,
52            'item'        => $this->item,
53            'created_at'  => (string)$this->created_at,
54            'user'        => UserSensitiveResource::make($this->whenLoaded('user')),
55            'observation' => $this->observation,
56            'sector'      => $this->sector
57        ];
58
59    }
60
61    private function convertToResource($transferable): JsonResource|null
62    {
63        if ($transferable instanceof Address) {
64            return AddressResource::make($transferable->loadMissing(['parent']));
65        } elseif ($transferable instanceof StockIO) {
66            if ($transferable->isOutput() && (int) $transferable->reason === StockOutputReasonEnum::REQUEST->value) {
67                return TransactionStockOutputRequestResource::make($transferable->loadMissing([
68                    'integration',
69                    'transferable',
70                    'request.requester',
71                    'request.costCenter.integration',
72                    'request.resourceCenter.integration',
73                ]));
74            }
75
76            return StockOIResource::make($transferable->loadMissing(['integration']));
77        }
78
79        return null;
80    }
81
82}