NavToastr::show()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
/*
5
 * This file is part of the Laravel Navigation Toastr package.
6
 *
7
 * (c) Mujtech Mujeeb <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Mujhtech\NavToastr;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Config\Repository;
16
use Illuminate\Session\SessionManager;
17
18
class NavToastr {
19
20
21
    const ERROR = 'error';
22
    const INFO = 'info';
23
    const SUCCESS = 'success';
24
    const WARNING = 'warning';
25
26
27
    const NAVTOASTR_NOTI = 'navtoastr::noti';
28
29
30
    /**
31
     * Added notifications.
32
     *
33
     * @var array
34
     */
35
    protected $notifications = [];
36
37
    /**
38
     * Illuminate Session.
39
     *
40
     * @var \Illuminate\Session\SessionManager
41
     */
42
    protected $session;
43
44
    /**
45
     * Toastr config.
46
     *
47
     * @var \Illuminate\Config\Repository
48
     */
49
    protected $config;
50
51
    /**
52
     * Allowed toast types.
53
     *
54
     * @var array
55
     */
56
57
58
    protected $allowedTypes = [self::ERROR, self::INFO, self::SUCCESS, self::WARNING];
59
60
61
    /**
62
     * Toastr constructor.
63
     *
64
     * @param SessionManager $session
65
     * @param Repository     $config
66
     */
67
68
    public function __construct(SessionManager $session, Repository $config)
69
    {
70
        $this->session = $session;
71
72
        $this->config = $config;
73
74
        $this->notifications = $this->session->get(self::NAVTOASTR_NOTI, []);
75
76
    }
77
78
    /**
79
     * Show the notifications' script tag.
80
     *
81
     * @return string
82
     */
83
84
    public function show(): string
85
    {
86
        $noti = '<script type="text/javascript">'.$this->notificationsAsString().'</script>';
87
88
        $this->session->forget(self::NAVTOASTR_NOTI);
89
90
        return $noti;
91
    }
92
93
94
    /**
95
     * @return string
96
     */
97
98
    public function notificationsAsString(): string
99
    {
100
        return implode('', array_slice($this->notifications(), -1));
101
    }
102
103
104
105
    /**
106
     * map over all notifications and create an array of toastrs.
107
     *
108
     * @return array
109
     */
110
111
112
    public function notifications(): array
113
    {
114
        return array_map(
115
            function ($n) {
116
                return $this->toast($n['type'], $n['message'], $n['enableCustomButton']);
117
            },
118
            $this->session->get(self::NAVTOASTR_NOTI, [])
119
        );
120
    }
121
122
123
    /**
124
     * Create a single toastr.
125
     *
126
     * @param string      $type
127
     * @param string      $message
128
     * @param bool|false $enableCustomButton
129
     *
130
     * @return string
131
     */
132
133
134
    public function toast(string $type, string $message = '', bool $enableCustomButton = false): string
135
    {
136
        $custombutton = $enableCustomButton ? $this->options() : '';
137
138
        return "new Toast({ message: '$message', type: '$type', $custombutton });";
139
    }
140
141
142
    /**
143
     * Shortcut for adding an error notification.
144
     *
145
     * @param string $message The notification's message
146
     * @param string $title   The notification's title
147
     * @param bool  $enableCustomButton
148
     *
149
     * @return NavToastr
150
     */
151
152
153
    public function error(string $message, bool $enableCustomButton = false): self
154
    {
155
        return $this->addNotification(self::ERROR, $message, $enableCustomButton);
156
    }
157
158
159
    /**
160
     * Shortcut for adding an info notification.
161
     *
162
     * @param string $message The notification's message
163
     * @param string $title   The notification's title
164
     * @param bool  $enableCustomButton
165
     *
166
     * @return NavToastr
167
     */
168
169
    public function info(string $message, bool $enableCustomButton = false): self
170
    {
171
        return $this->addNotification(self::INFO, $message, $enableCustomButton);
172
    }
173
174
175
    /**
176
     * Shortcut for adding an success notification.
177
     *
178
     * @param string $message The notification's message
179
     * @param string $title   The notification's title
180
     * @param bool  $enableCustomButton
181
     *
182
     * @return NavToastr
183
     */
184
185
    public function success(string $message, bool $enableCustomButton = false): self
186
    {
187
        return $this->addNotification(self::SUCCESS, $message, $enableCustomButton);
188
    }
189
190
191
    /**
192
     * Shortcut for adding an warning notification.
193
     *
194
     * @param string $message The notification's message
195
     * @param string $title   The notification's title
196
     * @param bool  $enableCustomButton
197
     *
198
     * @return NavToastr
199
     */
200
201
202
    public function warning(string $message, bool $enableCustomButton = false): self
203
    {
204
        return $this->addNotification(self::WARNING, $message, $enableCustomButton);
205
    }
206
207
208
     /**
209
     * Add a notification.
210
     *
211
     * @param string $type    Could be error, info, success, or warning.
212
     * @param string $message The notification's message
213
     * @param string $title   The notification's title
214
     * @param bool  $enableCustomButton
215
     *
216
     * @return NavToastr
217
     */
218
219
220
    public function addNotification(string $type, string $message, bool $enableCustomButton = false): self
221
    {
222
        $this->notifications[] = [
223
            'type'    => in_array($type, $this->allowedTypes, true) ? $type : self::WARNING,
224
            'message' => $this->escapeSingleQuote($message),
225
            'enableCustomButton' => $enableCustomButton,
226
        ];
227
228
        $this->session->flash(self::NAVTOASTR_NOTI, $this->notifications);
229
230
        return $this;
231
    }
232
233
234
235
    /**
236
     * Get global nav-toastr custom buttons.
237
     *
238
     * @return string
239
     */
240
241
242
    public function options(): string
243
    {
244
245
        $option = array_map(
246
            function ($n) {
247
                if(array_key_exists('reload', $n) && $n['reload'] == true)
248
                    return array(
249
                        'text' => $n['text'],
250
                        'onClick' => 'function(){window.location.reload();}'
251
                    );
252
                return array(
253
                    'text' => $n['text'], 
254
                    'onClick' => 'function(){window.open("'.$n['url'].'");}'
255
                );
256
            },
257
            $this->config->get('nav-toastr.custombuttons', [])
258
        );
259
260
        $subject = json_encode($option);
261
262
        $search = ['"text"', '"onClick"', '\"', '\/\/', '\/', '\"', '}"', '"f'];
263
264
        $replace   = ['text', 'onClick', '"', '//', '/', '"', '}', 'f'];
265
266
267
        return 'customButtons : '.str_replace($search, $replace, $subject).'';
268
    }
269
270
271
    /**
272
     * Shortcut for custom redirect back.
273
     *
274
     *
275
     * @return NavToastr
276
     */
277
278
279
    public function back()
280
    {
281
        return back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Mujhtech\NavToastr\NavToastr.
Loading history...
282
    }
283
284
    /**
285
     * Shortcut for custom redirect to url.
286
     *
287
     * @param string $url The url to redirect to
288
     *
289
     * @return NavToastr
290
     */
291
292
293
    public function to( string $url )
294
    {
295
        return redirect($url);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($url) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Mujhtech\NavToastr\NavToastr.
Loading history...
296
    }
297
298
299
    /**
300
     * Shortcut for custom redirect to named route.
301
     *
302
     * @param string $name The name route to redirect to
303
     *
304
     * @return NavToastr
305
     */
306
307
308
    public function named( string $name )
309
    {
310
311
        return redirect()->route($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route($name) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Mujhtech\NavToastr\NavToastr.
Loading history...
312
313
    }
314
315
316
    /**
317
     * Shortcut for custom redirect to url outside your app.
318
     *
319
     * @param string $url The url to redirect to
320
     *
321
     * @return NavToastr
322
     */
323
324
325
    public function out( string $url )
326
    {
327
328
        return redirect()->away($url);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->away($url) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Mujhtech\NavToastr\NavToastr.
Loading history...
329
330
    }
331
332
333
    private function escapeSingleQuote(string $value): string
334
    {
335
        return str_replace("'", "\\'", $value);
336
    }
337
338
339
    /**
340
     * Clear all notifications.
341
     *
342
     * @return NavToastr
343
     */
344
345
    public function clear() {
346
347
        $this->notifications = [];
348
349
        return $this;
350
351
    }
352
353
}