Passed
Push — develop ( 08926b...fc8040 )
by Brent
03:01
created

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