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

GuideResource   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultRole() 0 7 2
A toArray() 0 34 2
1
<?php
2
3
namespace Coyote\Http\Resources;
4
5
use Coyote\Comment;
6
use Coyote\Services\UrlBuilder;
7
use Illuminate\Http\Resources\Json\JsonResource;
8
9
/**
10
 * @property string $title
11
 * @property ?string $text
12
 * @property ?string $excerpt
13
 * @property string $slug
14
 * @property \Coyote\User $user
15
 * @property \Coyote\Tag[] $tags
16
 * @property Comment[] $commentsWithChildren
17
 * @property Comment[] $comments
18
 * @property int $comments_count
19
 * @property \Coyote\Guide\Vote[]|\Illuminate\Support\Collection $voters[]
20
 * @property \Coyote\Guide\Role[]|\Illuminate\Support\Collection $roles[]
21
 * @property \Coyote\Models\Subscription[]|\Illuminate\Support\Collection $subscribers
22
 * @property string $role
23
 * @property \Coyote\Models\Asset $assets
24
 */
25
class GuideResource extends JsonResource
26
{
27
    /**
28
     * Transform the resource into an array.
29
     *
30
     * @param  \Illuminate\Http\Request  $request
31
     * @return array
32
     */
33
    public function toArray($request)
34
    {
35
        $user = $request->user();
36
37
        return array_merge(
38
            $this->resource->only(['id', 'title', 'created_at', 'votes', 'views']),
39
            [
40
                'slug'          => $this->slug,
41
                'url'           => UrlBuilder::guide($this->resource),
42
43
                'user'          => new UserResource($this->user),
44
                'tags'          => TagResource::collection($this->tags),
45
                'permissions'   => [
46
                    'update'    => $request->user()?->can('update', $this->resource)
47
                ],
48
49
                'comments_count'=> $this->comments_count,
50
                'subscribers'   => $this->subscribers()->count(),
0 ignored issues
show
Bug introduced by
The method subscribers() does not exist on Coyote\Http\Resources\GuideResource. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                'subscribers'   => $this->/** @scrutinizer ignore-call */ subscribers()->count(),
Loading history...
51
52
                $this->mergeWhen($this->text || $this->excerpt, function () {
53
                    $parser = resolve('parser.post');
54
55
                    return [
56
                        'text'          => $this->text,
57
                        'html'          => $parser->parse((string) $this->text),
58
                        'excerpt_html'  => $parser->parse((string) $this->excerpt)
59
                    ];
60
                }),
61
62
                'is_voted'          => $this->when($user, fn () => $this->voters->contains('user_id', $user->id), false),
0 ignored issues
show
Bug Best Practice introduced by
The property voters does not exist on Coyote\Http\Resources\GuideResource. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
                'is_subscribed'     => $this->when($user, fn () => $this->subscribers->contains('user_id', $user->id), false),
64
                'comments'          => $this->whenLoaded('commentsWithChildren', fn () => (new CommentCollection($this->commentsWithChildren))->setOwner($this->resource)),
65
                'assets'            => $this->whenLoaded('assets', fn () => AssetsResource::collection($this->assets), []),
66
                'role'              => $this->defaultRole()
67
            ]
68
        );
69
    }
70
71
    private function defaultRole(): string
72
    {
73
        if (!$this->resource->relationLoaded('roles')) {
74
            return $this->role;
75
        }
76
77
        return $this->roles[0]->role ?? $this->role;
0 ignored issues
show
Bug Best Practice introduced by
The property roles does not exist on Coyote\Http\Resources\GuideResource. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
    }
79
}
80