Test Failed
Branch main (345e92)
by Rafael
08:42
created

Messages::addError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 * or see https://www.gnu.org/
18
 */
19
20
namespace Alxarafe\Lib;
21
22
abstract class Messages
23
{
24
    /**
25
     * Contains messages, advices and errors pending to be shown.
26
     *
27
     * @var array
28
     */
29
    private static array $messages = [];
30
31
    /**
32
     * Add a new message (success) to show the user
33
     *
34
     * @param $message
35
     * @return void
36
     */
37
    public static function addMessage($message): void
38
    {
39
        self::$messages[]['success'] = $message;
40
    }
41
42
    /**
43
     * Add a new advice (warning) to show the user
44
     *
45
     * @param $message
46
     * @return void
47
     */
48
    public static function addAdvice($message): void
49
    {
50
        self::$messages[]['warning'] = $message;
51
    }
52
53
    /**
54
     * Add a new error (danger) to show the user
55
     *
56
     * @param $message
57
     * @return void
58
     */
59
    public static function addError($message): void
60
    {
61
        self::$messages[]['danger'] = $message;
62
    }
63
64
    /**
65
     * Generates an array with the messages to be displayed, indicating the
66
     * type (message, advice or error) and the text to be displayed.
67
     *
68
     * @return array
69
     */
70
    public static function getMessages(): array
71
    {
72
        $alerts = [];
73
        foreach (self::$messages as $message) {
74
            foreach ($message as $type => $text) {
75
                $alerts[] = [
76
                    'type' => $type,
77
                    'text' => $text
78
                ];
79
            }
80
        }
81
        self::$messages = [];
82
        return $alerts;
83
    }
84
}
85