Passed
Push — master ( 29bd61...08926b )
by Brent
06:03
created

PaginationAdapter::transformPage()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 36
nc 14
nop 2
dl 0
loc 53
rs 7.1199
c 0
b 0
f 0

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
namespace Brendt\Stitcher\Adapter;
4
5
use Brendt\Stitcher\Factory\AdapterFactory;
6
use Brendt\Stitcher\Site\Page;
7
8
/**
9
 * The PaginationAdapter takes a page with a collection of entries and generates pagination for that collection.
10
 *
11
 * Sample configuration:
12
 *
13
 *  /examples:
14
 *      template: examples/overview
15
 *      data:
16
 *          collection: collection.yml
17
 *      adapters:
18
 *      pagination:
19
 *          variable: collection
20
 *          entriesPerPage: 4
21
 */
22
class PaginationAdapter extends AbstractAdapter
23
{
24
25
    /**
26
     * @param Page $page
27
     * @param null $filter
28
     *
29
     * @return Page[]
30
     */
31
    public function transformPage(Page $page, $filter = null) : array {
32
        $config = $page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER);
33
34
        if (!isset($config['variable'])) {
35
            return [$page];
36
        }
37
38
        $variable = $config['variable'];
39
40
        if (!$source = $page->getVariable($variable)) {
41
            throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\"");
42
        }
43
44
        $pageId = rtrim($page->getId(), '/');
45
        $entries = $this->getData($source);
46
        $entriesPerPage = isset($config['entriesPerPage']) ? $config['entriesPerPage'] : 10;
47
        $pageCount = (int) ceil(count($entries) / $entriesPerPage);
48
49
        $i = 0;
50
        $result = [];
51
52
        while ($i < $pageCount) {
53
            $pageEntries = array_splice($entries, 0, $entriesPerPage);
54
            $pageIndex = $i + 1;
55
56
            if ($filter && $pageIndex !== (int) $filter) {
57
                $i += 1;
58
                continue;
59
            }
60
61
            $url = "{$pageId}/page-{$pageIndex}";
62
            $pagination = $this->createPagination($pageId, $pageIndex, $pageCount, $entries);
63
            $entriesPage = clone $page;
64
            $entriesPage
65
                ->removeAdapter(AdapterFactory::PAGINATION_ADAPTER)
66
                ->setVariableValue($variable, $pageEntries)
67
                ->setVariableIsParsed($variable)
68
                ->setVariableValue('pagination', $pagination)
69
                ->setVariableIsParsed('pagination')
70
                ->setId($url);
71
72
            $result[$url] = $entriesPage;
73
            $i += 1;
74
        }
75
76
        if ($firstPage = reset($result)) {
77
            $mainPage = clone $firstPage;
78
            $mainPage->setId($pageId);
79
            $result[$pageId] = $mainPage;
80
        }
81
82
        return $result;
83
    }
84
85
    /**
86
     * Create a pagination array.
87
     *
88
     * @param $pageId
89
     * @param $pageIndex
90
     * @param $pageCount
91
     * @param $entries
92
     *
93
     * @return array
94
     */
95
    protected function createPagination($pageId, $pageIndex, $pageCount, $entries) {
96
        $next = count($entries) ? $pageIndex + 1 : null;
97
        $nextUrl = $next ? "{$pageId}/page-{$next}" : null;
98
        $previous = $pageIndex > 1 ? $pageIndex - 1 : null;
99
        $previousUrl = $previous ? "{$pageId}/page-{$previous}" : null;
100
101
        return [
102
            'current'  => $pageIndex,
103
            'previous' => $previous ? [
104
                'url'   => $previousUrl,
105
                'index' => $previous,
106
            ] : null,
107
            'next'     => $next ? [
108
                'url'   => $nextUrl,
109
                'index' => $next,
110
            ] : null,
111
            'pages'    => $pageCount,
112
        ];
113
    }
114
}
115