Completed
Push — master ( ff3871...123d0d )
by
unknown
01:28
created

Notifier::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Padosoft\Laravel\Notification\Notifier;
4
5
use Illuminate\Session\SessionManager;
6
7
class Notifier
8
{
9
10
    /**
11
     * Added notifications
12
     *
13
     * @var array
14
     */
15
    protected $notifications = [];
16
17
    /**
18
     * Flashable added notifications
19
     *
20
     * @var array
21
     */
22
    protected $flashedNotifications = [];
23
24
    /**
25
     * Added notifications
26
     *
27
     * @var array
28
     */
29
    protected $options = [];
30
31
    /**
32
     * Illuminate Session
33
     *
34
     * @var \Illuminate\Session\SessionManager
35
     */
36
    protected $session;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param \Illuminate\Session\SessionManager $session
42
     *
43
     * @internal param \Illuminate\Session\SessionManager $session
44
     */
45
    public function __construct(SessionManager $session)
46
    {
47
        $this->session = $session;
48
        $flashedNotifications = null;
49
50
        if ($this->session->has('laravel::flash-notifications')) {
51
            $flashedNotifications = $this->session->get('laravel::flash-notifications');
52
        }
53
        if ($flashedNotifications !== null) {
54
            $this->flashedNotifications = $flashedNotifications;
55
        } else {
56
            $this->flashedNotifications = [];
57
        }
58
    }
59
60
    /**
61
     * Render all the notifications' script to insert into script tag
62
     *
63
     * @return string
64
     *
65
     */
66
    public function render(): string
67
    {
68
        $output = [];
69
        foreach ($this->notifications as $notification) {
70
71
            $output[] = $notification;
72
        }
73
        foreach ($this->flashedNotifications as $flNotification) {
74
            $output[] = $flNotification;
75
        }
76
77
        return "window.notifications = " . json_encode($output) . ";";
78
    }
79
80
    /**
81
     * Add a notification
82
     *
83
     * @param string $type Could be error, info, success, or warning.
84
     * @param string $text The notification's message
85
     * @param string $title The notification's title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
86
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
87
     * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request
0 ignored issues
show
Bug introduced by
There is no parameter named $onlyNextRequest. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
     *
89
     */
90
    public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null)
91
    {
92
        if ($type == '') {
93
            $type = 'info';
94
        }
95
96
        if ($timeout === null) {
97
            $timeout = false;
98
        }
99
100
        $notification = [
101
            'theme' => $theme,
102
            'timeout' => $timeout,
103
            'type' => $type,
104
            'layout' => $layout,
105
            'text' => $text,
106
        ];
107 View Code Duplication
        if ($sounds !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            $notification['sources'] = [
109
                'sounds' => [$sounds],
110
                'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5,
111
            ];
112
        }
113
114
        $this->notifications[] = $notification;
115
    }
116
117
    public function addForNextRequest($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null)
118
    {
119
        if ($type == '') {
120
            $type = 'info';
121
        }
122
123
        if ($timeout === null) {
124
            $timeout = false;
125
        }
126
127
        $notification = [
128
            'theme' => $theme,
129
            'timeout' => $timeout,
130
            'type' => $type,
131
            'layout' => $layout,
132
            'text' => $text,
133
        ];
134 View Code Duplication
        if ($sounds !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
            $notification['sources'] = [
136
                'sounds' => [$sounds],
137
                'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5,
138
            ];
139
        }
140
141
        if ($this->session->has('laravel::flash-notifications')) {
142
            $flashNotifications = $this->session->get('laravel::flash-notifications');
143
        } else {
144
            $flashNotifications = [];
145
        }
146
        $this->session->forget('laravel::flash-notifications');
147
        $flashNotifications[] = $notification;
148
        $this->session->flash('laravel::flash-notifications', $flashNotifications);
149
    }
150
151
    /**
152
     * Shortcut for adding an info notification
153
     *
154
     * @param string $message The notification's message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
155
     * @param string $title The notification's title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
156
     * @param array $options
157
     */
158 View Code Duplication
    public function info($text, $onlyNextRequest = true, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
161
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : false;
162
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
163
164
        $this->add($theme, $timeout, 'info', $layout, $text, null, null, $onlyNextRequest);
0 ignored issues
show
Unused Code introduced by
The call to Notifier::add() has too many arguments starting with $onlyNextRequest.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
165
    }
166
167
    /**
168
     * Shortcut for adding an error notification
169
     *
170
     * @param string $message The notification's message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
171
     * @param string $title The notification's title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
172
     * @param array $options
173
     */
174 View Code Duplication
    public function error($text, $onlyNextRequest = true, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
177
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
178
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
179
180
        $this->add($theme, $timeout, 'error', $layout, $text, null, null, $onlyNextRequest);
0 ignored issues
show
Unused Code introduced by
The call to Notifier::add() has too many arguments starting with $onlyNextRequest.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
181
    }
182
183
    /**
184
     * Shortcut for adding a warning notification
185
     *
186
     * @param string $message The notification's message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
     * @param string $title The notification's title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
188
     * @param array $options
189
     */
190 View Code Duplication
    public function warning($text, $onlyNextRequest = true, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
193
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
194
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
195
196
        $this->add($theme, $timeout, 'warning', $layout, $text, null, null, $onlyNextRequest);
0 ignored issues
show
Unused Code introduced by
The call to Notifier::add() has too many arguments starting with $onlyNextRequest.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
197
    }
198
199
    /**
200
     * Shortcut for adding a success notification
201
     *
202
     * @param string $message The notification's message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
203
     * @param string $title The notification's title
0 ignored issues
show
Bug introduced by
There is no parameter named $title. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
204
     * @param array $options
205
     */
206 View Code Duplication
    public function success($text, $onlyNextRequest = true, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
209
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
210
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
211
        $this->add($theme, $timeout, 'success', $layout, $text, null, null, $onlyNextRequest);
0 ignored issues
show
Unused Code introduced by
The call to Notifier::add() has too many arguments starting with $onlyNextRequest.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
212
    }
213
214
215
    /**
216
     * Clear all notifications
217
     *
218
     * @param bool $withSession if true (default) clean notifications in session too.
0 ignored issues
show
Bug introduced by
There is no parameter named $withSession. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
219
     */
220
    public function clear()
221
    {
222
        $this->notifications = [];
223
    }
224
225
    public function clearFlashed(bool $withSession = true)
226
    {
227
        $this->flashedNotifications = [];
228
229
        if ($withSession) {
230
            $this->session->forget('laravel::flash-notifications');
231
        }
232
    }
233
234
    public function clearAll(bool $withSession = true)
235
    {
236
        $this->notifications = [];
237
        $this->flashedNotifications = [];
238
239
        if ($withSession) {
240
            $this->session->forget('laravel::flash-notifications');
241
        }
242
    }
243
}
244