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 |
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 |
View Code Duplication |
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
|
4 |
|
} |
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 |
View Code Duplication |
if (!isset($this->data) && isset($this->session[$this->key])) { |
|
|
|
|
79
|
1 |
|
$this->data = $this->session[$this->key]; |
80
|
1 |
|
} 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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.