Passed
Pull Request — main (#62)
by Tan
03:17
created

AuthorWithPostResource   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 15 1
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\Author;
4
5
use CSlant\Blog\Api\Http\Resources\Post\ListPostResource;
6
use CSlant\Blog\Api\Http\Resources\Post\ListPostResourceCollection;
7
use CSlant\Blog\Core\Models\User;
8
use Illuminate\Http\Resources\Json\JsonResource;
9
10
/**
11
 * @mixin User
12
 */
13
class AuthorWithPostResource extends JsonResource
14
{
15
    /**
16
     * @param $request
17
     *
18
     * @return array<string, mixed>
19
     */
20
    public function toArray($request): array
21
    {
22
        /** @var User $this */
23
        $posts = $this->posts()
24
            ->orderBy($request->get('order_by', 'created_at'), $request->get('order', 'DESC'))
25
            ->paginate($request->get('per_page', 10));
26
27
        return [
28
            'id' => $this->id,
29
            'email' => $this->email,
30
            'first_name' => $this->first_name,
31
            'last_name' => $this->last_name,
32
            'username' => $this->username,
33
            'image' => $this->avatar_url,
34
            'posts' => ListPostResourceCollection::make($posts),
35
        ];
36
    }
37
}
38