Completed
Push — master ( 191531...ff3871 )
by
unknown
01:26
created

Notifier   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 212
Duplicated Lines 15.09 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 0
dl 32
loc 212
rs 8.48
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A render() 0 17 3
B add() 0 33 6
B info() 8 8 8
B error() 8 8 8
B warning() 8 8 8
B success() 8 8 8
A clear() 0 4 1
A clearFlashed() 0 8 2
A clearAll() 0 9 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Notifier often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Notifier, and based on these observations, apply Extract Interface, too.

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 = $this->session->get('laravel::flash-notifications');
49
        if ($flashedNotifications !== null) {
50
            $this->flashedNotifications = $flashedNotifications;
51
        }
52
        if($flashedNotifications === null){
53
            $this->flashedNotifications = [];
54
        }
55
    }
56
57
    /**
58
     * Render all the notifications' script to insert into script tag
59
     *
60
     * @return string
61
     *
62
     */
63
    public function render(): string
64
    {
65
        $output = [];
66
67
        foreach ($this->notifications as $notification) {
68
69
            $output[] = $notification;
70
        }
71
        foreach ($this->flashedNotifications as $flNotification) {
72
73
            $output[] = $flNotification;
74
        }
75
76
        $this->session->forget('laravel::flash-notifications');
77
78
        return "window.notifications = " . json_encode($output) . ";";
79
    }
80
81
    /**
82
     * Add a notification
83
     *
84
     * @param string $type Could be error, info, success, or warning.
85
     * @param string $text The notification's message
86
     * @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...
87
     * @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...
88
     * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request
89
     *
90
     */
91
    public function add($theme, $timeout, $type, $layout, $text, $sounds = null, $soundsVolume = null, bool $onlyNextRequest = true)
92
    {
93
        if ($type == '') {
94
            $type = 'info';
95
        }
96
97
        if ($timeout === null) {
98
            $timeout = false;
99
        }
100
101
        $notification = [
102
            'theme' => $theme,
103
            'timeout' => $timeout,
104
            'type' => $type,
105
            'layout' => $layout,
106
            'text' => $text,
107
        ];
108
        if($sounds !== null){
109
            $notification['sources'] = [
110
                'sounds' => [$sounds],
111
                'soundsVolume' => $soundsVolume !== null ? $soundsVolume : 0.5,
112
            ];
113
        }
114
115
        if ($onlyNextRequest) {
116
            $flashNotifications = $this->session->get('laravel::flash-notifications');
117
            $this->session->forget('laravel::flash-notifications');
118
            $flashNotifications[] = $notification;
119
            $this->session->flash('laravel::flash-notifications', $flashNotifications);
120
        } else {
121
            $this->notifications[] = $notification;
122
        }
123
    }
124
125
    /**
126
     * Shortcut for adding an info notification
127
     *
128
     * @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...
129
     * @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...
130
     * @param array $options
131
     */
132 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...
133
    {
134
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
135
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : false;
136
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
137
138
        $this->add($theme, $timeout, 'info', $layout, $text, null, null, $onlyNextRequest);
139
    }
140
141
    /**
142
     * Shortcut for adding an error notification
143
     *
144
     * @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...
145
     * @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...
146
     * @param array $options
147
     */
148 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...
149
    {
150
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
151
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
152
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
153
154
        $this->add($theme, $timeout, 'error', $layout, $text, null, null, $onlyNextRequest);
155
    }
156
157
    /**
158
     * Shortcut for adding a warning notification
159
     *
160
     * @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...
161
     * @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...
162
     * @param array $options
163
     */
164 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...
165
    {
166
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
167
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
168
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
169
170
        $this->add($theme, $timeout, 'warning', $layout, $text, null, null, $onlyNextRequest);
171
    }
172
173
    /**
174
     * Shortcut for adding a success notification
175
     *
176
     * @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...
177
     * @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...
178
     * @param array $options
179
     */
180 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...
181
    {
182
        $theme = (isset($options['theme']) && $options['theme'] != '') ? $options['theme'] : 'metroui';
183
        $timeout = (isset($options['timeout']) && $options['timeout'] != '' && is_int($options['timeout'])) ? $options['timeout'] : 0;
184
        $layout = (isset($options['layout']) && $options['layout'] != '') ? $options['layout'] : 'topRight';
185
186
        $this->add($theme, $timeout, 'success', $layout, $text, null, null, $onlyNextRequest);
187
    }
188
189
190
    /**
191
     * Clear all notifications
192
     *
193
     * @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...
194
     */
195
    public function clear()
196
    {
197
        $this->notifications = [];
198
    }
199
200
    public function clearFlashed(bool $withSession = true)
201
    {
202
        $this->flashedNotifications = [];
203
204
        if ($withSession) {
205
            $this->session->forget('laravel::flash-notifications');
206
        }
207
    }
208
209
    public function clearAll(bool $withSession = true)
210
    {
211
        $this->notifications = [];
212
        $this->flashedNotifications = [];
213
214
        if ($withSession) {
215
            $this->session->forget('laravel::flash-notifications');
216
        }
217
    }
218
}
219