Passed
Push — master ( 8f6522...eef865 )
by Florian
06:34
created

NavbarNotification::makeUrlFromCfg()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 10
nop 2
dl 0
loc 21
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\View\Components\Layout;
4
5
use Illuminate\View\Component;
6
7
class NavbarNotification extends Component
8
{
9
    /**
10
     * Constants to define the available url configuration types.
11
     */
12
    protected const CFG_URL = 0;
13
    protected const CFG_ROUTE = 1;
14
15
    /**
16
     * The id attribute for the underlying <li> wrapper.
17
     *
18
     * @var string
19
     */
20
    public $id;
21
22
    /**
23
     * The notification icon (a Font Awesome icon).
24
     *
25
     * @var string
26
     */
27
    public $icon;
28
29
    /**
30
     * The notification icon color (an AdminLTE color).
31
     *
32
     * @var string
33
     */
34
    public $iconColor;
35
36
    /**
37
     * The label for the notification badge.
38
     *
39
     * @var string
40
     */
41
    public $badgeLabel;
42
43
    /**
44
     * The background color for the notification badge (an AdminLTE color).
45
     *
46
     * @var string
47
     */
48
    public $badgeColor;
49
50
    /**
51
     * An array with the update configuration. The valid properties are:
52
     * url => string/array representing the url to fetch for new data.
53
     * route => string/array representing the route to fetch for new data.
54
     * period => integer representing the updating period time (in seconds).
55
     *
56
     * @var array
57
     */
58
    public $updateCfg;
59
60
    /**
61
     * Enables the dropdown mode for the notification.
62
     *
63
     * @var bool
64
     */
65
    public $enableDropdownMode;
66
67
    /**
68
     * The label to use for the dropdown footer link.
69
     *
70
     * @var string
71
     */
72
    public $dropdownFooterLabel;
73
74
    /**
75
     * Create a new component instance.
76
     *
77
     * @return void
78
     */
79 3
    public function __construct(
80
        $id, $icon, $iconColor = null, $badgeLabel = null, $badgeColor = null,
81
        $updateCfg = [], $enableDropdownMode = false, $dropdownFooterLabel = null
82
    ) {
83 3
        $this->id = $id;
84 3
        $this->icon = $icon;
85 3
        $this->iconColor = $iconColor;
86 3
        $this->badgeLabel = $badgeLabel;
87 3
        $this->badgeColor = $badgeColor;
88 3
        $this->dropdownFooterLabel = $dropdownFooterLabel;
89 3
        $this->enableDropdownMode = boolval($enableDropdownMode);
90 3
        $this->updateCfg = is_array($updateCfg) ? $updateCfg : [];
91 3
    }
92
93
    /**
94
     * Make the class attribute for the list item.
95
     *
96
     * @return string
97
     */
98 1
    public function makeListItemClass()
99
    {
100 1
        $classes = ['nav-item'];
101
102 1
        if ($this->enableDropdownMode) {
103 1
            $classes[] = 'dropdown';
104
        }
105
106 1
        return implode(' ', $classes);
107
    }
108
109
    /**
110
     * Make the default attributes for the anchor tag.
111
     *
112
     * @return string
113
     */
114 1
    public function makeAnchorDefaultAttrs()
115
    {
116 1
        $attrs = ['class' => 'nav-link'];
117
118 1
        if ($this->enableDropdownMode) {
119 1
            $attrs['data-toggle'] = 'dropdown';
120
        }
121
122 1
        return $attrs;
123
    }
124
125
    /**
126
     * Make the class attribute for the notification icon.
127
     *
128
     * @return string
129
     */
130 1
    public function makeIconClass()
131
    {
132 1
        $classes = [$this->icon];
133
134 1
        if (! empty($this->iconColor)) {
135 1
            $classes[] = "text-{$this->iconColor}";
136
        }
137
138 1
        return implode(' ', $classes);
139
    }
140
141
    /**
142
     * Make the class attribute for the notification badge.
143
     *
144
     * @return string
145
     */
146 1
    public function makeBadgeClass()
147
    {
148 1
        $classes = ['badge navbar-badge text-bold text-xs badge-pill'];
149
150 1
        if (! empty($this->badgeColor)) {
151 1
            $classes[] = "badge-{$this->badgeColor}";
152
        }
153
154 1
        return implode(' ', $classes);
155
    }
156
157
    /**
158
     * Make the period time for updating the notification badge.
159
     *
160
     * @return int
161
     */
162 1
    public function makeUpdatePeriod()
163
    {
164 1
        if (! isset($this->updateCfg['period'])) {
165 1
            return 0;
166
        }
167
168 1
        return (intval($this->updateCfg['period']) ?? 0) * 1000;
169
    }
170
171
    /**
172
     * Create the url used for fetch new notification data.
173
     *
174
     * @return string|null
175
     */
176 2
    public function makeUpdateUrl()
177
    {
178
        // Check if the url property is available.
179
180 2
        if (! empty($this->updateCfg['url'])) {
181 1
            return $this->makeUrlFromCfg($this->updateCfg['url']);
182
        }
183
184
        // Check if the route property is available.
185
186 2
        if (! empty($this->updateCfg['route'])) {
187 1
            return $this->makeUrlFromCfg(
188 1
                $this->updateCfg['route'],
189 1
                self::CFG_ROUTE
190
            );
191
        }
192
193
        // Return null when no url was configured.
194
195 1
        return null;
196
    }
197
198
    /**
199
     * Create the url from specific configuration type.
200
     *
201
     * @param  string|array  $cfg  The configuration for the url.
202
     * @param  mixed  $type  The configuration type (url or route).
203
     * @return string|null
204
     */
205 1
    protected function makeUrlFromCfg($cfg, $type = self::CFG_URL)
206
    {
207
        // When config is just a string representing the url or route name,
208
        // wrap it inside an array.
209
210 1
        $cfg = is_string($cfg) ? [$cfg] : $cfg;
211
212
        // Check if config is an array with the url or route name and params.
213
214 1
        if (is_array($cfg) && count($cfg) >= 1) {
215 1
            $path = $cfg[0];
216 1
            $params = is_array($cfg[1] ?? null) ? $cfg[1] : [];
217
218 1
            return ($type === self::CFG_ROUTE) ?
219 1
                route($path, $params) :
220 1
                url($path, $params);
221
        }
222
223
        // Return null for invalid types or data.
224
225 1
        return null;
226
    }
227
228
    /**
229
     * Get the view / contents that represent the component.
230
     *
231
     * @return \Illuminate\View\View|string
232
     */
233 1
    public function render()
234
    {
235 1
        return view('adminlte::components.layout.navbar-notification');
236
    }
237
}
238