Passed
Push — master ( 410431...1494d3 )
by Atanas
03:46
created

Flash::setStore()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
ccs 3
cts 3
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Flash;
4
5
use ArrayAccess;
6
use Exception;
7
use WPEmerge\Helpers\Mixed;
8
use WPEmerge\Support\Arr;
9
10
/**
11
 * Provide a way to flash data into the session for the next request.
12
 */
13
class Flash {
14
	/**
15
	 * Keys for different request contexts.
16
	 */
17
	const CURRENT_KEY = 'current';
18
	const NEXT_KEY = 'next';
19
20
	/**
21
	 * Key to store flashed data in store with.
22
	 *
23
	 * @var string
24
	 */
25
	protected $store_key = '';
26
27
	/**
28
	 * Root store array or object implementing ArrayAccess.
29
	 *
30
	 * @var array|\ArrayAccess
31
	 */
32
	protected $store = null;
33
34
	/**
35
	 * Flash store array.
36
	 *
37
	 * @var array
38 1
	 */
39 1
	protected $flashed = [];
40 1
41
	/**
42
	 * Constructor.
43
	 *
44
	 * @codeCoverageIgnore
45
	 * @param array|\ArrayAccess $store
46
	 * @param string             $store_key
47
	 */
48 3
	public function __construct( &$store, $store_key = '__wpemergeFlash' ) {
49 3
		$this->store_key = $store_key;
50
		$this->setStore( $store );
51
	}
52
53
	/**
54
	 * Get whether a store object is valid.
55
	 *
56
	 * @param  mixed   $store
57
	 * @return boolean
58 9
	 */
59 9
	protected function isValidStore( $store ) {
60 1
		return ( is_array( $store ) || $store instanceof ArrayAccess );
61
	}
62 8
63
	/**
64
	 * Throw an exception if store is not valid.
65
	 *
66
	 * @throws Exception
67
	 * @return void
68
	 */
69 3
	protected function validateStore() {
70 3
		if ( ! $this->isValidStore( $this->store ) ) {
71
			throw new Exception( 'Attempted to use Flash without an active session. Did you forget to call session_start()?' );
72
		}
73
	}
74
75
	/**
76
	 * Get the store for flash messages.
77
	 *
78
	 * @return array|\ArrayAccess
79 2
	 */
80 2
	public function getStore() {
81 1
		return $this->store;
82
	}
83
84 2
	/**
85 2
	 * Set the store for flash messages.
86 2
	 *
87
	 * @param  array|\ArrayAccess $store
88 2
	 * @return void
89 2
	 */
90 2
	public function setStore( &$store ) {
91
		if ( ! $this->isValidStore( $store ) ) {
92
			return;
93
		}
94
95
		$this->store = &$store;
96
97 1
		if ( ! isset( $this->store[ $this->store_key ] ) ) {
98 1
			$this->store[ $this->store_key ] = [
99
				static::CURRENT_KEY => [],
100
				static::NEXT_KEY => [],
101
			];
102
		}
103
104
		$this->flashed = $store[ $this->store_key ];
105
	}
106
107 8
	/**
108 8
	 * Get whether the flash service is enabled.
109
	 *
110 7
	 * @return boolean
111 3
	 */
112
	public function enabled() {
113
		return $this->isValidStore( $this->store );
114 7
	}
115 3
116
	/**
117
	 * Get the entire store or the values for a key for a request.
118 7
	 *
119
	 * @param  string      $request_key
120
	 * @param  string|null $key
121
	 * @param  mixed       $default
122
	 * @return mixed
123
	 */
124
	protected function getFromRequest( $request_key, $key = null, $default = [] ) {
125
		$this->validateStore();
126
127 1
		if ( $key === null ) {
128 1
			return Arr::get( $this->flashed, $request_key, $default );
129
		}
130 1
131 1
		return Arr::get( $this->flashed[ $request_key ], $key, $default );
132 1
	}
133
134
	/**
135
	 * Add values for a key for a request.
136
	 *
137
	 * @param  string  $request_key
138
	 * @param  string  $key
139
	 * @param  mixed   $new_items
140
	 * @return void
141 5
	 */
142 5
	protected function addToRequest( $request_key, $key, $new_items ) {
143
		$this->validateStore();
144 5
145
		$new_items = Mixed::toArray( $new_items );
146 5
		$items = Mixed::toArray( $this->get( $key, [] ) );
147 5
		$this->flashed[ $request_key ][ $key ] = array_merge( $items, $new_items );
148
	}
149 5
150 5
	/**
151
	 * Remove all values or values for a key from a request.
152
	 *
153
	 * @param  string      $request_key
154
	 * @param  string|null $key
155
	 * @return void
156
	 */
157
	protected function clearFromRequest( $request_key, $key = null ) {
158 2
		$this->validateStore();
159 2
160
		$keys = $key === null ? array_keys( $this->flashed[ $request_key ] ) : [$key];
161 2
		foreach ( $keys as $k ) {
162 1
			unset( $this->flashed[ $request_key ][ $k ] );
163 1
		}
164 1
	}
165 1
166 1
	/**
167
	 * Add values for a key for the next request.
168 2
	 *
169
	 * @param  string $key
170
	 * @param  mixed  $new_items
171
	 * @return void
172
	 */
173
	public function add( $key, $new_items ) {
174
		$this->addToRequest( static::NEXT_KEY, $key, $new_items );
175
	}
176
177
	/**
178
	 * Add values for a key for the current request.
179
	 *
180
	 * @param string $key
181
	 * @param mixed  $new_items
182
	 */
183
	public function addNow( $key, $new_items ) {
184
		$this->addToRequest( static::CURRENT_KEY, $key, $new_items );
185
	}
186
187
	/**
188
	 * Get the entire store or the values for a key for the current request.
189
	 *
190
	 * @param  string|null $key
191
	 * @param  mixed       $default
192
	 * @return mixed
193
	 */
194
	public function get( $key = null, $default = [] ) {
195
		return $this->getFromRequest( static::CURRENT_KEY, $key, $default );
196
	}
197
198
	/**
199
	 * Get the entire store or the values for a key for the next request.
200
	 *
201
	 * @param  string|null $key
202
	 * @param  mixed       $default
203
	 * @return mixed
204
	 */
205
	public function getNext( $key = null, $default = [] ) {
206
		return $this->getFromRequest( static::NEXT_KEY, $key, $default );
207
	}
208
209
	/**
210
	 * Clear the entire store or the values for a key for the current request.
211
	 *
212
	 * @param  string|null $key
213
	 * @return void
214
	 */
215
	public function clear( $key = null ) {
216
		$this->clearFromRequest( static::CURRENT_KEY, $key );
217
	}
218
219
	/**
220
	 * Clear the entire store or the values for a key for the next request.
221
	 *
222
	 * @param  string|null $key
223
	 * @return void
224
	 */
225
	public function clearNext( $key = null ) {
226
		$this->clearFromRequest( static::NEXT_KEY, $key );
227
	}
228
229
	/**
230
	 * Shift current store and replace it with next store.
231
	 *
232
	 * @return void
233
	 */
234
	public function shift() {
235
		$this->validateStore();
236
237
		$this->flashed[ static::CURRENT_KEY ] = $this->flashed[ static::NEXT_KEY ];
238
		$this->flashed[ static::NEXT_KEY ] = [];
239
	}
240
241
	/**
242
	 * Save flashed data to store.
243
	 *
244
	 * @return void
245
	 */
246
	public function save() {
247
		$this->validateStore();
248
249
		$this->store[ $this->store_key ] = $this->flashed;
250
	}
251
}
252