Messages::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Asymptix\core;
4
5
/**
6
 * Messages functionlity.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2013 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class Messages {
16
17
    const MSG_EMPTY = 0;
18
    const MSG_INFO = 1;
19
    const MSG_SUCCESS = 2;
20
    const MSG_WARNING = 3;
21
    const MSG_ERROR = 4;
22
23
    /**
24
     * Messages list.
25
     *
26
     * @var array
27
     */
28
    private static $messages = [];
29
30
    /**
31
     * Push new message to the global messages list.
32
     *
33
     * @param int $type Priority type of the message.
34
     * @param string $text Text of the message.
35
     * @param string $code Code of the message in the message list (optional).
36
     *
37
     * @throws \Exception If wrong priority message type provided.
38
     */
39
    public static function pushMessage($type, $text, $code = null) {
40
        $oClass = new \ReflectionClass(new Messages);
41
        $constantsList = $oClass->getConstants();
42
43
        if (in_array($type, $constantsList)) {
44
            if (empty($code)) {
45
                self::$messages[] = new __MSG($type, $text);
46
            } else {
47
                self::$messages[$code] = new __MSG($type, $text);
48
            }
49
        } else {
50
            throw new \Exception("Invalid message type code");
51
        }
52
    }
53
54
    /**
55
     * Push new message to the global messages list.
56
     *
57
     * @param int $type Priority type of the message.
58
     * @param string $text Text of the message.
59
     * @param string $code Code of the message in the message list (optional).
60
     *
61
     * @throws \Exception If wrong priority message type provided.
62
     */
63
    public static function addMessage($type, $text, $code = null) {
64
        return self::pushMessage($type, $text, $code);
65
    }
66
67
    /**
68
     * Returns message by it's code from global messages list.
69
     *
70
     * @param string $code Code of the message in the message list.
71
     *
72
     * @return __MSG Message object.
73
     *
74
     * @throws \Exception If message with such code is not exists.
75
     */
76
    public static function getMessage($code) {
77
        if (isset(self::$messages[$code])) {
78
            return self::$messages[$code];
79
        } else {
80
            throw new \Exception("Invalid message code");
81
        }
82
    }
83
84
    /**
85
     * Returns message text (content) silently.
86
     *
87
     * @param mixed $code Key of the message.
88
     *
89
     * @return string Message text or empty string if message doesn't exist.
90
     */
91
    public static function get($code) {
92
        try {
93
            $msg = self::getMessage($code);
94
            if (is_object($msg)) {
95
                return $msg->text;
96
            }
97
        } catch (\Exception $ex) {
98
            unset($ex);
99
        }
100
101
        return "";
102
    }
103
104
    /**
105
     * Removes message with some code from global messages list.
106
     *
107
     * @param string $code Code of the message in the message list.
108
     */
109
    public static function popMessages($code) {
110
        if (isset(self::$messages[$code])) {
111
            unset(self::$messages[$code]);
112
        }
113
    }
114
115
    /**
116
     * Removes message with some code from global messages list.
117
     *
118
     * @param string $code Code of the message in the message list.
119
     */
120
    public static function removeMessages($code) {
121
        return self::popMessages($code);
122
    }
123
124
    /**
125
     * Sort messages list with most important messages priority but save order if
126
     * priority the same.
127
     */
128
    public static function reorderMessages() {
129
        uasort(self::$messages, function ($a, $b) {
130
            return ($a->type <= $b->type);
131
        });
132
    }
133
134
    /**
135
     * Sort messages list with most important messages priority but save order if
136
     * priority the same.
137
     */
138
    public static function sortMessages() {
139
        return self::reorderMessages();
140
    }
141
142
    /**
143
     * Returns reordered by priority messages list.
144
     *
145
     * @return array<_MSG> List with reordered by prioroty messages.
146
     */
147
    public static function getMessages() {
148
        self::reorderMessages();
149
150
        return self::$messages;
151
    }
152
153
}
154
155
/**
156
 * Message item helper class.
157
 */
158
class __MSG {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
159
160
    public $type = null;
161
    public $text = "";
162
163
    public function __construct($type, $text) {
164
        $this->type = $type;
165
        $this->text = trim($text);
166
    }
167
168
}
169