Completed
Branch BUG-10738-inconsistency-in-ses... (a1eed8)
by
unknown
24:27 queued 12:29
created

AdminNotice::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\notices;
4
5
defined('EVENT_ESPRESSO_VERSION') || exit;
6
7
8
9
/**
10
 * Class AdminNotice
11
 * generates WordPress admin notices from EventEspresso\core\services\notices\Notice objects
12
 *
13
 * @package EventEspresso\core\services\notices
14
 * @author  Brent Christensen
15
 * @since   4.9.47.rc.041
16
 */
17
class AdminNotice
18
{
19
20
    const ERROR       = 'notice-error';
21
22
    const WARNING     = 'notice-warning';
23
24
    const SUCCESS     = 'notice-success';
25
26
    const INFORMATION = 'notice-info';
27
28
    const DISMISSABLE = ' is-dismissible';
29
30
    /**
31
     * generic system notice to be converted into a WP admin notice
32
     *
33
     * @var NoticeInterface $notice
34
     */
35
    private $notice;
36
37
38
    /**
39
     * AdminNotice constructor.
40
     *
41
     * @param NoticeInterface $notice
42
     * @param bool            $display_now
43
     */
44
    public function __construct(NoticeInterface $notice, $display_now = true)
45
    {
46
        $this->notice = $notice;
47
        if (! did_action('admin_notices')) {
48
            add_action('admin_notices', array($this, 'displayNotice'));
49
        } elseif ($display_now) {
50
            $this->displayNotice();
51
        }
52
    }
53
54
55
    /**
56
     * @return void
57
     */
58
    public function displayNotice()
59
    {
60
        echo $this->getNotice();
61
    }
62
63
64
    /**
65
     * produces something  like:
66
     *  <div class="notice notice-success is-dismissible event-espresso-admin-notice">
67
     *      <p>YOU DID IT!</p>
68
     *      <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this
69
     *      notice.</span></button>
70
     *  </div>
71
     *
72
     * @return string
73
     */
74
    public function getNotice()
75
    {
76
        return sprintf(
77
            '<div class="notice %1$s%2$s event-espresso-admin-notice"><p>%3$s</p></div>',
78
            $this->getType(),
79
            $this->notice->isDismissible() ? AdminNotice::DISMISSABLE : '',
80
            $this->getMessage()
81
        );
82
    }
83
84
85
    /**
86
     * @return string
87
     */
88
    private function getType()
89
    {
90
        switch ($this->notice->type()) {
91
            case Notice::ERROR :
92
                return AdminNotice::ERROR;
93
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
94
            case Notice::ATTENTION :
95
                return AdminNotice::WARNING;
96
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
97
            case Notice::SUCCESS :
98
                return AdminNotice::SUCCESS;
99
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
100
            case Notice::INFORMATION :
101
            default:
102
                return AdminNotice::INFORMATION;
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
        }
105
    }
106
107
108
    /**
109
     * @return string
110
     */
111
    protected function getMessage()
112
    {
113
        $message = $this->notice->message();
114
        if (WP_DEBUG && $this->getType() === AdminNotice::ERROR) {
115
            $message .= '<br/><span class="tiny-text">' . $this->generateErrorCode() . '</span>';
116
        }
117
        return $message;
118
    }
119
120
121
    /**
122
     * create error code from filepath, function name,
123
     * and line number where notice was generated
124
     *
125
     * @return string
126
     */
127
    protected function generateErrorCode()
128
    {
129
        $file       = explode('.', basename($this->notice->file()));
130
        $error_code = ! empty($file[0]) ? $file[0] : '';
131
        $error_code .= ! empty($error_code) ? ' - ' . $this->notice->func() : $this->notice->func();
132
        $error_code .= ' - ' . $this->notice->line();
133
        return $error_code;
134
    }
135
136
137
}
138