Completed
Pull Request — master (#387)
by Anton
05:25
created

Messages::popAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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