Passed
Push — master ( 9b6942...62d175 )
by Bruno
08:20
created

Pagination::render()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 96
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 59
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 96
rs 8.5833

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 declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vue\Element;
4
5
use Formularium\Element;
6
use Formularium\Exception\ClassNotFoundException;
7
use Formularium\HTMLNode;
8
use Formularium\Framework;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
12
class Pagination extends Element
13
{
14
    public function render(array $parameters, HTMLNode $previous): HTMLNode
15
    {
16
        $node = HTMLNode::factory(
17
            'nav',
18
            ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"],
19
            HTMLNode::factory(
20
                'ul',
21
                ['class' => 'formularium-pagination'],
22
                [
23
                    HTMLNode::factory(
24
                        'li',
25
                        [
26
                            "class" => "formularium-pagination-item",
27
                            "v-show" => "currentPage > pagesAround",
28
                            "@click" => "\$emit('page', 1)"
29
                        ],
30
                        HTMLNode::factory(
31
                            'a',
32
                            [
33
                                "class" => "formularium-pagination-link",
34
                                ":href" => "basePath + '/1'",
35
                                "@click.prevent" => ""
36
                            ],
37
                            "1",
38
                            true
39
                        )
40
                    ),
41
                    HTMLNode::factory(
42
                        'li',
43
                        [
44
                            "class" => "formularium-pagination-item",
45
                            "v-show" => "currentPage > pagesAround"
46
                        ],
47
                        "...",
48
                        true
49
                    ),
50
                    HTMLNode::factory(
51
                        'li',
52
                        [
53
                            "class" => "formularium-pagination-item",
54
                            "v-for" => "p in pages",
55
                            "v-bind:key" => "p.page",
56
                            "@click" => "\$emit('page', p.page)"
57
                        ],
58
                        [
59
                            HTMLNode::factory(
60
                                'a',
61
                                [
62
                                    "v-if" => "p.page == currentPage",
63
                                    "class" => ["formularium-pagination-link", "formularium-pagination-current"],
64
                                    ":href" => "basePath + '/' + p.page",
65
                                    "@click.prevent" => ""
66
                                ],
67
                                "{{p.page}}"
68
                            ),
69
                            HTMLNode::factory(
70
                                'a',
71
                                [
72
                                    "v-else" => null,
73
                                    "class" => "formularium-pagination-link",
74
                                    ":href" => "basePath + '/' + p.page",
75
                                    "@click.prevent" => ""
76
                                ],
77
                                "{{p.page}}"
78
                            ),
79
                        ]
80
                    ),
81
                    HTMLNode::factory(
82
                        'li',
83
                        [
84
                            "class" => "formularium-pagination-item",
85
                            "v-show" => "lastPage > currentPage + pagesAround",
86
                        ],
87
                        "..."
88
                    )
89
                ]
90
            )
91
        );
92
93
        foreach ($this->composer->getFrameworks() as $framework) {
94
            /**
95
             * @var Framework $framework
96
             */
97
            $f =$framework->getName();
98
            if ($f === 'HTML' || $f === 'Vue') {
99
                continue;
100
            }
101
            try {
102
                $element = $framework->getElement($this->getName());
103
                $node = $element->render($parameters, $node);
104
            } catch (ClassNotFoundException $e) {
105
                continue; // element default
106
            }
107
        }
108
109
        return $node;
110
    }
111
112
    public static function script(): string
113
    {
114
        return <<<EOF
115
export default {
116
    props: {
117
        basePath: {
118
            type: String,
119
            default: "/post"
120
        },
121
        currentPage: {
122
            type: Number,
123
            required: true
124
        },
125
        perPage: {
126
            type: Number,
127
            required: true
128
        },
129
        lastPage: {
130
            type: Number,
131
            required: true
132
        },
133
        total: {
134
            type: Number,
135
            required: true
136
        },
137
        pagesAround: {
138
            type: Number,
139
            default: 4
140
        },
141
    },
142
143
    computed: {
144
        pages() {
145
            let first = Math.max(1, this.currentPage - this.pagesAround);
146
            let last = Math.min(this.lastPage, this.currentPage + this.pagesAround);
147
            let pages = [];
148
            for (let i = first; i <= last; i++) {
149
                pages.push({page: i});
150
            }
151
            return pages;
152
        }
153
    }
154
}
155
EOF;
156
    }
157
158
    public static function getMetadata(): Metadata
159
    {
160
        return new Metadata(
161
            'Pagination',
162
            'Creates a pagination element',
163
            [
164
            ]
165
        );
166
    }
167
}
168