Passed
Pull Request — main (#82)
by
unknown
04:19 queued 01:19
created

ListPostResource   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 29
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B toArray() 0 22 7
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 = 0;
28
        if (Auth::user()) {
29
            $userId = Auth::user()->id;
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...
30
        }
31
32
        /** @var Post $this */
33
        return [
34
            'id' => $this->id,
35
            'name' => $this->name,
36
            '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...
37
            'description' => $this->description,
38
            'image' => $this->image ? RvMedia::url($this->image) : null,
39
            'categories' => CategoryResource::collection($this->categories),
40
            'tags' => TagResource::collection($this->tags),
41
            'author' => AuthorResource::make($this->author),
42
            'likes_count' => $this->relationLoaded('likes') ? $this->likesCountDigital() : 0,
43
            'is_interacted_by' => $this->relationLoaded('likes') ? $this->isInteractedBy($userId, null) : false,
44
            'is_interacted_comment_by' => $this->relationLoaded('comments') ? $this->isInteractedCommentBy($userId) : false,
45
            'created_at' => $this->created_at,
46
            'updated_at' => $this->updated_at,
47
        ];
48
    }
49
}
50