JsonApiPaginateServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 22
c 5
b 1
f 1
dl 0
loc 41
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 2
A register() 0 3 1
A registerMacro() 0 23 3
1
<?php
2
3
namespace Spatie\JsonApiPaginate;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\ServiceProvider;
8
9
class JsonApiPaginateServiceProvider extends ServiceProvider
10
{
11
    public function boot()
12
    {
13
        if ($this->app->runningInConsole()) {
14
            $this->publishes([
15
                __DIR__.'/../config/json-api-paginate.php' => config_path('json-api-paginate.php'),
16
            ], 'config');
17
        }
18
19
        $this->registerMacro();
20
    }
21
22
    public function register()
23
    {
24
        $this->mergeConfigFrom(__DIR__.'/../config/json-api-paginate.php', 'json-api-paginate');
25
    }
26
27
    protected function registerMacro()
28
    {
29
        Builder::macro(config('json-api-paginate.method_name'), function (int $maxResults = null, int $defaultSize = null) {
30
            $maxResults = $maxResults ?? config('json-api-paginate.max_results');
31
            $defaultSize = $defaultSize ?? config('json-api-paginate.default_size');
32
            $numberParameter = config('json-api-paginate.number_parameter');
33
            $sizeParameter = config('json-api-paginate.size_parameter');
34
            $paginationParameter = config('json-api-paginate.pagination_parameter');
35
36
            $size = (int) request()->input($paginationParameter.'.'.$sizeParameter, $defaultSize);
37
38
            $size = $size > $maxResults ? $maxResults : $size;
39
40
            $paginator = $this
41
                ->paginate($size, ['*'], $paginationParameter.'.'.$numberParameter)
0 ignored issues
show
Bug introduced by
The method paginate() does not exist on Spatie\JsonApiPaginate\J...PaginateServiceProvider. ( Ignorable by Annotation )

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

41
                ->/** @scrutinizer ignore-call */ paginate($size, ['*'], $paginationParameter.'.'.$numberParameter)

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...
42
                ->setPageName($paginationParameter.'['.$numberParameter.']')
43
                ->appends(Arr::except(request()->input(), $paginationParameter.'.'.$numberParameter));
44
45
            if (! is_null(config('json-api-paginate.base_url'))) {
46
                $paginator->setPath(config('json-api-paginate.base_url'));
47
            }
48
49
            return $paginator;
50
        });
51
    }
52
}
53