Completed
Pull Request — master (#379)
by
unknown
01:24
created

PaginatesQuery::getPageValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Contracts\Pagination\Paginator;
6
7
/**
8
 * PaginatesQuery provides enhanced query pagination mechanism.
9
 *
10
 * It allows control over page size via request parameter and pagination parameters setup via 'query-builder' config.
11
 *
12
 * @mixin \Spatie\QueryBuilder\QueryBuilder
13
 */
14
trait PaginatesQuery
15
{
16
    /**
17
     * Paginates the query.
18
     * @see \Illuminate\Database\Eloquent\Builder::paginate()
19
     *
20
     * @param  array|int|null  $perPage per page options, refer to {@see getPerPageValue()} for more details.
21
     * @param  array  $columns
22
     * @return \Illuminate\Contracts\Pagination\Paginator
23
     */
24
    public function pagination($perPage = null, $columns = ['*']) : Paginator
25
    {
26
        return $this->paginate($this->getPerPageValue($perPage), $columns, $this->getPageParameterName(), $this->getPageValue());
0 ignored issues
show
Bug introduced by
It seems like paginate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
    }
28
29
    /**
30
     * Paginate the query into a simple paginator.
31
     * @see \Illuminate\Database\Eloquent\Builder::simplePaginate()
32
     *
33
     * @param  array|int|null  $perPage per page options, refer to {@see getPerPageValue()} for more details.
34
     * @param array $columns
35
     * @return \Illuminate\Contracts\Pagination\Paginator
36
     */
37
    public function simplePagination($perPage = null, $columns = ['*']) : Paginator
38
    {
39
        return $this->simplePaginate($this->getPerPageValue($perPage), $columns, $this->getPageParameterName(), $this->getPageValue());
0 ignored issues
show
Bug introduced by
It seems like simplePaginate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    }
41
42
    /**
43
     * Extracts per page value from the related HTTP request.
44
     *
45
     * @param  array|int|null  $options if not set - use default options, if scalar - use it as default per page value.
46
     * If array - use as options with following keys:
47
     *
48
     * - 'default' - int, default per page value.
49
     * - 'min' - int, min allowed per page value.
50
     * - 'max' - int, max allowed per page value.
51
     *
52
     * @return int per page value.
53
     */
54
    protected function getPerPageValue($options = null): int
55
    {
56
        $defaultOptions = [
57
            'default' => config('query-builder.pagination.per-page.default', 15),
58
            'min' => config('query-builder.pagination.per-page.min', 1),
59
            'max' => config('query-builder.pagination.per-page.max', 50),
60
        ];
61
62
        if (is_array($options)) {
63
            $options = array_merge($defaultOptions, $options);
64
        } elseif ($options === null) {
65
            $options = $defaultOptions;
66
        } else {
67
            $options = array_merge($defaultOptions, ['default' => $options]);
68
        }
69
70
        $options = array_map(function ($value) {
71
            return (int) $value;
72
        }, $options);
73
74
        $perPageParameterName = config('query-builder.parameters.per-page', 'per-page');
75
76
        $perPage = (int) $this->request->query($perPageParameterName, $options['default']);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77
78
        if ($perPage < $options['min']) {
79
            return $options['min'];
80
        }
81
82
        if ($perPage > $options['max']) {
83
            return $options['max'];
84
        }
85
86
        return $perPage;
87
    }
88
89
    /**
90
     * @return string HTTP query parameter name for page number.
91
     */
92
    protected function getPageParameterName() : string
93
    {
94
        return config('query-builder.parameters.page', 'page');
95
    }
96
97
    /**
98
     * @return int|null page number passed within associated HTTP request.
99
     */
100
    protected function getPageValue()
101
    {
102
        return $this->request->query($this->getPageParameterName());
103
    }
104
}
105