PostResource   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
eloc 22
dl 0
loc 33
rs 10
c 7
b 1
f 2
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 26 4
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\Comment\ListCommentResourceCollection;
8
use CSlant\Blog\Api\Http\Resources\Tag\TagResource;
9
use CSlant\Blog\Core\Facades\Base\Media\RvMedia;
10
use CSlant\Blog\Core\Models\Post;
11
use CSlant\Blog\Core\Models\Slug;
12
use Illuminate\Http\Resources\Json\JsonResource;
13
use Illuminate\Support\Facades\Auth;
14
15
/**
16
 * @mixin Post
17
 */
18
class PostResource extends JsonResource
19
{
20
    /**
21
     * @param $request
22
     *
23
     * @return array<string, mixed>
24
     */
25
    public function toArray($request): array
26
    {
27
        /** @var Post $this */
28
        $comments = $this->comments()
29
            ->orderBy((string) $request->get('order_by', 'created_at'), (string) $request->get('order', 'DESC'))
30
            ->paginate($request->integer('per_page', 10));
31
32
        $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...
33
34
        return [
35
            'id' => $this->id,
36
            'name' => $this->name,
37
            '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...
38
            'description' => $this->description,
39
            'content' => $this->content,
40
            'image' => $this->image ? RvMedia::url($this->image) : null,
41
            'categories' => CategoryResource::collection($this->categories),
42
            'tags' => TagResource::collection($this->tags),
43
            'author' => AuthorResource::make($this->author),
44
            'comments' => ListCommentResourceCollection::make($comments),
45
            'likes_count' => $this->likesCountDigital(),
46
            'is_liked' => $this->isLikedBy($userId),
47
            'comments_count' => $this->comments()->count(),
48
            'is_commented' => $this->isCommentBy($userId),
49
            'created_at' => $this->created_at,
50
            'updated_at' => $this->updated_at,
51
        ];
52
    }
53
}
54