Passed
Push — main ( 572954...b4775f )
by Tan
03:35 queued 51s
created

AuthorService   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAllAuthor() 0 11 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\Database\Eloquent\Model;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class AuthorService
14
 *
15
 * @package CSlant\Blog\Api\Services
16
 *
17
 * @method BaseHttpResponse httpResponse()
18
 */
19
class AuthorService
20
{
21
    /**
22
     * Get all author.
23
     *
24
     * @param  Request  $request
25
     *
26
     * @return LengthAwarePaginator<Model>
27
     */
28
    public function getAllAuthor(Request $request): LengthAwarePaginator
29
    {
30
        $data = User::query()
31
            ->withCount('posts');  //Eloquent method
32
33
        $orderBy = (string) Arr::get($request->toArray(), 'order_by', 'posts_count');
34
        $order = (string) Arr::get($request->toArray(), 'order', 'desc');
35
36
        $data = $data->orderBy($orderBy, $order);
37
38
        return $data->paginate($request->integer('per_page', 10));
39
    }
40
}
41