Completed
Push — master ( 7f6ab6...0b9ac4 )
by Arnold
07:54 queued 04:52
created

Flash::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Jasny\Controller\Session;
4
5
/**
6
 * Class for the flash message
7
 */
8
class Flash
9
{
10
    /**
11
     * @var array|null
12
     */
13
    protected $data;
14
    
15
    /**
16
     * @var array|\ArrayObject
17
     */
18
    protected $session;
19
20
    /**
21
     * Session key for flash
22
     * @var string
23
     */
24
    protected $key = 'flash';
25
    
26
    
27
    /**
28
     * Class constructor
29
     * 
30
     * @param array|\ArrayObject $session
31
     */
32 9
    public function __construct(&$session)
33
    {
34 9
        $this->session =& $session;
35 9
    }
36
    
37
    /**
38
     * Check if the flash is set.
39
     * 
40
     * @return boolean
41
     */
42 4
    public function isIssued()
43
    {
44 4
        return isset($this->session[$this->key]);
45
    }
46
    
47
    /**
48
     * Set the flash.
49
     * 
50
     * @param string $type     flash type, eg. 'error', 'notice' or 'success'
51
     * @param mixed  $message  flash message
52
     */
53 2
    public function set($type, $message)
54
    {
55 2
        $this->session[$this->key] = compact('type', 'message');
56 2
    }
57
    
58
    /**
59
     * Get the flash.
60
     * 
61
     * @return object
62
     */
63 7
    public function get()
64
    {
65 7
        if (!isset($this->data) && isset($this->session[$this->key])) {
66 4
            $this->data = $this->session[$this->key];
67 4
            unset($this->session[$this->key]);
68
        }
69
        
70 7
        return $this->data ? (object)$this->data : null;
71
    }
72
    
73
    /**
74
     * Reissue the flash.
75
     */
76 2
    public function reissue()
77
    {
78 2
        if (!isset($this->data) && isset($this->session[$this->key])) {
79 1
            $this->data = $this->session[$this->key];
80
        } else {
81 1
            $this->session[$this->key] = $this->data;
82
        }
83 2
    }
84
    
85
    /**
86
     * Clear the flash.
87
     */
88 3
    public function clear()
89
    {
90 3
        $this->data = null;
91 3
        unset($this->session[$this->key]);
92 3
    }     
93
    
94
    /**
95
     * Get the flash type
96
     * 
97
     * @return string
98
     */
99 2
    public function getType()
100
    {
101 2
        $data = $this->get();
102 2
        return isset($data) ? $data->type : null;
103
    }
104
    
105
    /**
106
     * Get the flash message
107
     * 
108
     * @return string
109
     */
110 2
    public function getMessage()
111
    {
112 2
        $data = $this->get();
113 2
        return isset($data) ? $data->message : null;
114
    }
115
    
116
    /**
117
     * Cast object to string
118
     * 
119
     * @return string
120
     */
121 2
    public function __toString()
122
    {
123 2
        return (string)$this->getMessage();
124
    }
125
}
126