Completed
Push — master ( 7d5fc8...3fda6b )
by WEBEWEB
08:27
created

AbstractUITwigExtension::button()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 2.7855
cc 12
eloc 16
nc 2048
nop 8

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * This file is part of the adminbsb-material-design-bundle package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\AdminBSBMaterialDesignBundle\Twig\Extension\UI;
13
14
use WBW\Bundle\AdminBSBMaterialDesignBundle\Provider\Color\DefaultColorProvider;
15
use WBW\Bundle\AdminBSBMaterialDesignBundle\Twig\Extension\AbstractABSBMDTwigExtension;
16
use WBW\Bundle\BootstrapBundle\Navigation\NavigationInterface;
17
use WBW\Library\Core\Utility\StringUtility;
18
19
/**
20
 * Abstract UI Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\AdminBSBMaterialDesignBundle\Twig\Extension\UI
24
 * @abstract
25
 */
26
abstract class AbstractUITwigExtension extends AbstractABSBMDTwigExtension implements NavigationInterface {
27
28
    /**
29
     * Constructor.
30
     */
31
    final public function __construct() {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Displays a badge.
37
     *
38
     * @param string $content The badge content.
39
     * @param string $label The badge label.
40
     * @param boolean $large Large badge ?
41
     * @param string $class The badge class.
42
     * @param boolean $list List badge ?
43
     * @param string $link The badge link.
44
     * @return string Returns the badge.
45
     */
46
    final protected function badge($content, $label, $large, $class, $list = false, $link = false) {
47
48
        // Initialize the template.
49
        $template = '<button %attributes%>%content%<span class="badge">%label%</span></button>';
50
        if ($list) {
51
            $template = '<a class="list-group-item" href="%link%"><span %attributes%>%content%</span>%label%</a>';
52
        }
53
54
        // Initialize the attributes.
55
        $_attr = [];
56
57
        if (true === $list) {
58
            $_attr["class"] = ["badge", $class];
59
        } else {
60
            $_attr["class"]   = ["btn", $class, "btn-block", "waves-effect"];
61
            $_attr["class"][] = true === $large ? "btn-lg" : null;
62
            $_attr["type"]    = "button";
63
        }
64
65
        // Initialize the parameters.
66
        $_content = null !== $content ? $content : self::DEFAULT_CONTENT;
67
        $_label   = null !== $label ? $label : self::DEFAULT_CONTENT;
68
        $_link    = null !== $link ? $link : self::DEFAULT_HREF;
69
70
        // Return the HTML.
71
        return StringUtility::replace($template, ["%attributes%", "%content%", "%label%", "%link%"], [StringUtility::parseArray($_attr), $_content, $_label, $_link]);
72
    }
73
74
    /**
75
     * Displays a button.
76
     *
77
     * @param string $content The button content.
78
     * @param string $title The button title.
79
     * @param string $size The button size.
80
     * @param boolean $block Block button ?
81
     * @param booelan $disable Disable button ?
82
     * @param string $class The button class.
83
     * @param string $icon The button icon.
84
     * @param boolean $circle Circle button ?
85
     * @return string Returns the button.
86
     */
87
    final protected function button($content, $title, $size, $block, $disable, $class, $icon, $circle) {
88
89
        // Disable the parameters.
90
        $circle = null !== $content ? false : $circle;
91
        $style  = null !== $content ? "margin: -4px 2px 0; vertical-align: sub;" : "";
92
93
        // Initialize the template.
94
        $template = "<button %attributes%>%icon%%content%</button>";
95
96
        // Initialize the attributes.
97
        $_attr = [];
98
99
        $_attr["class"]       = ["btn", $class, "waves-effect"];
100
        $_attr["class"][]     = true === $block ? "btn-block" : null;
101
        $_attr["class"][]     = true === $circle ? "btn-circle" . ("lg" === $size ? "-lg" : "") . " waves-circle waves-float" : null;
102
        $_attr["class"][]     = true !== $circle && true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
103
        $_attr["title"]       = $title;
104
        $_attr["type"]        = "button";
105
        $_attr["data-toggle"] = null !== $title ? "tooltip" : null;
106
        $_attr["disabled"]    = true === $disable ? "disabled" : null;
107
108
        // Handle the parameters.
109
        $_content = null !== $content ? $content : self::DEFAULT_CONTENT;
110
        $_icon    = null !== $icon ? $this->icon($icon, $style) : "";
111
112
        // Return the HTML.
113
        return StringUtility::replace($template, ["%attributes%", "%icon%", "%content%"], [StringUtility::parseArray($_attr), $_icon, $_content]);
114
    }
115
116
    /**
117
     * Displays a color.
118
     *
119
     * @param string $name The color name.
120
     * @param string $code The color code.
121
     * @param string $output The output.
122
     * @return string Returns the color.
123
     */
124
    final protected function color($name, $code, $output) {
125
126
        // Initialize the parameters.
127
        $_name = $this->getColor($name, "");
128
        $_code = null !== $code ? $code : "500";
129
130
        // Return the HTML.
131
        return "hex" === $output ? DefaultColorProvider::getColors()[$_name][$_code] : "col-" . $_name;
132
    }
133
134
    /**
135
     * Displays an icon.
136
     *
137
     * @param string $name The icon name.
138
     * @param string $style The icon style.
139
     * @param string $class The icon class.
140
     * @return string Returns the icon.
141
     */
142
    final protected function icon($name, $style, $class = null) {
143
144
        // Initialize the template.
145
        $template = "<i %attributes%>%name%</i>";
146
147
        // Initialize the attributes.
148
        $_attr = [];
149
150
        $_attr["class"] = ["material-icons", $class];
151
        $_attr["style"] = $style;
152
153
        // Initialize the parameters.
154
        $_name = null !== $name ? $name : "home";
155
156
        // Return the HTML.
157
        return StringUtility::replace($template, ["%attributes%", "%name%"], [StringUtility::parseArray($_attr), $_name]);
158
    }
159
160
    /**
161
     * Displays a label.
162
     *
163
     * @param string $content The label content.
164
     * @param string $class The label class.
165
     * @return string Returns the label.
166
     */
167
    final protected function label($content, $class) {
168
169
        // Initialize the template.
170
        $template = "<span %attributes%>%content%</span>";
171
172
        // Initialize the attributes.
173
        $_attr = [];
174
175
        $_attr["class"] = ["label", $class];
176
177
        // Initialize the parameters.
178
        $_content = null !== $content ? $content : self::DEFAULT_CONTENT;
179
180
        // Return the HTML.
181
        return StringUtility::replace($template, ["%attributes%", "%content%"], [StringUtility::parseArray($_attr), $_content]);
182
    }
183
184
    /**
185
     * Displays a preloader.
186
     *
187
     * @param string $class The preloader class.
188
     * @param string $size The preloader size.
189
     * @return string Returns the preloader.
190
     */
191
    final protected function preloader($class, $size) {
192
193
        // Initialize the template.
194
        $template = '<div %attributes1%><div %attributes2%><div class="circle-clipper left"><div class="circle"></div></div><div class="circle-clipper right"><div class="circle"></div></div></div></div>';
195
196
        // Initialize the attributes.
197
        $_attr = [];
198
199
        $_attr["preloader"]["class"] = ["preloader", in_array($size, ["xs", "sm", "l", "xl"]) ? "pl-size-" . $size : null];
200
        $_attr["spinner"]["class"]   = ["spinner-layer", $class];
201
202
        // Return the HTML.
203
        return StringUtility::replace($template, ["%attributes1%", "%attributes2%"], [StringUtility::parseArray($_attr["preloader"]), StringUtility::parseArray($_attr["spinner"])]);
204
    }
205
206
    /**
207
     * Displays a progress bar.
208
     *
209
     * @param string $label The progress bar label.
210
     * @param integer $value The progress bar value.
211
     * @param integer $min The progress bar min.
212
     * @param integer $max The progress bar max.
213
     * @param boolean $striped Progress bar striped ?
214
     * @param boolean $animated Progress bar animated ?
215
     * @param string $class The progress bar class.
216
     * @return string Returns the progress bar.
217
     */
218
    final protected function progressBar($label, $value, $min, $max, $striped, $animated, $class = null) {
219
220
        // Initialize the template.
221
        $template = '<div class="progress"><div %attributes%>%label%</div></div>';
222
223
        // Initialize the attributes.
224
        $_attr = [];
225
226
        $_attr["class"]         = ["progress-bar", $class];
227
        $_attr["class"][]       = true === $striped ? "progress-bar-striped" : null;
228
        $_attr["class"][]       = true === $animated ? "active" : null;
229
        $_attr["style"]         = "width: " . $value . "%;";
230
        $_attr["role"]          = "progressbar";
231
        $_attr["aria-valuenow"] = $value;
232
        $_attr["aria-valuemin"] = $min;
233
        $_attr["aria-valuemax"] = $max . "%";
234
235
        // Initialize the parameters.
236
        $_label = null !== $label ? $label . '<span class="sr-only">' . $value . ' %</span>' : self::DEFAULT_CONTENT;
237
238
        // Return the HTML.
239
        return StringUtility::replace($template, ["%attributes%", "%label%"], [StringUtility::parseArray($_attr), $_label]);
240
    }
241
242
}
243