Passed
Push — master ( 29b5f3...ca4ac3 )
by Yuri
04:18 queued 02:43
created

Engine::isReadyForDisplay()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 7
rs 9.6111
1
<?php
2
3
namespace Tamtamchik\SimpleFlash\Core;
4
5
use Tamtamchik\SimpleFlash\Exceptions\FlashTemplateNotFoundException;
6
use Tamtamchik\SimpleFlash\TemplateFactory;
7
use Tamtamchik\SimpleFlash\TemplateInterface;
8
use Tamtamchik\SimpleFlash\Templates;
9
10
/**
11
 * Class Engine.
12
 */
13
class Engine extends MessageManager
14
{
15
    /**
16
     * Creates flash container from session.
17
     *
18
     * @param TemplateInterface $template
19
     */
20
    public function __construct(TemplateInterface $template)
21
    {
22
        parent::__construct($template);
23
    }
24
25
    /**
26
     * If requested as string will HTML will be returned.
27
     *
28
     * @return string - HTML with flash messages
29
     *
30
     * @throws FlashTemplateNotFoundException
31
     */
32
    public function __toString()
33
    {
34
        return $this->display();
35
    }
36
37
    /**
38
     * Getter for $template.
39
     *
40
     * @return TemplateInterface
41
     */
42
    public function getTemplate(): ?TemplateInterface
43
    {
44
        return $this->_getTemplate();
45
    }
46
47
    /**
48
     * Setter for $template.
49
     *
50
     * @param TemplateInterface $template
51
     *
52
     * @return Engine $this
53
     */
54
    public function setTemplate(TemplateInterface $template): Engine
55
    {
56
        $this->_setTemplate($template);
57
58
        return $this;
59
    }
60
61
    /**
62
     * Returns Bootstrap ready HTML for Engine messages.
63
     *
64
     * @param string|null $type - message type: success, info, warning, error
65
     * @param string|null $template - template name from Templates class
66
     *
67
     * @return string - HTML with flash messages
68
     *
69
     * @throws FlashTemplateNotFoundException
70
     */
71
    public function display(string $type = null, string $template = null): string
72
    {
73
        $result = '';
74
        $session = $this->getSession();
75
76
        if ($this->isReadyForDisplay($session, $type)) {
77
            return $result;
78
        }
79
80
        if ( ! is_null($template)) {
81
            $this->setTemplate(TemplateFactory::create($template));
82
        }
83
84
        if (is_null($type)) {
85
            foreach ($session as $messageType => $messages) {
86
                $result .= $this->_compileMessage($messages, $messageType);
87
            }
88
        } else {
89
            $result .= $this->_compileMessage($session[$type], $type);
90
        }
91
92
        $this->clear($type);
93
94
        return $result;
95
    }
96
97
    /**
98
     * @param array $session
99
     * @param string|null $type
100
     * @return bool
101
     */
102
    private function isReadyForDisplay(array $session, string $type = null): bool
103
    {
104
        $isEmptySession = empty($session);
105
        $isTypeNotInSession = ! is_null($type) && ! array_key_exists($type, $session);
106
        $isTypeNotInTypes = ! is_null($type) && ! $this->_hasMessageType($type);
107
108
        return $isEmptySession || $isTypeNotInSession || $isTypeNotInTypes;
109
    }
110
111
    /**
112
     * Returns if there are any messages in container.
113
     *
114
     * @param string|null $type - message type: success, info, warning, error
115
     *
116
     * @return bool
117
     */
118
    public function some(string $type = null): bool
119
    {
120
        return $this->_hasMessage($type);
121
    }
122
123
    /**
124
     * Clears messages from session store.
125
     *
126
     * @param string|null $type - message type: success, info, warning, error
127
     *
128
     * @return Engine $this
129
     */
130
    public function clear(string $type = null): Engine
131
    {
132
        $this->_clearMessage($type);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Base method for adding messages to flash.
139
     *
140
     * @param string|string[] $message - message text
141
     * @param string $type - message type: success, info, warning, error
142
     *
143
     * @return Engine $this
144
     */
145
    public function message($message = '', string $type = 'info'): Engine
146
    {
147
        if (is_array($message)) {
148
            foreach ($message as $issue) {
149
                $this->_addMessage($issue, $type);
150
            }
151
        } else {
152
            $this->_addMessage($message, $type);
153
        }
154
        return $this;
155
    }
156
157
    /**
158
     * Shortcut for error message.
159
     *
160
     * @param string|string[] $message - message text
161
     *
162
     * @return Engine $this
163
     */
164
    public function error($message): Engine
165
    {
166
        return $this->message($message, 'error');
167
    }
168
169
    /**
170
     * Shortcut for warning message.
171
     *
172
     * @param string|string[] $message - message text
173
     *
174
     * @return Engine $this
175
     */
176
    public function warning($message): Engine
177
    {
178
        return $this->message($message, 'warning');
179
    }
180
181
    /**
182
     * Shortcut for info message.
183
     *
184
     * @param string|string[] $message - message text
185
     *
186
     * @return Engine $this
187
     */
188
    public function info($message): Engine
189
    {
190
        return $this->message($message);
191
    }
192
193
    /**
194
     * Shortcut for success message.
195
     *
196
     * @param string|string[] $message - message text
197
     *
198
     * @return Engine $this
199
     */
200
    public function success($message): Engine
201
    {
202
        return $this->message($message, 'success');
203
    }
204
205
    /**
206
     * Returns Bootstrap ready HTML for messages.
207
     *
208
     * @param string|null $type - message type: success, info, warning, error
209
     *
210
     * @throws FlashTemplateNotFoundException
211
     */
212
    public function displayBootstrap(string $type = null): string
213
    {
214
        return $this->display($type, Templates::BOOTSTRAP);
215
    }
216
217
    /**
218
     * Returns Foundation ready HTML for messages.
219
     *
220
     * @param string|null $type - message type: success, info, warning, error
221
     *
222
     * @throws FlashTemplateNotFoundException
223
     */
224
    public function displayFoundation(string $type = null): string
225
    {
226
        return $this->display($type, Templates::FOUNDATION);
227
    }
228
229
    /**
230
     * Returns Bulma ready HTML for messages.
231
     *
232
     * @param string|null $type - message type: success, info, warning, error
233
     *
234
     * @throws FlashTemplateNotFoundException
235
     */
236
    public function displayBulma(string $type = null): string
237
    {
238
        return $this->display($type, Templates::BULMA);
239
    }
240
241
    /**
242
     * Returns Materialize ready HTML for messages.
243
     *
244
     * @param string|null $type - message type: success, info, warning, error
245
     *
246
     * @throws FlashTemplateNotFoundException
247
     */
248
    public function displayMaterialize(string $type = null): string
249
    {
250
        return $this->display($type, Templates::MATERIALIZE);
251
    }
252
253
    /**
254
     * Returns Tailwind ready HTML for messages.
255
     *
256
     * @param string|null $type - message type: success, info, warning, error
257
     *
258
     * @throws FlashTemplateNotFoundException
259
     */
260
    public function displayTailwind(string $type = null): string
261
    {
262
        return $this->display($type, Templates::TAILWIND);
263
    }
264
265
    /**
266
     * Returns Primer ready HTML for messages.
267
     *
268
     * @param string|null $type - message type: success, info, warning, error
269
     *
270
     * @throws FlashTemplateNotFoundException
271
     */
272
    public function displayPrimer(string $type = null): string
273
    {
274
        return $this->display($type, Templates::PRIMER);
275
    }
276
277
    /**
278
     * Returns UiKit ready HTML for messages.
279
     *
280
     * @param string|null $type - message type: success, info, warning, error
281
     *
282
     * @throws FlashTemplateNotFoundException
283
     */
284
    public function displayUiKit(string $type = null): string
285
    {
286
        return $this->display($type, Templates::UIKIT);
287
    }
288
289
    /**
290
     * Returns Semantic ready HTML for messages.
291
     *
292
     * @param string|null $type - message type: success, info, warning, error
293
     *
294
     * @throws FlashTemplateNotFoundException
295
     */
296
    public function displaySemantic(string $type = null): string
297
    {
298
        return $this->display($type, Templates::SEMANTIC);
299
    }
300
301
    /**
302
     * Returns Spectre ready HTML for messages.
303
     *
304
     * @param string|null $type - message type: success, info, warning, error
305
     *
306
     * @throws FlashTemplateNotFoundException
307
     */
308
    public function displaySpectre(string $type = null): string
309
    {
310
        return $this->display($type, Templates::SPECTRE);
311
    }
312
313
    /**
314
     * Returns Spectre ready HTML for messages.
315
     *
316
     * @param string|null $type - message type: success, info, warning, error
317
     *
318
     * @throws FlashTemplateNotFoundException
319
     */
320
    public function displayHalfmoon(string $type = null): string
321
    {
322
        return $this->display($type, Templates::HALFMOON);
323
    }
324
}
325