Passed
Push — master ( fc0856...e4fdff )
by Sugeng
02:03
created

ToastrFlash   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 17
c 4
b 1
f 0
dl 0
loc 40
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 5
A generateToastr() 0 7 1
1
<?php
2
3
namespace diecoding\toastr;
4
5
use Yii;
6
7
/**
8
 * ToastrFlash is a widget integrating the [Toastr](https://codeseven.github.io/toastr/).
9
 * 
10
 * @link [sugeng-sulistiyawan.github.io](sugeng-sulistiyawan.github.io)
11
 * @author Sugeng Sulistiyawan <[email protected]>
12
 * @copyright Copyright (c) 2023
13
 */
14
class ToastrFlash extends ToastrBase
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function run()
20
    {
21
        $session = Yii::$app->session;
22
        $flashes = $session->getAllFlashes();
0 ignored issues
show
Bug introduced by
The method getAllFlashes() does not exist on null. ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-call */ 
23
        $flashes = $session->getAllFlashes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
23
        foreach ($flashes as $type => $data) {
24
            $datas = (array) $data;
25
            if (is_array($datas[0])) {
26
                foreach ($datas as $value) {
27
                    $this->generateToastr($type, $value[1], $value[0]);
28
                }
29
            } else {
30
                foreach ($datas as $value) {
31
                    $this->generateToastr($type, $value);
32
                }
33
            }
34
35
            $session->removeFlash($type);
36
        }
37
    }
38
39
    /**
40
     * Generate Single Toastr
41
     * 
42
     * @param string $type
43
     * @param string $message
44
     * @param string|null $title
45
     * @return void
46
     */
47
    private function generateToastr($type, $message, $title = null)
48
    {
49
        Toastr::widget([
50
            "type"    => $type,
51
            "title"   => $title,
52
            "message" => $message,
53
            "options" => $this->options,
54
        ]);
55
    }
56
}
57