Notice::initializes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WPDFI\Admin;
3
/**
4
 * This class handle all notice stuffs in admin dashboard of this plugin
5
 *
6
 * @author Duc Bui Quang <[email protected]>
7
 * @since 1.0.0
8
 */
9
use WPDFI\Traits\Singleton;
10
11
final class Notice 
12
{
13
    use Singleton;
14
15
    /**
16
     * @traitDoc
17
     */
18
    public function initializes() 
19
    {   
20
        //
21
    }
22
23
    /**
24
     * Type of notice, it can be 'success', 'error', 'warning', 'info'.
25
     */
26
    private $type;
27
        
28
    /**
29
     * Message of the notice
30
     */
31
    private $message;
32
    
33
    /**
34
     * Add new notice to the admin dashboard.
35
     *
36
     * @param string $noticeMessage
37
     * @param string $noticeType
38
     * @since 1.0.0
39
     * @return void
40
     */
41
    public function add($noticeMessage, $noticeType = 'info') 
42
    {
43
        $this->message = $noticeMessage;
44
        $this->type = $noticeType;
45
        
46
        \add_action( 'admin_notices', [$this, 'display']);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
47
    }
48
    
49
    /**
50
     * Display notice template to the admin dashboard.
51
     * 
52
     * @since 1.0.0
53
     * @return \VA\Templater
54
     */
55
    public function display() 
56
    {
57
        if(!$this->message) return;
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
58
        
59
        echo \wpdfi()->templater->render('admin/notice', [
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '\'
Loading history...
60
            'classes'   => $this->get_classes(),
61
            'message'   => __($this->message, 'wpdfi')
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
62
        ]);
63
    }
64
    
65
    /**
66
     * Get all the classes of the notice
67
     * 
68
     * @since 1.0.0
69
     * @return string
70
     */
71
    private function get_classes() 
72
    {
73
        return "notice notice-{$this->get_type()} is-dismissible";
74
    }
75
    
76
    /**
77
     * Get the type of the notice
78
     *
79
     * @since 1.0.0
80
     * @return string
81
     */ 
82
    private function get_type()
83
    {
84
        return $this->type ? $this->type : $this->get_default_type();
85
    }
86
    
87
    /**
88
     * Get default type of the notice
89
     *
90
     * @since 1.0.0
91
     * @return string
92
     */
93
    private function get_default_type()
94
    {
95
        return 'info';
96
    }
97
}