Completed
Push — master ( 640da7...eb7508 )
by Danilo
03:54
created

Paginator::paginateItems()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 0
cts 35
cp 0
rs 7.1199
c 0
b 0
f 0
cc 8
eloc 30
nc 10
nop 7
crap 72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Utilities;
20
21
define("DELIMITER", "::::::::::::::::::::::::::::::::::::::\n");
22
23
class Paginator
24
{
25
    public static function paginateItems(
26
        $items,
27
        int $index,
28
        &$keyboard,
29
        $format_item,
30
        int $item_per_page = 3,
31
        $prefix = 'list',
32
        string $delimiter = DELIMITER
33
    ) {
34
    
35
        // Assign the position of first item to show
36
        $item_position = ($index - 1) * $item_per_page + 1;
37
38
        $items_number = $items->rowCount();
39
40
        $counter = 1;
41
42
        $items_displayed = 0;
43
44
        $total_pages = intval($items_number / $item_per_page);
45
46
        // If there an incomplete page
47
        if (($items_number % $item_per_page) != 0) {
48
            $total_pages++;
49
        }
50
51
        // Initialize keyboard with the list
52
        $keyboard->addListKeyboard($index, $total_pages, $prefix);
53
54
        $message = '';
55
56
        // Iterate over all results
57
        while ($item = $items->fetch()) {
58
            // If we have to display the first item of the page and we
59
            // found the item to show (using the position calculated before)
60
            if ($items_displayed === 0 && $counter === $item_position) {
61
                $message .= $format_item($item, $keyboard);
62
                $items_displayed++;
63
            // If there are space for other items
64
            } elseif ($items_displayed > 0 && $items_displayed < $item_per_page) {
65
                $message .= $delimiter;
66
                $message .= $format_item($item, $keyboard);
67
68
                $items_displayed++;
69
            } elseif ($items_displayed === $item_per_page) {
70
                break;
71
            } else {
72
                $counter++;
73
            }
74
        }
75
76
        return $message;
77
    }
78
}
79