Finder::fetchAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Builder 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...
6
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model 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...
7
use Illuminate\Http\Request;
8
use Illuminate\Pagination\LengthAwarePaginator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Pagination\LengthAwarePaginator 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...
9
use Terranet\Administrator\Contracts\Module;
10
use Terranet\Administrator\Contracts\Services\Finder as FinderContract;
11
use Terranet\Administrator\Exception;
12
use Terranet\Administrator\Filters\Assembler;
13
14
class Finder implements FinderContract
15
{
16
    /** @var Module */
17
    protected $module;
18
19
    /** @var Model */
20
    protected $model;
21
22
    /** @var Builder */
23
    protected $query;
24
25
    /** @var Assembler */
26
    protected $assembler;
27
28
    /** @var Request */
29
    protected $request;
30
31
    /**
32
     * Finder constructor.
33
     *
34
     * @param  Module  $module
35
     */
36
    public function __construct(Module $module)
37
    {
38
        $this->module = $module;
39
        $this->model = $module->model();
40
        $this->request = $module->request();
0 ignored issues
show
Bug introduced by
The method request() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        /** @scrutinizer ignore-call */ 
41
        $this->request = $module->request();
Loading history...
41
    }
42
43
    /**
44
     * Fetch all items from repository.
45
     *
46
     * @return mixed
47
     * @throws Exception
48
     */
49
    public function fetchAll()
50
    {
51
        if ($builder = $this->getQuery()) {
52
            return $builder->paginate($this->perPage());
53
        }
54
55
        return new LengthAwarePaginator([], 0, 10, 1);
56
    }
57
58
    /**
59
     * Build Scaffolding Index page query.
60
     *
61
     * @return Builder
62
     * @throws Exception
63
     */
64
    public function getQuery(): Builder
65
    {
66
        // prevent duplicated execution
67
        if (null === $this->query && $this->model) {
68
            $this->initQuery()
69
                ->applyFilters()
70
                ->applyRelationalFilters()
71
                ->applySorting();
72
73
            $this->query = $this->assembler()->getQuery();
74
        }
75
76
        return $this->query;
77
    }
78
79
    /**
80
     * Find a record by id or fail.
81
     *
82
     * @param       $key
83
     * @param  array  $columns
84
     * @return mixed
85
     */
86
    public function find($key, $columns = ['*'])
87
    {
88
        return $this->model = once(function () use ($key, $columns) {
89
            return $this->model->newQueryWithoutScopes()->findOrFail($key, $columns);
90
        });
91
    }
92
93
    /**
94
     * Get the query assembler object.
95
     *
96
     * @return Assembler
97
     */
98
    protected function assembler(): Assembler
99
    {
100
        if (null === $this->assembler) {
101
            $this->assembler = (new Assembler($this->model));
102
        }
103
104
        return $this->assembler;
105
    }
106
107
    /**
108
     * @return $this
109
     */
110
    protected function initQuery(): FinderContract
111
    {
112
        if (method_exists($this->module, 'query')) {
113
            $this->assembler()->applyQueryCallback([$this->module, 'query']);
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * Apply query string filters.
121
     *
122
     * @return FinderContract
123
     * @throws Exception
124
     */
125
    protected function applyFilters(): FinderContract
126
    {
127
        if (!$filter = $this->module->filter()) {
128
            return $this;
129
        }
130
131
        if ($filters = $filter->filters()) {
132
            $this->assembler()->filters($filters);
133
        }
134
135
        if (empty($scopes = $filter->scopes())) {
136
            return $this;
137
        }
138
139
        $scope = $filter->scope();
0 ignored issues
show
Bug introduced by
The method scope() does not exist on Terranet\Administrator\Contracts\Filter. Did you maybe mean scopes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        /** @scrutinizer ignore-call */ 
140
        $scope = $filter->scope();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
        if ($scope && $found = $scopes->find($scope)) {
141
            $this->assembler()->scope($found);
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * Apply relation filters: cross-links to other
149
     *
150
     * @return $this
151
     */
152
    protected function applyRelationalFilters(): FinderContract
153
    {
154
        if (($relation = request('viaResource')) && ($value = request('viaResourceId'))) {
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
        if (($relation = /** @scrutinizer ignore-call */ request('viaResource')) && ($value = request('viaResourceId'))) {
Loading history...
155
            $this->assembler()->relations($relation, $value);
156
        }
157
158
        return $this;
159
    }
160
161
    /**
162
     * Extend query with Order By Statement.
163
     *
164
     * @return FinderContract
165
     * @throws \Exception
166
     */
167
    protected function applySorting(): FinderContract
168
    {
169
        /** @var Sorter $sortable */
170
        $sortable = $this->module->sortableManager();
0 ignored issues
show
Bug introduced by
The method sortableManager() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
        /** @scrutinizer ignore-call */ 
171
        $sortable = $this->module->sortableManager();
Loading history...
171
172
        $element = $sortable->element();
173
        $direction = $sortable->direction();
174
175
        if ($element && $direction && is_string($element)) {
176
            $this->assembler()->sort($element, $direction);
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * Items per page.
184
     *
185
     * @return int
186
     */
187
    protected function perPage(): int
188
    {
189
        return method_exists($this->module, 'perPage')
190
            ? $this->module->perPage()
191
            : 20;
192
    }
193
}
194