Completed
Push — master ( 5bb2ae...adea1c )
by Terzi
04:11
created

Finder::perPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Pagination\LengthAwarePaginator;
7
use Terranet\Administrator;
8
use Terranet\Administrator\Contracts\Module;
9
use Terranet\Administrator\Contracts\Services\Finder as FinderContract;
10
use Terranet\Administrator\Filters\Assembler;
11
use Terranet\Administrator\Filters\InputFactory;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Filters\InputFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Terranet\Administrator\Form\FormElement;
0 ignored issues
show
Bug introduced by
The type Terranet\Administrator\Form\FormElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class Finder implements FinderContract
15
{
16
    /**
17
     * @var Module
18
     */
19
    protected $module;
20
21
    /**
22
     * @var
23
     */
24
    protected $model;
25
26
    /**
27
     * @var Builder
28
     */
29
    protected $query;
30
31
    /**
32
     * Query assembler.
33
     *
34
     * @var
35
     */
36
    protected $assembler;
37
38
    public function __construct(Module $module)
39
    {
40
        $this->module = $module;
41
        $this->model = $module->model();
42
    }
43
44
    /**
45
     * Fetch all items from repository.
46
     *
47
     * @return mixed
48
     */
49
    public function fetchAll()
50
    {
51
        if ($query = $this->getQuery()) {
52
            return $query->paginate($this->perPage());
53
        }
54
55
        return new LengthAwarePaginator([], 0, 10, 1);
56
    }
57
58
    /**
59
     * Build Scaffolding Index page query.
60
     *
61
     * @return mixed
62
     */
63
    public function getQuery()
64
    {
65
        // prevent duplicated execution
66
        if (null === $this->query && $this->model) {
67
            $this->initQuery()
68
                 ->applyFilters()
69
                 ->applySorting();
70
71
            $this->query = $this->assembler()->getQuery();
72
        }
73
74
        return $this->query;
75
    }
76
77
    /**
78
     * Find a record by id or fail.
79
     *
80
     * @param       $key
81
     * @param array $columns
82
     *
83
     * @return mixed
84
     */
85
    public function find($key, $columns = ['*'])
86
    {
87
        $this->model = $this->model->newQueryWithoutScopes()->findOrFail($key, $columns);
88
89
        return $this->model;
90
    }
91
92
    /**
93
     * Get the query assembler object.
94
     *
95
     * @return Assembler
96
     */
97
    protected function assembler()
98
    {
99
        if (null === $this->assembler) {
100
            $this->assembler = (new Assembler($this->model));
101
        }
102
103
        return $this->assembler;
104
    }
105
106
    protected function initQuery()
107
    {
108
        if (method_exists($this->module, 'query')) {
109
            $this->assembler()->applyQueryCallback([$this->module, 'query']);
110
        }
111
112
        return $this;
113
    }
114
115
    protected function applyFilters()
116
    {
117
        if ($filter = app('scaffold.filter')) {
118
            if ($filters = $filter->filters()) {
119
                $this->assembler()->filters($filters);
120
            }
121
122
            if ($scopes = $filter->scopes()) {
123
                if (($scope = $filter->scope()) && ($found = $scopes->find($scope))) {
124
                    $this->assembler()->scope(
125
                        $found
126
                    );
127
                }
128
            }
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Extend query with Order By Statement.
136
     */
137
    protected function applySorting()
138
    {
139
        $sortable = app('scaffold.sortable');
140
        $element = $sortable->element();
141
        $direction = $sortable->direction();
142
143
        if ($element && $direction) {
144
            if (\is_string($element)) {
145
                $this->assembler()->sort($element, $direction);
146
            }
147
        }
148
149
        return $this;
150
    }
151
152
    protected function perPage()
153
    {
154
        return method_exists($this->module, 'perPage')
155
            ? $this->module->perPage()
156
            : 20;
157
    }
158
}
159