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

Notice::setFile()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.2
c 2
b 1
f 0
1
<?php
2
3
namespace EventEspresso\core\services\notices;
4
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
7
defined('EVENT_ESPRESSO_VERSION') || exit;
8
9
10
11
/**
12
 * Class Notice
13
 * DTO for temporarily holding notification information until it can be processed
14
 *
15
 * @package       Event Espresso
16
 * @author        Brent Christensen
17
 * @since         $VID:$
18
 */
19
class Notice implements NoticeInterface
20
{
21
22
    const ERROR       = 'error';
23
24
    const SUCCESS     = 'success';
25
26
    const ATTENTION   = 'attention'; // alias for warning
27
28
    const INFORMATION = 'information';
29
30
    /**
31
     * @var string $type
32
     */
33
    private $type;
34
35
36
    /**
37
     * @var string $message
38
     */
39
    private $message;
40
41
42
    /**
43
     * @var string $file
44
     */
45
    private $file;
46
47
48
    /**
49
     * @var string $func
50
     */
51
    private $func;
52
53
54
    /**
55
     * @var string $line
56
     */
57
    private $line;
58
59
60
    /**
61
     * @var boolean $dismissible
62
     */
63
    private $dismissible;
64
65
66
67
    /**
68
     * Notice constructor.
69
     *
70
     * @param string $type
71
     * @param string $message
72
     * @param bool   $dismissible
73
     * @param string $file
74
     * @param string $func
75
     * @param string $line
76
     * @throws InvalidDataTypeException
77
     */
78
    public function __construct($type, $message, $dismissible = true, $file = '', $func = '', $line = '')
79
    {
80
        $this->setType($type);
81
        $this->setMessage($message);
82
        $this->setDismissible($dismissible);
83
        $this->setFile($file);
84
        $this->setFunc($func);
85
        $this->setLine($line);
86
    }
87
88
89
90
    /**
91
     * @return array
92
     */
93
    private function types()
94
    {
95
        return (array)apply_filters(
96
            'FHEE__EventEspresso_core_services_notices_Notice__types',
97
            array(
98
                Notice::ERROR,
99
                Notice::SUCCESS,
100
                Notice::ATTENTION,
101
                Notice::INFORMATION,
102
            )
103
        );
104
    }
105
106
107
108
    /**
109
     * @return string
110
     */
111
    public function type()
112
    {
113
        return $this->type;
114
    }
115
116
117
118
    /**
119
     * @return string
120
     */
121
    public function message()
122
    {
123
        return $this->message;
124
    }
125
126
127
128
    /**
129
     * @return string
130
     */
131
    public function file()
132
    {
133
        return $this->file;
134
    }
135
136
137
138
    /**
139
     * @return string
140
     */
141
    public function func()
142
    {
143
        return $this->func;
144
    }
145
146
147
148
    /**
149
     * @return string
150
     */
151
    public function line()
152
    {
153
        return $this->line;
154
    }
155
156
157
    /**
158
     * @return bool
159
     */
160
    public function isDismissible()
161
    {
162
        return $this->dismissible;
163
    }
164
165
166
    /**
167
     * @param string $type
168
     * @throws InvalidDataTypeException
169
     */
170
    private function setType($type)
171
    {
172
        if (! in_array($type, $this->types(), true)) {
173
            throw new InvalidDataTypeException(
174
                '$type',
175
                $type,
176
                $this->invalidTypeMessage()
177
            );
178
        }
179
        $this->type = $type;
180
    }
181
182
183
184
    /**
185
     * gets the $invalid_type_message string
186
     */
187
    private function invalidTypeMessage()
188
    {
189
        return apply_filters(
190
            'FHEE__EventEspresso_core_services_notices_Notice__invalidTypeMessage',
191
            sprintf(
192
                esc_html__(
193
                    ' one of the following notice types was expected: %1$s %2$s',
194
                    'event_espresso'
195
                ),
196
                '<br />',
197
                var_export($this->types(), true)
198
            )
199
        );
200
    }
201
202
203
204
    /**
205
     * @param string $message
206
     * @throws InvalidDataTypeException
207
     */
208 View Code Duplication
    private function setMessage($message)
209
    {
210
        if (empty($message) || ! is_string($message)) {
211
            throw new InvalidDataTypeException(
212
                '$message',
213
                $message,
214
                esc_html__('non empty string', 'event_espresso')
215
            );
216
        }
217
        $this->message = $message;
218
    }
219
220
221
222
    /**
223
     * @param string $file
224
     * @throws InvalidDataTypeException
225
     */
226
    private function setFile($file)
227
    {
228
        if ($this->type === Notice::ERROR && (empty($file) || ! is_string($file))) {
229
            throw new InvalidDataTypeException(
230
                '$file',
231
                $file,
232
                esc_html__('non empty string', 'event_espresso')
233
            );
234
        }
235
        $this->file = $file;
236
    }
237
238
239
240
    /**
241
     * @param string $func
242
     * @throws InvalidDataTypeException
243
     */
244
    private function setFunc($func)
245
    {
246
        if ($this->type === Notice::ERROR && (empty($func) || ! is_string($func))) {
247
            throw new InvalidDataTypeException(
248
                '$func',
249
                $func,
250
                esc_html__('non empty string', 'event_espresso')
251
            );
252
        }
253
        $this->func = $func;
254
    }
255
256
257
258
    /**
259
     * @param int $line
260
     * @throws InvalidDataTypeException
261
     */
262
    private function setLine($line)
263
    {
264
        $line = absint($line);
265
        if ($this->type === Notice::ERROR && $line === 0) {
266
            throw new InvalidDataTypeException(
267
                '$line',
268
                $line,
269
                esc_html__('integer', 'event_espresso')
270
            );
271
        }
272
        $this->line = $line;
273
    }
274
275
276
    /**
277
     * @param boolean $dismissible
278
     */
279
    private function setDismissible($dismissible = true)
280
    {
281
        $this->dismissible = filter_var($dismissible, FILTER_VALIDATE_BOOLEAN);
282
    }
283
284
}
285