PaginatedCollection::paginate()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
rs 9.6111
cc 5
nc 4
nop 4
1
<?php
2
/*
3
* File:     PaginatedCollection.php
4
* Category: Collection
5
* Author:   M. Goldenbaum
6
* Created:  16.03.18 03:13
7
* Updated:  -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP\Support;
14
15
use Illuminate\Pagination\LengthAwarePaginator;
16
use Illuminate\Support\Collection;
17
use Illuminate\Pagination\Paginator;
18
19
/**
20
 * Class PaginatedCollection
21
 *
22
 * @package Webklex\PHPIMAP\Support
23
 */
24
class PaginatedCollection extends Collection {
25
26
    /**
27
     * Number of total entries
28
     *
29
     * @var int $total
30
     */
31
    protected $total;
32
33
    /**
34
     * Paginate the current collection.
35
     * @param int $per_page
36
     * @param int|null $page
37
     * @param string $page_name
38
     * @param boolean $prepaginated
39
     *
40
     * @return LengthAwarePaginator
41
     */
42
    public function paginate(int $per_page = 15, $page = null, string $page_name = 'page', bool $prepaginated = false): LengthAwarePaginator {
43
        $page = $page ?: Paginator::resolveCurrentPage($page_name);
44
45
        $total = $this->total ?: $this->count();
46
47
        $results = !$prepaginated && $total ? $this->forPage($page, $per_page)->toArray() : $this->all();
48
49
        return $this->paginator($results, $total, $per_page, $page, [
50
            'path'      => Paginator::resolveCurrentPath(),
51
            'pageName'  => $page_name,
52
        ]);
53
    }
54
55
    /**
56
     * Create a new length-aware paginator instance.
57
     * @param array $items
58
     * @param int $total
59
     * @param int $per_page
60
     * @param  int|null $current_page
61
     * @param  array    $options
62
     *
63
     * @return LengthAwarePaginator
64
     */
65
    protected function paginator(array $items, int $total, int $per_page, $current_page, array $options): LengthAwarePaginator {
66
        return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options);
67
    }
68
69
    /**
70
     * Get and set the total amount
71
     * @param null $total
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $total is correct as it would always require null to be passed?
Loading history...
72
     *
73
     * @return int|null
74
     */
75
    public function total($total = null) {
76
        if($total === null) {
0 ignored issues
show
introduced by
The condition $total === null is always true.
Loading history...
77
            return $this->total;
78
        }
79
80
        return $this->total = $total;
81
    }
82
}
83