ListPostResource   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 27
rs 10
c 2
b 0
f 1
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B toArray() 0 20 8
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\Post;
4
5
use CSlant\Blog\Api\Http\Resources\Author\AuthorResource;
6
use CSlant\Blog\Api\Http\Resources\Category\CategoryResource;
7
use CSlant\Blog\Api\Http\Resources\Tag\TagResource;
8
use CSlant\Blog\Core\Facades\Base\Media\RvMedia;
9
use CSlant\Blog\Core\Models\Post;
10
use CSlant\Blog\Core\Models\Slug;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\Resources\Json\JsonResource;
13
use Illuminate\Support\Facades\Auth;
14
15
/**
16
 * @mixin Post
17
 */
18
class ListPostResource extends JsonResource
19
{
20
    /**
21
     * @param  Request  $request
22
     *
23
     * @return array<string, mixed>
24
     */
25
    public function toArray($request): array
26
    {
27
        $userId = Auth::user() ? Auth::user()->id : 0;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
28
29
        /** @var Post $this */
30
        return [
31
            'id' => $this->id,
32
            'name' => $this->name,
33
            'slug' => $this->slug instanceof Slug ? $this->slug->key : $this->slug,
0 ignored issues
show
introduced by
$this->slug is always a sub-type of CSlant\Blog\Core\Models\Slug.
Loading history...
34
            'description' => $this->description,
35
            'image' => $this->image ? RvMedia::url($this->image) : null,
36
            'categories' => CategoryResource::collection($this->categories),
37
            'tags' => TagResource::collection($this->tags),
38
            'author' => AuthorResource::make($this->author),
39
            'likes_count' => $this->relationLoaded('likes') ? $this->likesCountDigital() : 0,
40
            'comments_count' => $this->relationLoaded('comments') ? $this->comments()->count() : 0,
41
            'is_liked' => $this->relationLoaded('likes') && $this->isLikedBy($userId),
42
            'is_commented' => $this->relationLoaded('comments') && $this->isCommentBy($userId),
43
            'created_at' => $this->created_at,
44
            'updated_at' => $this->updated_at,
45
        ];
46
    }
47
}
48