WithPerPagePagination::applyPagination()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Traits;
6
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Database\Eloquent\Builder;
9
use Livewire\WithPagination;
10
use Psr\Container\ContainerExceptionInterface;
11
use Psr\Container\NotFoundExceptionInterface;
12
13
trait WithPerPagePagination
14
{
15
    use WithPagination;
16
17
    /**
18
     * Assign the perPage option from the session.
19
     *
20
     * @return void
21
     *
22
     * @throws ContainerExceptionInterface
23
     * @throws NotFoundExceptionInterface
24
     */
25
    public function mountWithPerPagePagination(): void
26
    {
27
        $this->perPage = session()->get('perPage', $this->perPage);
0 ignored issues
show
Bug Best Practice introduced by
The property perPage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
    }
29
30
    /**
31
     * Store in session the perPage option.
32
     *
33
     * @param mixed $value The value of the option perPage.
34
     *
35
     * @return void
36
     */
37
    public function updatedPerPage(mixed $value): void
38
    {
39
        session()->put('perPage', $value);
40
    }
41
42
    /**
43
     * Apply the pagination to the query.
44
     *
45
     * @param Builder $query The query to apply the pagination.
46
     *
47
     * @return LengthAwarePaginator
48
     */
49
    public function applyPagination(Builder $query): LengthAwarePaginator
50
    {
51
        return $query->paginate($this->perPage);
52
    }
53
}
54