Completed
Push — master ( 69097b...4e1c03 )
by Danilo
04:33
created

Paginator::paginateItems()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 35
cp 0
rs 6.8493
c 0
b 0
f 0
cc 8
eloc 31
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
use PhpBotFramework\Entities\InlineKeyboard as Keyboard;
22
23
define("DELIMITER", "::::::::::::::::::::::::::::::::::::::\n");
24
25
/**
26
 * \class Paginator
27
 * \brief Delimitate items in one page.
28
 */
29
class Paginator
30
{
31
    /**
32
     * \addtogroup Utility-classes Utility classes
33
     * @{
34
     */
35
36
    /**
37
     * \brief Paginate a number of items got as a result by a query.
38
     * \details Take items to show in the page $index, delimiting in by $delimiter, and call the closure $format_item on each item paginated.
39
     * Taking a select query result, take items that have to be shown on page of index $index (calculated with $item_per_page).
40
     * @param mixed $items Result of a select query using pdo object.
41
     * @param int $index Index of the page to show.
42
     * @param PhpBotFramework\Entities\InlineKeyboard $keyboard Inline keyboard object to call PhpBotFramework\\Entities\\InlineKeyboard::addListKeyboard() on it for browsing results.
43
     * @param closure $format_item A closure that take the item to paginate and the keyboard. You can use it to format the item and add some inline keyboard button for each item.
44
     * @param string $prefix Prefix to pass to InlineKeyboard::addListKeyboard.
45
     * @param string $delimiter A string that will be used to concatenate each item.
46
     * @return string The string message with the items of page $index, with $delimiter between each of them.
47
     * @see PhpBotFramework\\Entities\\InlineKeyboard
48
     */
49
    public static function paginateItems(
50
        $items,
51
        int $index,
52
        Keyboard &$keyboard,
53
        callable $format_item,
54
        int $item_per_page = 3,
55
        string $prefix = 'list',
56
        string $delimiter = DELIMITER
57
    ) : string {
58
        // Assign the position of first item to show
59
        $item_position = ($index - 1) * $item_per_page + 1;
60
61
        $items_number = $items->rowCount();
62
63
        $counter = 1;
64
65
        $items_displayed = 0;
66
67
        $total_pages = intval($items_number / $item_per_page);
68
69
        // If there an incomplete page
70
        if (($items_number % $item_per_page) != 0) {
71
            $total_pages++;
72
        }
73
74
        // Initialize keyboard with the list
75
        $keyboard->addListKeyboard($index, $total_pages, $prefix);
76
77
        $message = '';
78
79
        // Iterate over all results
80
        while ($item = $items->fetch()) {
81
            // If we have to display the first item of the page and we
82
            // found the item to show (using the position calculated before)
83
            if ($items_displayed === 0 && $counter === $item_position) {
84
                $message .= $format_item($item, $keyboard);
85
                $items_displayed++;
86
            // If there are space for other items
87
            } elseif ($items_displayed > 0 && $items_displayed < $item_per_page) {
88
                $message .= $delimiter;
89
                $message .= $format_item($item, $keyboard);
90
91
                $items_displayed++;
92
            } elseif ($items_displayed === $item_per_page) {
93
                break;
94
            } else {
95
                $counter++;
96
            }
97
        }
98
99
        return $message;
100
    }
101
}
102
103
/*
104
 * @}
105
 */
106