Passed
Push — master ( 120bfe...f39408 )
by Yuri
04:03
created

MessageManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _hasMessageType() 0 3 1
A _hasMessage() 0 15 4
A _addMessage() 0 17 4
A __construct() 0 3 1
A _clearMessage() 0 12 3
A _compileMessage() 0 8 2
1
<?php
2
3
namespace Tamtamchik\SimpleFlash\Core;
4
5
use Tamtamchik\SimpleFlash\TemplateInterface;
6
7
class MessageManager extends TemplateManager
8
{
9
    private $types = [
10
        'error',
11
        'warning',
12
        'info',
13
        'success',
14
    ];
15
16
    public function __construct(TemplateInterface $template)
17
    {
18
        parent::__construct($template);
19
    }
20
21
    /**
22
     * Add message to Session.
23
     *
24
     * @param string $message - message text
25
     * @param string $type - message type: success, info, warning, error
26
     */
27
    protected function _addMessage(string $message = '', string $type = 'info')
28
    {
29
        $session = $this->getSession();
30
31
        $type = strip_tags($type);
32
33
        if (empty($message) || ! in_array($type, $this->types)) {
34
            return;
35
        }
36
37
        if ( ! array_key_exists($type, $session)) {
38
            $session[$type] = [];
39
        }
40
41
        $session[$type][] = $message;
42
43
        $this->setSession($session);
44
    }
45
46
    /**
47
     * Builds messages for a single type.
48
     *
49
     * @param array $flashes - array of messages to show
50
     * @param string $type - message type: success, info, warning, error
51
     *
52
     * @return string - HTML with flash messages
53
     */
54
    protected function _compileMessage(array $flashes, string $type): string
55
    {
56
        $messages = '';
57
        foreach ($flashes as $msg) {
58
            $messages .= $this->_getTemplate()->wrapMessage($msg);
59
        }
60
61
        return $this->_getTemplate()->wrapMessages($messages, $type);
62
    }
63
64
    /**
65
     * Returns all messages from container.
66
     *
67
     * @param string|null $type - message type: success, info, warning, error
68
     *
69
     * @return bool
70
     */
71
    public function _hasMessageType(string $type): bool
72
    {
73
        return in_array($type, $this->types);
74
    }
75
76
    /**
77
     * Returns if there are any messages in container.
78
     *
79
     * @param string|null $type - message type: success, info, warning, error
80
     *
81
     * @return bool
82
     */
83
    public function _hasMessage(string $type = null): bool
84
    {
85
        $session = $this->getSession();
86
87
        if ( ! is_null($type)) {
88
            return ! empty($session[$type]);
89
        }
90
91
        foreach ($this->types as $type) {
92
            if ( ! empty($session[$type])) {
93
                return true;
94
            }
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * Clears messages from session store.
102
     *
103
     * @param string|null $type - message type: success, info, warning, error
104
     */
105
    protected function _clearMessage(string $type = null)
106
    {
107
        if (is_null($type)) {
108
            $this->setSession([]);
109
            return;
110
        }
111
112
        $session = $this->getSession();
113
        if (array_key_exists($type, $session)) {
114
            unset($session[$type]);
115
        }
116
        $this->setSession($session);
117
    }
118
}
119