Completed
Push — master ( d45aba...28528f )
by Anton
9s
created

Messages   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 182
ccs 54
cts 54
cp 1
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
A popAll() 0 10 2
A reset() 0 4 1
A getMessagesStore() 0 4 1
A createEmptyMessagesStore() 0 10 1
B pop() 0 23 6
A count() 0 11 3
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 4
    protected function init()
47
    {
48 4
        if (!$this->getMessagesStore()) {
49 4
            $this->reset();
50
        }
51 4
        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 4
    public function addNotice($message, ...$text)
63
    {
64 4
        $this->add(self::TYPE_NOTICE, $message, ...$text);
65 4
    }
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 4
    public function addSuccess($message, ...$text)
76
    {
77 4
        $this->add(self::TYPE_SUCCESS, $message, ...$text);
78 4
    }
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 4
    public function addError($message, ...$text)
89
    {
90 4
        $this->add(self::TYPE_ERROR, $message, ...$text);
91 4
    }
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 4
    protected function add($type, $message, ...$text)
102
    {
103 4
        $this->init();
104 4
        $this->getMessagesStore()[$type][] = Translator::translate($message, ...$text);
105 4
    }
106
107
    /**
108
     * Pop a message
109
     *
110
     * @param  string $type
111
     * @return \stdClass|null
112
     */
113 4
    public function pop($type = null)
114
    {
115 4
        if (!$this->getMessagesStore()) {
116 1
            return null;
117
        }
118
119 3
        if ($type !== null) {
120 3
            $text = array_shift($this->getMessagesStore()[$type]);
121 3
            if ($text) {
122 3
                $message = new \stdClass();
123 3
                $message->text = $text;
124 3
                $message->type = $type;
125 3
                return $message;
126
            }
127
        } else {
128 1
            foreach ($this->types as $type) {
129 1
                if ($message = $this->pop($type)) {
130 1
                    return $message;
131
                }
132
            }
133
        }
134 1
        return null;
135
    }
136
137
    /**
138
     * Pop all messages
139
     *
140
     * @return \ArrayObject|array
141
     */
142 638
    public function popAll()
143
    {
144 638
        if (!$this->getMessagesStore()) {
145 634
            return $this->createEmptyMessagesStore();
146
        }
147
148 4
        $messages = $this->getMessagesStore()->getArrayCopy();
149 4
        $this->reset();
150 4
        return $messages;
151
    }
152
153
    /**
154
     * Get size of messages container
155
     *
156
     * @return integer
157
     */
158 2
    public function count()
159
    {
160 2
        $size = 0;
161 2
        if (!$store = $this->getMessagesStore()) {
162 1
            return $size;
163
        }
164 1
        foreach ($store as $messages) {
165 1
            $size += sizeof($messages);
166
        }
167 1
        return $size;
168
    }
169
170
    /**
171
     * Reset messages
172
     *
173
     * @return void
174
     */
175 4
    public function reset()
176
    {
177 4
        Session::set('messages:store', $this->createEmptyMessagesStore());
178 4
    }
179
180
    /**
181
     * Returns current messages store
182
     *
183
     * @return \ArrayObject|null Returns null if store not exists yet
184
     */
185 638
    protected function getMessagesStore()
186
    {
187 638
        return Session::get('messages:store');
188
    }
189
190
    /**
191
     * Creates a new empty store for messages
192
     *
193
     * @return \ArrayObject
194
     */
195 638
    protected function createEmptyMessagesStore()
196
    {
197 638
        return new \ArrayObject(
198
            [
199 638
                self::TYPE_ERROR => [],
200 638
                self::TYPE_SUCCESS => [],
201 638
                self::TYPE_NOTICE => []
202
            ]
203
        );
204
    }
205
}
206