Passed
Push — master ( 9e4977...ad78b7 )
by Brent
02:46
created

PaginationAdapter::transform()   D

Complexity

Conditions 14
Paths 266

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 50
nc 266
nop 2
dl 0
loc 71
rs 4.1552
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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\element\Page;
6
use brendt\stitcher\factory\AdapterFactory;
7
8
class PaginationAdapter extends AbstractAdapter {
9
10
    /**
11
     * @param Page $page
12
     * @param null $filter
13
     *
14
     * @return Page[]
15
     */
16
    public function transform(Page $page, $filter = null) {
17
        $config = $page->getAdapter(AdapterFactory::PAGINATION_ADAPTER);
18
19
        if (!isset($config['variable'])) {
20
            return [$page];
21
        }
22
23
        $variable = $config['variable'];
24
25
        if (!$source = $page->getVariable($variable)) {
26
            throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\"");
27
        }
28
29
        $pageId = rtrim($page->getId(), '/');
30
        $entries = $this->getData($source);
31
        $amount = isset($config['amount']) ? $config['amount'] : 10;
32
        $pageCount = (int) ceil(count($entries) / $amount);
33
34
        $i = 0;
35
        $result = [];
36
37
        while ($i < $pageCount) {
38
            $pageEntries = array_splice($entries, 0, $amount);
39
            $pageIndex = $i + 1;
40
41
            if ($filter && $pageIndex !== (int) $filter) {
42
                $i += 1;
43
                continue;
44
            }
45
46
            $url = "{$pageId}/page-{$pageIndex}";
47
48
            $next = count($entries) ? $pageIndex + 1 : null;
49
            $nextUrl = $next ? "{$pageId}/page-{$next}" : null;
50
            $previous = $pageIndex > 1 ? $pageIndex - 1 : null;
51
            $previousUrl = $previous ? "{$pageId}/page-{$previous}" : null;
52
53
            $entriesPage = clone $page;
54
            $pagination = [
55
                'current'  => $pageIndex,
56
                'previous' => $previous ? [
57
                    'url'   => $previousUrl,
58
                    'index' => $previous,
59
                ] : null,
60
                'next'     => $next ? [
61
                    'url'   => $nextUrl,
62
                    'index' => $next,
63
                ] : null,
64
                'pages'    => $pageCount,
65
            ];
66
67
            $entriesPage
68
                ->clearAdapter(AdapterFactory::PAGINATION_ADAPTER)
69
                ->setVariable($variable, $pageEntries)
70
                ->setParsedField($variable)
71
                ->setVariable('pagination', $pagination)
72
                ->setParsedField('pagination')
73
                ->setId($url);
74
75
            $result[$url] = $entriesPage;
76
            $i += 1;
77
        }
78
79
        if ($firstPage = reset($result)) {
80
            $mainPage = clone $firstPage;
81
            $mainPage->setId($pageId);
82
            $result[$pageId] = $mainPage;
83
        }
84
85
        return $result;
86
    }
87
}
88