Completed
Pull Request — master (#399)
by Anton
04:06
created

Messages   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 182
ccs 0
cts 84
cp 0
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A addNotice() 0 4 1
A addSuccess() 0 4 1
A addError() 0 4 1
A add() 0 5 1
B pop() 0 23 6
A popAll() 0 10 2
A count() 0 11 3
A reset() 0 4 1
A getMessagesStore() 0 4 1
A createEmptyMessagesStore() 0 10 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Messages;
12
13
use Bluz\Common\Options;
14
use Bluz\Proxy\Session;
15
use Bluz\Proxy\Translator;
16
17
/**
18
 * Realization of Flash Messages
19
 *
20
 * @package  Bluz\Messages
21
 * @author   Anton Shevchuk
22
 * @link     https://github.com/bluzphp/framework/wiki/Messages
23
 */
24
class Messages
25
{
26
    use Options;
27
28
    const TYPE_ERROR = 'error';
29
    const TYPE_SUCCESS = 'success';
30
    const TYPE_NOTICE = 'notice';
31
32
    /**
33
     * @var array list of messages types
34
     */
35
    protected $types = [
36
        self::TYPE_ERROR,
37
        self::TYPE_SUCCESS,
38
        self::TYPE_NOTICE
39
    ];
40
41
    /**
42
     * Initialize Messages container
43
     *
44
     * @return Messages
45
     */
46
    protected function init()
47
    {
48
        if (!$this->getMessagesStore()) {
49
            $this->reset();
50
        }
51
        return $this;
52
    }
53
54
    /**
55
     * Add notice
56
     *
57
     * @param  string   $message
58
     * @param  string[] $text
59
     * @return void
60
     * @since  1.0.0 added $text
61
     */
62
    public function addNotice($message, ...$text)
63
    {
64
        $this->add(self::TYPE_NOTICE, $message, ...$text);
65
    }
66
67
    /**
68
     * Add success
69
     *
70
     * @param  string   $message
71
     * @param  string[] $text
72
     * @return void
73
     * @since  1.0.0 added $text
74
     */
75
    public function addSuccess($message, ...$text)
76
    {
77
        $this->add(self::TYPE_SUCCESS, $message, ...$text);
78
    }
79
80
    /**
81
     * Add error
82
     *
83
     * @param  string   $message
84
     * @param  string[] $text
85
     * @return void
86
     * @since  1.0.0 added $text
87
     */
88
    public function addError($message, ...$text)
89
    {
90
        $this->add(self::TYPE_ERROR, $message, ...$text);
91
    }
92
93
    /**
94
     * Add message to container
95
     *
96
     * @param  string   $type One of error, notice or success
97
     * @param  string   $message
98
     * @param  string[] $text
99
     * @return void
100
     */
101
    protected function add($type, $message, ...$text)
102
    {
103
        $this->init();
104
        $this->getMessagesStore()[$type][] = Translator::translate($message, ...$text);
105
    }
106
107
    /**
108
     * Pop a message
109
     *
110
     * @param  string $type
111
     * @return \stdClass|null
112
     */
113
    public function pop($type = null)
114
    {
115
        if (!$this->getMessagesStore()) {
116
            return null;
117
        }
118
119
        if ($type !== null) {
120
            $text = array_shift($this->getMessagesStore()[$type]);
121
            if ($text) {
122
                $message = new \stdClass();
123
                $message->text = $text;
124
                $message->type = $type;
125
                return $message;
126
            }
127
        } else {
128
            foreach ($this->types as $type) {
129
                if ($message = $this->pop($type)) {
130
                    return $message;
131
                }
132
            }
133
        }
134
        return null;
135
    }
136
137
    /**
138
     * Pop all messages
139
     *
140
     * @return \ArrayObject|array
141
     */
142
    public function popAll()
143
    {
144
        if (!$this->getMessagesStore()) {
145
            return $this->createEmptyMessagesStore();
146
        }
147
148
        $messages = $this->getMessagesStore()->getArrayCopy();
149
        $this->reset();
150
        return $messages;
151
    }
152
153
    /**
154
     * Get size of messages container
155
     *
156
     * @return integer
157
     */
158
    public function count()
159
    {
160
        $size = 0;
161
        if (!$store = $this->getMessagesStore()) {
162
            return $size;
163
        }
164
        foreach ($store as $messages) {
165
            $size += sizeof($messages);
166
        }
167
        return $size;
168
    }
169
170
    /**
171
     * Reset messages
172
     *
173
     * @return void
174
     */
175
    public function reset()
176
    {
177
        Session::set('messages:store', $this->createEmptyMessagesStore());
178
    }
179
180
    /**
181
     * Returns current messages store
182
     *
183
     * @return \ArrayObject|null Returns null if store not exists yet
184
     */
185
    protected function getMessagesStore()
186
    {
187
        return Session::get('messages:store');
188
    }
189
190
    /**
191
     * Creates a new empty store for messages
192
     *
193
     * @return \ArrayObject
194
     */
195
    protected function createEmptyMessagesStore()
196
    {
197
        return new \ArrayObject(
198
            [
199
                self::TYPE_ERROR => [],
200
                self::TYPE_SUCCESS => [],
201
                self::TYPE_NOTICE => []
202
            ]
203
        );
204
    }
205
}
206