Completed
Push — master ( fd844f...b84825 )
by Roeland
10:13
created

Memory::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author Jörn Friedrich Dreyer <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Phil Davis <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud, Inc.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Session;
28
29
use Exception;
30
use OCP\Session\Exceptions\SessionNotAvailableException;
31
32
/**
33
 * Class Internal
34
 *
35
 * store session data in an in-memory array, not persistent
36
 *
37
 * @package OC\Session
38
 */
39
class Memory extends Session {
40
	protected $data;
41
42
	public function __construct($name) {
43
		//no need to use $name since all data is already scoped to this instance
44
		$this->data = array();
45
	}
46
47
	/**
48
	 * @param string $key
49
	 * @param integer $value
50
	 */
51
	public function set($key, $value) {
52
		$this->validateSession();
53
		$this->data[$key] = $value;
54
	}
55
56
	/**
57
	 * @param string $key
58
	 * @return mixed
59
	 */
60
	public function get($key) {
61
		if (!$this->exists($key)) {
62
			return null;
63
		}
64
		return $this->data[$key];
65
	}
66
67
	/**
68
	 * @param string $key
69
	 * @return bool
70
	 */
71
	public function exists($key) {
72
		return isset($this->data[$key]);
73
	}
74
75
	/**
76
	 * @param string $key
77
	 */
78
	public function remove($key) {
79
		$this->validateSession();
80
		unset($this->data[$key]);
81
	}
82
83
	public function clear() {
84
		$this->data = array();
85
	}
86
87
	/**
88
	 * Stub since the session ID does not need to get regenerated for the cache
89
	 *
90
	 * @param bool $deleteOldSession
91
	 */
92
	public function regenerateId($deleteOldSession = true) {}
93
94
	/**
95
	 * Wrapper around session_id
96
	 *
97
	 * @return string
98
	 * @throws SessionNotAvailableException
99
	 * @since 9.1.0
100
	 */
101
	public function getId() {
102
		throw new SessionNotAvailableException('Memory session does not have an ID');
103
	}
104
105
	/**
106
	 * Helper function for PHPUnit execution - don't use in non-test code
107
	 */
108
	public function reopen() {
109
		$this->sessionClosed = false;
110
	}
111
112
	/**
113
	 * In case the session has already been locked an exception will be thrown
114
	 *
115
	 * @throws Exception
116
	 */
117
	private function validateSession() {
118
		if ($this->sessionClosed) {
119
			throw new Exception('Session has been closed - no further changes to the session are allowed');
120
		}
121
	}
122
}
123