Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemLocationResource
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 toArray
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Domain\Item\Resource;
4
5use App\Models\Transferable\Transferable;
6use Domain\Integration\Resources\IntegrationResource;
7use Domain\Product\Resources\ProductResource;
8use Domain\Storage\Resources\StorageResource;
9use Illuminate\Http\Request;
10use 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 */
18class ItemLocationResource 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            $transferable_id = Transferable::query()
30                ->where('transferable_id', $this->itemable_id)
31                ->where('transferable_type', $this->itemable_type)
32                ->select('id')
33                ->first();
34
35            $transferable_id = $transferable_id->id;
36        }
37
38        return [
39            'id' => $this->id,
40            'amount' => $this->amount,
41            'transferable_id' => $transferable_id,
42            'picked_item_amount' => $this->pickedItems->sum('amount'),
43            'integration' => IntegrationResource::make($this->whenLoaded('integration')),
44            'owner' => $this->whenLoaded('itemable'),
45            'created_at' => (string)$this->created_at,
46            'updated_at' => (string)$this->updated_at,
47        ];
48    }
49}