Completed
Push — master ( 8d8bfa...591466 )
by WEBEWEB
14:10
created

AbstractUITwigExtension::adminBSBButton()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
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\AdminBSBBundle\Twig\Extension\UI;
13
14
use WBW\Bundle\AdminBSBBundle\Provider\Color\DefaultColorProvider;
15
use WBW\Bundle\AdminBSBBundle\Twig\Extension\AbstractABSBMDTwigExtension;
16
use WBW\Bundle\BootstrapBundle\Navigation\NavigationInterface;
17
use WBW\Library\Core\Utility\Argument\StringUtility;
18
19
/**
20
 * Abstract UI Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\AdminBSBBundle\Twig\Extension\UI
24
 * @abstract
25
 */
26
abstract class AbstractUITwigExtension extends AbstractABSBMDTwigExtension implements NavigationInterface {
27
28
    /**
29
     * Constructor.
30
     */
31
    protected function __construct() {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Displays an AdminBSB 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 AdminBSB badge.
45
     */
46
    final protected function adminBSBBadge($content, $label, $large, $class, $list = false, $link = false) {
47
48
        // Initialize the template.
49
        $template = '<button %attributes%>%innerHTML%<span class="badge">%label%</span></button>';
50
        if (true === $list) {
51
            $template = '<a class="list-group-item" href="%href%"><span %attributes%>%innerHTML%</span>%label%</a>';
52
        }
53
54
        // Initialize the attributes.
55
        $attributes = [];
56
57
        if (true === $list) {
58
            $attributes["class"] = ["badge", $class];
59
        } else {
60
            $attributes["class"]   = ["btn", $class, "btn-block", "waves-effect"];
61
            $attributes["class"][] = true === $large ? "btn-lg" : null;
62
            $attributes["type"]    = "button";
63
        }
64
65
        // Initialize the parameters.
66
        $innerHTML = null !== $content ? $content : "";
67
        $spanLabel = null !== $label ? $label : "";
68
        $href      = null !== $link ? $link : self::DEFAULT_HREF;
69
70
        // Return the HTML.
71
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%", "%label%", "%href%"], [StringUtility::parseArray($attributes), $innerHTML, $spanLabel, $href]);
72
    }
73
74
    /**
75
     * Displays an AdminBSB 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 AdminBSB button.
86
     */
87
    final protected function adminBSBButton($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%%innerHTML%</button>";
95
96
        // Initialize the attributes.
97
        $attributes = [];
98
99
        $attributes["class"]       = ["btn", $class, "waves-effect"];
100
        $attributes["class"][]     = true === $block ? "btn-block" : null;
101
        $attributes["class"][]     = true === $circle ? "btn-circle" . ("lg" === $size ? "-lg" : "") . " waves-circle waves-float" : null;
102
        $attributes["class"][]     = true !== $circle && true === in_array($size, ["lg", "sm", "xs"]) ? "btn-" . $size : null;
103
        $attributes["title"]       = $title;
104
        $attributes["type"]        = "button";
105
        $attributes["data-toggle"] = null !== $title ? "tooltip" : null;
106
        $attributes["disabled"]    = true === $disable ? "disabled" : null;
107
108
        // Handle the parameters.
109
        $innerHTML = null !== $content ? $content : "";
110
        $glyphicon = null !== $icon ? $this->adminBSBIcon($icon, $style) : "";
111
112
        // Return the HTML.
113
        return StringUtility::replace($template, ["%attributes%", "%icon%", "%innerHTML%"], [StringUtility::parseArray($attributes), $glyphicon, $innerHTML]);
114
    }
115
116
    /**
117
     * Displays an AdminBSB 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 AdminBSB color.
123
     */
124
    final protected function adminBSBColor($name, $code, $output) {
125
126
        // Initialize the parameters.
127
        $_name = self::fixColor($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 AdminBSB 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 AdminBSB icon.
141
     */
142
    final protected function adminBSBIcon($name, $style, $class = null) {
143
144
        // Initialize the template.
145
        $template = "<i %attributes%>%innerHTML%</i>";
146
147
        // Initialize the attributes.
148
        $attributes = [];
149
150
        $attributes["class"] = ["material-icons", $class];
151
        $attributes["style"] = $style;
152
153
        // Initialize the parameters.
154
        $innerHTML = null !== $name ? $name : "home";
155
156
        // Return the HTML.
157
        return StringUtility::replace($template, ["%attributes%", "%innerHTML%"], [StringUtility::parseArray($attributes), $innerHTML]);
158
    }
159
160
    /**
161
     * Displays an AdminBSB preloader.
162
     *
163
     * @param string $class The preloader class.
164
     * @param string $size The preloader size.
165
     * @return string Returns the AdminBSB preloader.
166
     */
167
    final protected function adminBSBPreloader($class, $size) {
168
169
        // Initialize the template.
170
        $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>';
171
172
        // Initialize the attributes.
173
        $attributes = [];
174
175
        $attributes["preloader"]["class"] = ["preloader", in_array($size, ["xs", "sm", "l", "xl"]) ? "pl-size-" . $size : null];
176
        $attributes["spinner"]["class"]   = ["spinner-layer", $class];
177
178
        // Return the HTML.
179
        return StringUtility::replace($template, ["%attributes1%", "%attributes2%"], [StringUtility::parseArray($attributes["preloader"]), StringUtility::parseArray($attributes["spinner"])]);
180
    }
181
182
}
183