Passed
Pull Request — master (#888)
by Diego
03:37 queued 01:08
created

NavbarNotificationLink::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Components\Layout;
4
5
use Illuminate\View\Component;
6
7
class NavbarNotificationLink 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
     * Create a new component instance.
62
     *
63
     * @return void
64
     */
65 3
    public function __construct(
66
        $id, $icon, $iconColor = null, $badgeLabel = null, $badgeColor = null,
67
        $updateCfg = []
68
    ) {
69 3
        $this->id = $id;
70 3
        $this->icon = $icon;
71 3
        $this->iconColor = $iconColor;
72 3
        $this->badgeLabel = $badgeLabel;
73 3
        $this->badgeColor = $badgeColor;
74 3
        $this->updateCfg = is_array($updateCfg) ? $updateCfg : [];
75 3
    }
76
77
    /**
78
     * Make the class attribute for the notification icon.
79
     *
80
     * @return string
81
     */
82 1
    public function makeIconClass()
83
    {
84 1
        $classes = [$this->icon];
85
86 1
        if (! empty($this->iconColor)) {
87 1
            $classes[] = "text-{$this->iconColor}";
88
        }
89
90 1
        return implode(' ', $classes);
91
    }
92
93
    /**
94
     * Make the class attribute for the notification badge.
95
     *
96
     * @return string
97
     */
98 1
    public function makeBadgeClass()
99
    {
100 1
        $classes = ['badge navbar-badge text-bold text-xs badge-pill'];
101
102 1
        if (! empty($this->badgeColor)) {
103 1
            $classes[] = "badge-{$this->badgeColor}";
104
        }
105
106 1
        return implode(' ', $classes);
107
    }
108
109
    /**
110
     * Make the period time for updating the notification badge.
111
     *
112
     * @return int
113
     */
114 1
    public function makeUpdatePeriod()
115
    {
116 1
        if (! isset($this->updateCfg['period'])) {
117 1
            return 0;
118
        }
119
120 1
        return (intval($this->updateCfg['period']) ?? 0) * 1000;
121
    }
122
123
    /**
124
     * Create the url used for fetch new notification data.
125
     *
126
     * @return string|null
127
     */
128 2
    public function makeUpdateUrl()
129
    {
130
        // Check if the url property is available.
131
132 2
        if (! empty($this->updateCfg['url'])) {
133 1
            return $this->makeUrlFromCfg($this->updateCfg['url']);
134
        }
135
136
        // Check if the route property is available.
137
138 2
        if (! empty($this->updateCfg['route'])) {
139 1
            return $this->makeUrlFromCfg(
140 1
                $this->updateCfg['route'],
141 1
                self::CFG_ROUTE
142
            );
143
        }
144
145
        // Return null when no url was configured.
146
147 1
        return null;
148
    }
149
150
    /**
151
     * Create the url from specific configuration type.
152
     *
153
     * @param string|array $cfg The configuration for the url.
154
     * @param mixed $type The configuration type (url or route).
155
     * @return string|null
156
     */
157 1
    protected function makeUrlFromCfg($cfg, $type = self::CFG_URL)
158
    {
159
        // When config is just a string representing the url or route name,
160
        // wrap it inside an array.
161
162 1
        $cfg = is_string($cfg) ? [$cfg] : $cfg;
163
164
        // Check if config is an array with the url or route name and params.
165
166 1
        if (is_array($cfg) && count($cfg) >= 1) {
167 1
            $path = $cfg[0];
168 1
            $params = is_array($cfg[1] ?? null) ? $cfg[1] : [];
169
170 1
            return ($type === self::CFG_ROUTE) ?
171 1
                route($path, $params) :
172 1
                url($path, $params);
173
        }
174
175
        // Return null for invalid types or data.
176
177 1
        return null;
178
    }
179
180
    /**
181
     * Get the view / contents that represent the component.
182
     *
183
     * @return \Illuminate\View\View|string
184
     */
185 1
    public function render()
186
    {
187 1
        return view('adminlte::components.layout.navbar-notification-link');
188
    }
189
}
190