Passed
Pull Request — main (#66)
by
unknown
02:34
created

AuthorService   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAllAuthor() 0 14 1
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
6
use CSlant\Blog\Core\Models\User;
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Arr;
10
11
/**
12
 * Class AuthorService
13
 *
14
 * @package CSlant\Blog\Api\Services
15
 *
16
 * @method BaseHttpResponse httpResponse()
17
 */
18
class AuthorService
19
{
20
    /**
21
     * Get all author.
22
     *
23
     * @param  Request  $request
24
     *
25
     * @return LengthAwarePaginator<User>
26
     */
27
    public function getAllAuthor(Request $request): LengthAwarePaginator
28
    {
29
        $data = User::query()
30
            ->withCount('posts');  //Eloquent method
31
32
        $orderBy = (string) Arr::get($request->toArray(), 'order_by', 'posts_count');
33
        $order = (string) Arr::get($request->toArray(), 'order', 'desc');
34
35
        $data = $data->orderBy($orderBy, $order);
36
37
        /** @var LengthAwarePaginator<User> $result */
38
        $result = $data->paginate($request->integer('per_page', 10));
39
40
        return $result;
41
    }
42
}
43