Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

CommentCollection::setOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Resources;
4
5
use Coyote\Job;
6
use Coyote\Guide;
7
use Illuminate\Http\Resources\Json\ResourceCollection;
8
9
class CommentCollection extends ResourceCollection
10
{
11
    /**
12
     * DO NOT REMOVE! This will preserver keys from being filtered in data
13
     *
14
     * @var bool
15
     */
16
    protected $preserveKeys = true;
17
18
    protected Guide|Job $owner;
19
20
    public function setOwner(Guide|Job $owner): static
21
    {
22
        $this->owner = $owner;
23
24
        return $this;
25
    }
26
27
    /**
28
     * Transform the resource collection into an array.
29
     *
30
     * @param  \Illuminate\Http\Request  $request
31
     * @return array
32
     */
33
    public function toArray($request)
34
    {
35
        $owner = (clone $this->owner)->unsetRelations();
36
37
        return $this
38
            ->collection
39
            ->map(function (CommentResource $resource) use ($request, $owner) {
0 ignored issues
show
Unused Code introduced by
The import $request is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
40
                $comment = $resource->resource;
41
                $comment->setRelation('resource', $owner);
42
43
                foreach ($comment->children as $child) {
44
                    $child->setRelation('resource', $owner);
45
                }
46
47
                return $resource;
48
            })
49
            ->keyBy('id')
50
            ->toArray();
51
    }
52
}
53