Passed
Push — master ( 099b8d...379f19 )
by
unknown
01:50
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 Exception;
6
7
/**
8
 * Provide a way to flash data into the session for the next request
9
 */
10
class Flash {
11
	/**
12
	 * Storage array or object implementing ArrayAccess
13
	 * @var array|\ArrayAccess
14
	 */
15
	protected $storage = null;
16
17
	/**
18
	 * Key to store flashed data in storage with
19
	 *
20
	 * @var string
21
	 */
22
	protected $storage_key = '__carbonFrameworkFlash';
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param array|\ArrayAccess $storage
28
	 */
29
	public function __construct( &$storage ) {
30
		if ( $this->isValidStorage( $storage ) ) {
31
			if ( ! isset( $storage[ $this->storage_key ] ) ) {
32
				$storage[ $this->storage_key ] = [];
33
			}
34
			$this->storage = &$storage[ $this->storage_key ];
35
		}
36
	}
37
38
	/**
39
	 * Return whether a storage object is valid
40
	 *
41
	 * @param  mixed   $storage
42
	 * @return boolean
43
	 */
44
	protected function isValidStorage( $storage ) {
45
		return $storage !== null;
46
	}
47
48
	/**
49
	 * Throw an exception if storage is not valid
50
	 *
51
	 * @throws Exception
52
	 * @return null
53
	 */
54
	protected function validateStorage() {
55
		if ( ! $this->isValidStorage( $this->storage ) ) {
56
			throw new Exception( 'Attempted to use Flash without an active session. Did you forget to call session_start()?' );
57
		}
58
	}
59
60
	/**
61
	 * Return whether the flash service is enabled
62
	 *
63
	 * @return boolean
64
	 */
65 1
	public function enabled() {
66 1
		return $this->isValidStorage( $this->storage );
67
	}
68
69
	/**
70
	 * Get the entire storage or the values for a key
71
	 *
72
	 * @param  string|null $key
73
	 * @return array|\ArrayAccess
74
	 */
75 7
	public function peek( $key = null ) {
76 7
		$this->validateStorage();
77
78 7
		if ( $key === null ) {
79 3
			return $this->storage;
80
		}
81
82 7
		if ( isset( $this->storage[ $key ] ) ) {
83 3
			return $this->storage[ $key ];
84
		}
85
86 7
		return [];
87
	}
88
89
	/**
90
	 * Get and clear the entire storage or the values for a key
91
	 *
92
	 * @param  string|null $key
93
	 * @return array|\ArrayAccess
94
	 */
95 1
	public function get( $key = null ) {
96 1
		$this->validateStorage();
97
98 1
		$items = $this->peek( $key );
99 1
		$this->clear( $key );
100 1
		return $items;
101
	}
102
103
	/**
104
	 * Add values for a key
105
	 *
106
	 * @param string $key
107
	 * @param mixed  $new_items
108
	 */
109 5
	public function add( $key, $new_items ) {
110 5
		$this->validateStorage();
111
112 5
		$new_items = is_array( $new_items ) ? $new_items : [$new_items];
113
114 5
		$items = (array) $this->peek( $key );
115 5
		$items = array_merge( $items, $new_items );
116
117 5
		if ( $key === null ) {
118
			$this->storage = $items;
119
		} else {
120 5
			$this->storage[ $key ] = $items;
121
		}
122 5
	}
123
124
	/**
125
	 * Clear the entire storage or the values for a key
126
	 *
127
	 * @param  string|null $key
128
	 * @return null
129
	 */
130 2
	public function clear( $key = null ) {
131 2
		$this->validateStorage();
132
133 2
		if ( $key === null ) {
134 1
			foreach ( $this->storage as $key => $value ) {
135 1
				$this->storage[ $key ] = [];
136 1
			}
137 1
		} else {
138 1
			$this->storage[ $key ] = [];
139
		}
140 2
	}
141
}
142