Passed
Pull Request — master (#888)
by Diego
03:13
created

NavbarNotificationLink::makeUpdatePeriod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Components\Layout;
4
5
use Illuminate\View\Component;
6
7
class NavbarNotificationLink extends Component
8
{
9
    /**
10
     * The id attribute for the underlying <li> wrapper.
11
     *
12
     * @var string
13
     */
14
    public $id;
15
16
    /**
17
     * The notification icon (a Font Awesome icon).
18
     *
19
     * @var string
20
     */
21
    public $icon;
22
23
    /**
24
     * The notification icon color (an AdminLTE color).
25
     *
26
     * @var string
27
     */
28
    public $iconColor;
29
30
    /**
31
     * The label for the notification badge.
32
     *
33
     * @var string
34
     */
35
    public $badgeLabel;
36
37
    /**
38
     * The background color for the notification badge (an AdminLTE color).
39
     *
40
     * @var string
41
     */
42
    public $badgeColor;
43
44
    /**
45
     * An array with the update configuration. The valid properties are:
46
     * url => string/array representing the url to fetch for new data.
47
     * route => string/array representing the route to fetch for new data.
48
     * period => integer representing the updating period time (in seconds).
49
     *
50
     * @var array
51
     */
52
    public $updateCfg;
53
54
    /**
55
     * Create a new component instance.
56
     *
57
     * @return void
58
     */
59 2
    public function __construct(
60
        $id, $icon, $iconColor = null, $badgeLabel = null, $badgeColor = null,
61
        $updateCfg = []
62
    ) {
63 2
        $this->id = $id;
64 2
        $this->icon = $icon;
65 2
        $this->iconColor = $iconColor;
66 2
        $this->badgeLabel = $badgeLabel;
67 2
        $this->badgeColor = $badgeColor;
68 2
        $this->updateCfg = is_array($updateCfg) ? $updateCfg : [];
69 2
    }
70
71
    /**
72
     * Make the class attribute for the notification icon.
73
     *
74
     * @return string
75
     */
76 1
    public function makeIconClass()
77
    {
78 1
        $classes = [$this->icon];
79
80 1
        if (! empty($this->iconColor)) {
81 1
            $classes[] = "text-{$this->iconColor}";
82
        }
83
84 1
        return implode(' ', $classes);
85
    }
86
87
    /**
88
     * Make the class attribute for the notification badge.
89
     *
90
     * @return string
91
     */
92 1
    public function makeBadgeClass()
93
    {
94 1
        $classes = ['badge navbar-badge text-bold text-xs badge-pill'];
95
96 1
        if (! empty($this->badgeColor)) {
97 1
            $classes[] = "badge-{$this->badgeColor}";
98
        }
99
100 1
        return implode(' ', $classes);
101
    }
102
103
    /**
104
     * Make the period time for updating the notification badge.
105
     *
106
     * @return int
107
     */
108 1
    public function makeUpdatePeriod()
109
    {
110 1
        if (! isset($this->updateCfg['period'])) {
111 1
            return 0;
112
        }
113
114 1
        return (intval($this->updateCfg['period']) ?? 0) * 1000;
115
    }
116
117
    /**
118
     * Make the url to use for fetch new notification data.
119
     *
120
     * @return string|null
121
     */
122 1
    public function makeUpdateUrl()
123
    {
124
        // Check if url property is available.
125
126 1
        if (! empty($this->updateCfg['url'])) {
127 1
            $uParams = $this->updateCfg['url'];
128
129 1
            return is_array($uParams) ? url(...$uParams) : url($uParams);
0 ignored issues
show
Bug introduced by
$uParams is expanded, but the parameter $path of url() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            return is_array($uParams) ? url(/** @scrutinizer ignore-type */ ...$uParams) : url($uParams);
Loading history...
130
        }
131
132
        // Check if route property is available.
133
134 1
        if (! empty($this->updateCfg['route'])) {
135 1
            $rParams = $this->updateCfg['route'];
136
137 1
            return is_array($rParams) ? route(...$rParams) : route($rParams);
0 ignored issues
show
Bug introduced by
$rParams is expanded, but the parameter $name of route() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
            return is_array($rParams) ? route(/** @scrutinizer ignore-type */ ...$rParams) : route($rParams);
Loading history...
138
        }
139
140
        // Return null when no url was configured.
141
142 1
        return null;
143
    }
144
145
    /**
146
     * Get the view / contents that represent the component.
147
     *
148
     * @return \Illuminate\View\View|string
149
     */
150 1
    public function render()
151
    {
152 1
        return view('adminlte::components.layout.navbar-notification-link');
153
    }
154
}
155