Completed
Push — master ( 5e61a0...2e1bfd )
by Michael
04:48 queued 02:16
created

RequestResourceContext   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 23
c 3
b 0
f 1
dl 0
loc 65
ccs 30
cts 30
cp 1
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 12 1
A sortBy() 0 5 1
A paginate() 0 3 1
A with() 0 5 2
A user() 0 3 1
A perPage() 0 3 1
A __construct() 0 3 1
A page() 0 3 1
A filters() 0 3 1
1
<?php
2
3
namespace Mblarsen\LaravelRepository;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Arr;
8
9
class RequestResourceContext implements ResourceContext
10
{
11
    /** @var Request */
12
    protected $request;
13
14 49
    public function __construct()
15
    {
16 49
        $this->request = request();
17 49
    }
18
19 15
    public function filters(): array
20
    {
21 15
        return $this->request->get('filters', []);
22
    }
23
24 14
    public function page(): int
25
    {
26 14
        return $this->request->get('page', 1);
27
    }
28
29 14
    public function perPage(): int
30
    {
31 14
        return $this->request->get('per_page', 15);
32
    }
33
34 14
    public function paginate(): bool
35
    {
36 14
        return $this->request->has('page');
37
    }
38
39 15
    public function sortBy(): array
40
    {
41
        return [
42 15
            $this->request->get('sort_by', null),
43 15
            $this->request->get('sort_order', null),
44
        ];
45
    }
46
47 2
    public function user(): ?Model
48
    {
49 2
        return $this->request->user();
50
    }
51
52 17
    public function with(): array
53
    {
54 17
        return $this->request->has('with')
55 3
            ? Arr::wrap($this->request->get('with'))
56 17
            : [];
57
    }
58
59
    /**
60
     * Convert the request context to an array
61
     */
62 2
    public function toArray(): array
63
    {
64 2
        [$sort_by, $sort_order] = $this->sortBy();
65
        return [
66 2
            'filters' => $this->filters(),
67 2
            'page' => $this->page(),
68 2
            'paginate' => $this->paginate(),
69 2
            'per_page' => $this->perPage(),
70 2
            'sort_by' => $sort_by,
71 2
            'sort_order' => $sort_order,
72 2
            'user' => $this->user(),
73 2
            'with' => $this->with(),
74
        ];
75
    }
76
}
77