Completed
Push — master ( be9901...dbd7cb )
by
unknown
01:38
created

Notifier::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
     * Added notifications
19
     *
20
     * @var array
21
     */
22
    protected $options = [];
23
24
    /**
25
     * Illuminate Session
26
     *
27
     * @var \Illuminate\Session\SessionManager
28
     */
29
    protected $session;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param \Illuminate\Session\SessionManager $session
35
     *
36
     * @internal param \Illuminate\Session\SessionManager $session
37
     */
38
    public function __construct(SessionManager $session)
39
    {
40
        $this->session = $session;
41
    }
42
43
    /**
44
     * Render all the notifications' script to insert into script tag
45
     *
46
     * @return string
47
     *
48
     */
49
    public function render() : string
50
    {
51
        $notifications = $this->session->get('laravel::notifications');
52
        if (!$notifications) {
53
            $notifications = [];
54
        }
55
56
        $output = '';
57
58
        foreach ($notifications as $notification) {
59
60
            $output .= $this->renderNotification($notification);
61
        }
62
63
        return $output;
64
    }
65
66
    /**
67
     * Render the script of given notification to insert into script tag
68
     *
69
     * @param $notification
70
     * @return string
71
     */
72
    public function renderNotification($notification): string
73
    {        
74
		$theme = (isset($notification['theme']) ? $notification['theme'] : 'metroui');
75
		$timeout = (isset($notification['timeout']) ? $notification['timeout'] : 2000);
76
		$text = (isset($notification['text']) ? str_replace("'", "\\'", htmlentities($notification['text'])) : null);
77
        $message = (isset($notification['message']) ? $notification['message'] : '');
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        $type = (isset($notification['type']) ? $notification['type'] : 'info');
79
		$layout = (isset($notification['layout']) ? $notification['layout'] : 'topRight');
80
		
81
        $output = "				
82
                $(function () {
83
                    new Noty({            
84
						theme: '".$theme."',
85
						timeout: '".$timeout."',
86
						type: '".$type."',
87
						layout: '".$layout."',
88
						text: '".$text."'
89
					}).show();            
90
                });				
91
				";
92
        
93
        return $output;
94
    }
95
96
    /**
97
     * Add a notification
98
     *
99
     * @param string $type Could be error, info, success, or warning.
100
     * @param string $text The notification's message
101
     * @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...
102
     * @param array $options
103
     * @param bool $onlyNextRequest if true(default), se the notification in session only for the next request
104
     *
105
     */
106
    public function add($theme, $timeout, $type, $layout, $text, array $options = [], bool $onlyNextRequest=true)
107
    {
108
        if ($type=='') {
109
            $type = 'info';
110
        }
111
112
        $this->notifications[] = [
113
            'theme' => $theme,
114
            'timeout' => $timeout,
115
            'type' => $type,
116
			'layout' => $layout,
117
			'text' => $text,
118
			'options' => $options
119
        ];
120
121
        if($onlyNextRequest){
122
            $this->session->flash('laravel::notifications', $this->notifications);
123
        }else{
124
            $this->session->put('laravel::notifications', $this->notifications);
125
        }
126
    }
127
128
    /**
129
     * Shortcut for adding an info notification
130
     *
131
     * @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...
132
     * @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...
133
     * @param array $options
134
     */
135
    public function info($text, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        $this->add('metroui', 2000, 'info', 'topRight', $text, array(), true);
138
    }
139
	
140
	/**
141
     * Shortcut for adding an error notification
142
     *
143
     * @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...
144
     * @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...
145
     * @param array $options
146
     */
147
    public function error($text, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $this->add('metroui', 2000, 'error', 'topRight', $text, array(), true);
150
    }
151
152
    /**
153
     * Shortcut for adding a warning notification
154
     *
155
     * @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...
156
     * @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...
157
     * @param array $options
158
     */
159
    public function warning($text, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        $this->add('metroui', 2000, 'warning', 'topRight', $text, array(), true);
162
    }
163
164
    /**
165
     * Shortcut for adding a success notification
166
     *
167
     * @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...
168
     * @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...
169
     * @param array $options
170
     */
171
    public function success($text, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        $this->add('metroui', 2000, 'success', 'topRight', $text, array(), true);
174
    }
175
    
176
177
    /**
178
     * Clear all notifications
179
     * @param bool $withSession if true (default) clean notifications in session too.
180
     */
181
    public function clear(bool $withSession = true)
182
    {
183
        $this->notifications = [];
184
185
        if($withSession){
186
            $this->session->forget('laravel::notifications');
187
        }
188
    }
189
}
190