Passed
Push — master ( d80277...8673d0 )
by Christoph
16:23 queued 12s
created

Memory::validateSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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