Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ItemResource | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| toArray | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Item\Resource; |
| 4 | |
| 5 | use App\Models\Transferable\Transferable; |
| 6 | use Domain\Integration\Resources\IntegrationResource; |
| 7 | use Domain\Product\Resources\ProductResource; |
| 8 | use Domain\Storage\Resources\StorageResource; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\Http\Resources\Json\JsonResource; |
| 11 | |
| 12 | /** |
| 13 | * @property float $amount |
| 14 | * @property int $id |
| 15 | * @property string $created_at |
| 16 | * @property string $updated_at |
| 17 | */ |
| 18 | class ItemResource extends JsonResource |
| 19 | { |
| 20 | /** |
| 21 | * @param Request $request |
| 22 | * @return array |
| 23 | */ |
| 24 | public function toArray(Request $request): array |
| 25 | { |
| 26 | $transferable_id = $this->transferable_id; |
| 27 | |
| 28 | if (is_null($transferable_id)) { |
| 29 | $owner = $this->whenLoaded('itemable'); |
| 30 | |
| 31 | if ($owner && method_exists($owner, 'relationLoaded') && $owner->relationLoaded('transferable')) { |
| 32 | $transferable_id = $owner->transferable?->id; |
| 33 | } else { |
| 34 | $transferable = Transferable::query() |
| 35 | ->where('transferable_id', $this->itemable_id) |
| 36 | ->where('transferable_type', $this->itemable_type) |
| 37 | ->select('id') |
| 38 | ->first(); |
| 39 | |
| 40 | $transferable_id = $transferable?->id; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $pickedItemAmount = $this->relationLoaded('pickedItems') |
| 45 | ? $this->pickedItems->sum('amount') |
| 46 | : $this->pickedItems()->sum('amount'); |
| 47 | |
| 48 | return [ |
| 49 | 'id' => $this->id, |
| 50 | 'storage' => StorageResource::make($this->whenLoaded('storage')), |
| 51 | 'product' => ProductResource::make($this->whenLoaded('product')), |
| 52 | 'amount' => $this->amount, |
| 53 | 'transferable_id' => $transferable_id, |
| 54 | 'picked_item_amount' => $pickedItemAmount, |
| 55 | 'integration' => IntegrationResource::make($this->whenLoaded('integration')), |
| 56 | 'owner' => $this->whenLoaded('itemable'), |
| 57 | 'created_at' => (string)$this->created_at, |
| 58 | 'updated_at' => (string)$this->updated_at, |
| 59 | ]; |
| 60 | } |
| 61 | } |