Completed
Pull Request — master (#451)
by Anton
13:12
created

Messages   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 171
rs 10
c 1
b 0
f 0
ccs 49
cts 49
cp 1
wmc 15
lcom 1
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addNotice() 0 4 1
A addSuccess() 0 4 1
A addError() 0 4 1
A add() 0 4 1
A pop() 0 11 2
A popAll() 0 6 1
A count() 0 11 3
A setMessagesStore() 0 4 1
A getMessagesStore() 0 7 2
A resetMessagesStore() 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
    private const TYPE_ERROR = 'error';
29
    private const TYPE_SUCCESS = 'success';
30
    private 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
     * Add notice
43
     *
44
     * @param  string   $message
45
     * @param  string[] $text
46 4
     *
47
     * @return void
48 4
     * @since  1.0.0 added $text
49 4
     */
50
    public function addNotice($message, ...$text) : void
51 4
    {
52
        $this->add(self::TYPE_NOTICE, $message, ...$text);
53
    }
54
55
    /**
56
     * Add success
57
     *
58
     * @param  string   $message
59
     * @param  string[] $text
60
     *
61
     * @return void
62
     * @since  1.0.0 added $text
63 4
     */
64
    public function addSuccess($message, ...$text) : void
65 4
    {
66 4
        $this->add(self::TYPE_SUCCESS, $message, ...$text);
67
    }
68
69
    /**
70
     * Add error
71
     *
72
     * @param  string   $message
73
     * @param  string[] $text
74
     *
75
     * @return void
76
     * @since  1.0.0 added $text
77 4
     */
78
    public function addError($message, ...$text) : void
79 4
    {
80 4
        $this->add(self::TYPE_ERROR, $message, ...$text);
81
    }
82
83
    /**
84
     * Add message to container
85
     *
86
     * @param  string   $type One of error, notice or success
87
     * @param  string   $message
88
     * @param  string[] $text
89
     *
90
     * @return void
91 4
     */
92
    protected function add($type, $message, ...$text) : void
93 4
    {
94 4
        $this->getMessagesStore()[$type][] = Translator::translate($message, ...$text);
95
    }
96
97
    /**
98
     * Pop a message by type
99
     *
100
     * @param  string $type
101
     *
102
     * @return \stdClass|null
103
     */
104
    public function pop($type) : ?\stdClass
105 4
    {
106
        $text = array_shift($this->getMessagesStore()[$type]);
107 4
        if ($text) {
108 4
            $message = new \stdClass();
109 4
            $message->text = $text;
110
            $message->type = $type;
111
            return $message;
112
        }
113
        return null;
114
    }
115
116
    /**
117
     * Pop all messages
118 4
     *
119
     * @return \ArrayObject|array
120 4
     */
121 1
    public function popAll()
122
    {
123
        $messages = $this->getMessagesStore()->getArrayCopy();
124 3
        $this->resetMessagesStore();
125 3
        return $messages;
126 3
    }
127 3
128 3
    /**
129 3
     * Get size of messages container
130 3
     *
131
     * @return integer
132
     */
133 1
    public function count() : int
134 1
    {
135 1
        $size = 0;
136
        if (!$store = $this->getMessagesStore()) {
137
            return $size;
138
        }
139 1
        foreach ($store as $messages) {
140
            $size += count($messages);
141
        }
142
        return $size;
143
    }
144
145
    /**
146
     * Reset messages
147 2
     *
148
     * @param \ArrayObject $store
149 2
     * @return void
150 1
     */
151
    protected function setMessagesStore(\ArrayObject $store) : void
152
    {
153 1
        Session::set('messages:store', $store);
154 1
    }
155 1
156
    /**
157
     * Returns current or new messages store if it not exists
158
     *
159
     * @return \ArrayObject
160
     */
161
    protected function getMessagesStore() : \ArrayObject
162
    {
163 2
        if (!$store = Session::get('messages:store')) {
164
            $this->resetMessagesStore();
165 2
        }
166 2
        return Session::get('messages:store');
167 1
    }
168
169 1
    /**
170 1
     * Reset messages
171
     *
172 1
     * @return void
173
     */
174
    protected function resetMessagesStore() : void
175
    {
176
        $this->setMessagesStore($this->createEmptyMessagesStore());
177
    }
178
179
    /**
180 4
     * Creates a new empty store for messages
181
     *
182 4
     * @return \ArrayObject
183 4
     */
184
    protected function createEmptyMessagesStore() : \ArrayObject
185
    {
186
        return new \ArrayObject(
187
            [
188
                self::TYPE_ERROR => [],
189
                self::TYPE_SUCCESS => [],
190 6
                self::TYPE_NOTICE => []
191
            ]
192 6
        );
193
    }
194
}
195