Passed
Push — master ( b26d44...e096d1 )
by
unknown
01:42
created

Flash::add()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
crap 3.0123
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 9
	public function __construct( &$storage ) {
30 9
		if ( $this->isValidStorage( $storage ) ) {
31 9
			if ( ! isset( $storage[ $this->storage_key ] ) ) {
32 9
				$storage[ $this->storage_key ] = [];
33
			}
34 9
			$this->storage = &$storage[ $this->storage_key ];
35
		}
36 9
	}
37
38
	/**
39
	 * Return whether a storage object is valid
40
	 * 
41
	 * @param  mixed   $storage
42
	 * @return boolean
43
	 */
44 9
	protected function isValidStorage( $storage ) {
45 9
		return $storage !== null;
46
	}
47
48
	/**
49
	 * Throw an exception if storage is not valid
50
	 * 
51
	 * @throws Exception
52
	 * @return null
53
	 */
54 8
	protected function validateStorage() {
55 8
		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 8
	}
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 8
	public function peek( $key = null ) {
76 8
		$this->validateStorage();
77
		
78 8
		if ( $key === null ) {
79 3
			return $this->storage;
80
		}
81
		
82 8
		if ( isset( $this->storage[ $key ] ) ) {
83 4
			return $this->storage[ $key ];
84
		}
85
86 8
		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 7
	public function add( $key, $new_items ) {
110 7
		$this->validateStorage();
111
		
112 7
		$new_items = is_array( $new_items ) ? $new_items : [$new_items];
113
114 7
		$items = (array) $this->peek( $key );
115 7
		$items = array_merge( $items, $new_items );
116
		
117 7
		if ( $key === null ) {
118
			$this->storage = $items;
119
		} else {
120 7
			$this->storage[ $key ] = $items;
121
		}
122 7
	}
123
124
	/**
125
	 * Clear the entire storage or the values for a key
126
	 * 
127
	 * @param  string|null $key
128
	 * @return null
129
	 */
130 3
	public function clear( $key = null ) {
131 3
		$this->validateStorage();
132
		
133 3
		if ( $key === null ) {
134 1
			foreach ( $this->storage as $key => $value ) {
135 1
				$this->storage[ $key ] = [];
136
			}
137
		} else {
138 2
			$this->storage[ $key ] = [];
139
		}
140
	}
141
}