AllKeysList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
getKeysData() 0 1 ?
saveKeysData() 0 1 ?
A pop() 0 9 2
A push() 0 9 1
A clearExpiredKeys() 0 8 3
1
<?php
2
3
namespace PhpConsole\Storage;
4
use PhpConsole\Storage;
5
6
/**
7
 * Abstract class for stores that manipulates with all keys data in memory
8
 *
9
 * @package PhpConsole
10
 * @version 3.1
11
 * @link http://consle.com
12
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
13
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
14
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
15
 */
16
abstract class AllKeysList extends Storage {
17
18
	/**
19
	 * Get all postponed keys data
20
	 * @return array
21
	 */
22
	abstract protected function getKeysData();
23
24
	/**
25
	 * Save all postponed keys data
26
	 * @param array $keysData
27
	 */
28
	abstract protected function saveKeysData(array $keysData);
29
30
	/**
31
	 * Get postponed data from storage and delete
32
	 * @param string $key
33
	 * @return string
34
	 */
35 6
	public function pop($key) {
36 6
		$keysData = $this->getKeysData();
37 6
		if(isset($keysData[$key])) {
38 4
			$keyData = $keysData[$key]['data'];
39 4
			unset($keysData[$key]);
40 4
			$this->saveKeysData($keysData);
41 4
			return $keyData;
42
		}
43 4
	}
44
45
	/**
46
	 * Save postponed data to storage
47
	 * @param string $key
48
	 * @param string $data
49
	 */
50 6
	public function push($key, $data) {
51 6
		$keysData = $this->getKeysData();
52 6
		$this->clearExpiredKeys($keysData);
53 6
		$keysData[$key] = array(
54 6
			'time' => time(),
55 6
			'data' => $data
56
		);
57 6
		$this->saveKeysData($keysData);
58 6
	}
59
60
	/**
61
	 * Remove postponed data that is out of limit
62
	 * @param array $keysData
63
	 */
64 6
	protected function clearExpiredKeys(array &$keysData) {
65 6
		$expireTime = time() - $this->keyLifetime;
66 6
		foreach($keysData as $key => $item) {
67 2
			if($item['time'] < $expireTime) {
68 2
				unset($keysData[$key]);
69
			}
70
		}
71 6
	}
72
}
73