Completed
Push — develop ( c7538c...82a2b1 )
by jake
02:45
created

Messenger::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Vperyod Session Handler
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  FlashMessenger
13
 * @package   Vperyod\SessionHandler
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/vperyod/vperyod.session-handler
18
 */
19
20
namespace Vperyod\SessionHandler;
21
22
use Aura\Session\SegmentInterface as Segment;
23
24
/**
25
 * Messenger
26
 *
27
 * @category FlashMessenger
28
 * @package  Vperyod\SessionHandler
29
 * @author   Jake Johns <[email protected]>
30
 * @license  http://jnj.mit-license.org/ MIT License
31
 * @link     https://github.com/vperyod/vperyod.session-handler
32
 */
33
class Messenger
34
{
35
    /**
36
     * Default message levels
37
     */
38
    const LVL_SUCCESS = 'success';
39
    const LVL_INFO    = 'info';
40
    const LVL_WARNING = 'warning';
41
42
    /**
43
     * Session Segment
44
     *
45
     * @var Segment
46
     *
47
     * @access protected
48
     */
49
    protected $segment;
50
51
    /**
52
     * Create a flash messenger
53
     *
54
     * @param Segment $segment session storage
55
     *
56
     * @access public
57
     */
58 7
    public function __construct(Segment $segment)
59
    {
60 7
        $this->segment = $segment;
61 7
    }
62
63
    /**
64
     * Add a message
65
     *
66
     * @param string $message Message text
67
     * @param string $level   Message level
68
     *
69
     * @return $this
70
     *
71
     * @access public
72
     */
73 5
    public function add($message, $level = self::LVL_INFO)
74
    {
75 5
        $this->segment->addFlash(
76
            [
77 5
                'message' => $message,
78
                'level'   => $level
79 5
            ]
80 5
        );
81
82 5
        return $this;
83
    }
84
85
    /**
86
     * Get current messages
87
     *
88
     * @return array
89
     *
90
     * @access public
91
     */
92 1
    public function getMessages()
93
    {
94 1
        return $this->segment->getAllCurrentFlash();
95
    }
96
97
    /**
98
     * Set a success message
99
     *
100
     * @param string $message Message text
101
     *
102
     * @return $this
103
     *
104
     * @access public
105
     */
106 1
    public function success($message)
107
    {
108 1
        return $this->add($message, self::LVL_SUCCESS);
109
    }
110
111
    /**
112
     * Set an info message
113
     *
114
     * @param string $message Message text
115
     *
116
     * @return $this
117
     *
118
     * @access public
119
     */
120 1
    public function info($message)
121
    {
122 1
        return $this->add($message, self::LVL_INFO);
123
    }
124
125
    /**
126
     * Set a warning message
127
     *
128
     * @param string $message Message text
129
     *
130
     * @return $this
131
     *
132
     * @access public
133
     */
134 1
    public function warning($message)
135
    {
136 1
        return $this->add($message, self::LVL_WARNING);
137
    }
138
}
139