Passed
Push — master ( 1bc95e...277078 )
by Malte
03:34
created

PaginatedCollection::total()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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\IMAP\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\IMAP\Support
23
 */
24
class PaginatedCollection extends Collection {
25
26
    /** @var int $total */
27
    protected $total;
28
29
    /**
30
     * Paginate the current collection.
31
     *
32
     * @param int      $per_page
33
     * @param int|null $page
34
     * @param string   $page_name
35
     *
36
     * @return LengthAwarePaginator
37
     */
38
    public function paginate($per_page = 15, $page = null, $page_name = 'page') {
39
        $page = $page ?: Paginator::resolveCurrentPage($page_name);
40
41
        $total = $this->total ? $this->total : $this->count();
42
43
        $results = $total ? $this->forPage($page, $per_page) : $this->all();
44
45
        return $this->paginator($results, $total, $per_page, $page, [
0 ignored issues
show
Bug introduced by
It seems like $results can also be of type Webklex\IMAP\Support\PaginatedCollection; however, parameter $items of Webklex\IMAP\Support\Pag...Collection::paginator() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        return $this->paginator(/** @scrutinizer ignore-type */ $results, $total, $per_page, $page, [
Loading history...
46
            'path'      => Paginator::resolveCurrentPath(),
47
            'pageName'  => $page_name,
48
        ]);
49
    }
50
51
    /**
52
     * Create a new length-aware paginator instance.
53
     *
54
     * @param  array    $items
55
     * @param  int      $total
56
     * @param  int      $per_page
57
     * @param  int|null $current_page
58
     * @param  array    $options
59
     *
60
     * @return LengthAwarePaginator
61
     */
62
    protected function paginator($items, $total, $per_page, $current_page, array $options) {
63
        return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options);
64
    }
65
66
    /**
67
     * @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...
68
     *
69
     * @return int|null
70
     */
71
    public function total($total = null) {
72
        if($total === null) {
0 ignored issues
show
introduced by
The condition $total === null is always true.
Loading history...
73
            return $this->total;
74
        }
75
76
        return $this->total = $total;
77
    }
78
}