Passed
Push — master ( 16ecde...2e1789 )
by Atanas
02:15
created

Flash::enabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CarbonFramework\Flash;
4
5
use ArrayAccess;
6
use Exception;
7
8
/**
9
 * Provide a way to flash data into the session for the next request
10
 */
11
class Flash {
12
	/**
13
	 * Root storage array or object implementing ArrayAccess
14
	 *
15
	 * @var array|\ArrayAccess
16
	 */
17
	protected $root_storage = null;
18
19
	/**
20
	 * Child storage array or object implementing ArrayAccess
21
	 *
22
	 * @var array|\ArrayAccess
23
	 */
24
	protected $storage = null;
25
26
	/**
27
	 * Key to store flashed data in storage with
28
	 *
29
	 * @var string
30
	 */
31
	protected $storage_key = '__carbonFrameworkFlash';
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param array|\ArrayAccess $storage
37
	 */
38 1
	public function __construct( &$storage ) {
39 1
		$this->setStorage( $storage );
40 1
	}
41
42
	/**
43
	 * Return whether a storage object is valid
44
	 *
45
	 * @param  mixed   $storage
46
	 * @return boolean
47
	 */
48
	protected function isValidStorage( $storage ) {
49
		return ( is_array( $storage ) || is_a( $storage, ArrayAccess::class ) );
50
	}
51
52
	/**
53
	 * Throw an exception if storage is not valid
54
	 *
55
	 * @throws Exception
56
	 * @return null
57
	 */
58 1
	protected function validateStorage() {
59 1
		if ( ! $this->isValidStorage( $this->storage ) ) {
60 1
			throw new Exception( 'Attempted to use Flash without an active session. Did you forget to call session_start()?' );
61
		}
62
	}
63
64
	/**
65
	 * Get the storage for flash messages
66
	 *
67
	 * @return array|\ArrayAccess
68
	 */
69 3
	public function getStorage() {
70 3
		return $this->root_storage;
71
	}
72
73
	/**
74
	 * Set the storage for flash messages
75
	 *
76
	 * @param  array|\ArrayAccess $storage
77
	 * @return null
78
	 */
79 2
	public function setStorage( &$storage ) {
80 2
		if ( ! $this->isValidStorage( $storage ) ) {
81 1
			return;
82
		}
83
84 2
		if ( ! isset( $storage[ $this->storage_key ] ) ) {
85 2
			$storage[ $this->storage_key ] = [];
86 2
		}
87
88 2
		$this->root_storage = &$storage;
89 2
		$this->storage = &$this->root_storage[ $this->storage_key ];
90 2
	}
91
92
	/**
93
	 * Return whether the flash service is enabled
94
	 *
95
	 * @return boolean
96
	 */
97 1
	public function enabled() {
98 1
		return $this->isValidStorage( $this->storage );
99
	}
100
101
	/**
102
	 * Get the entire storage or the values for a key
103
	 *
104
	 * @param  string|null $key
105
	 * @return array|\ArrayAccess
106
	 */
107 7
	public function peek( $key = null ) {
108 7
		$this->validateStorage();
109
110 7
		if ( $key === null ) {
111 3
			return $this->storage;
112
		}
113
114 7
		if ( isset( $this->storage[ $key ] ) ) {
115 3
			return $this->storage[ $key ];
116
		}
117
118 7
		return [];
119
	}
120
121
	/**
122
	 * Get and clear the entire storage or the values for a key
123
	 *
124
	 * @param  string|null $key
125
	 * @return array|\ArrayAccess
126
	 */
127 1
	public function get( $key = null ) {
128 1
		$this->validateStorage();
129
130 1
		$items = $this->peek( $key );
131 1
		$this->clear( $key );
132 1
		return $items;
133
	}
134
135
	/**
136
	 * Add values for a key
137
	 *
138
	 * @param string $key
139
	 * @param mixed  $new_items
140
	 */
141 5
	public function add( $key, $new_items ) {
142 5
		$this->validateStorage();
143
144 5
		$new_items = is_array( $new_items ) ? $new_items : [$new_items];
145
146 5
		$items = (array) $this->peek( $key );
147 5
		$items = array_merge( $items, $new_items );
148
149 5
		$this->storage[ $key ] = $items;
150 5
	}
151
152
	/**
153
	 * Clear the entire storage or the values for a key
154
	 *
155
	 * @param  string|null $key
156
	 * @return null
157
	 */
158 2
	public function clear( $key = null ) {
159 2
		$this->validateStorage();
160
161 2
		if ( $key === null ) {
162 1
			foreach ( $this->storage as $key => $value ) {
163 1
				$this->storage[ $key ] = [];
164 1
			}
165 1
		} else {
166 1
			$this->storage[ $key ] = [];
167
		}
168 2
	}
169
}
170