Passed
Push — master ( 4a88c4...ef8e8c )
by Aimeos
11:36 queued 08:59
created

Typo3::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2014-2022
7
 * @package MW
8
 * @subpackage Session
9
 */
10
11
12
namespace Aimeos\Base\Session;
13
14
15
/**
16
 * Managing session data using the TYPO3 session
17
 *
18
 * @package MW
19
 * @subpackage Session
20
 */
21
class Typo3 extends Base implements \Aimeos\Base\Session\Iface
22
{
23
	private $user;
24
25
26
	/**
27
	 * Initializes the Typo3 session object.
28
	 *
29
	 * @param \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication $user Typo3 user object (FE or BE)
30
	 */
31
	public function __construct( \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication $user )
32
	{
33
		$this->user = $user;
34
	}
35
36
37
	/**
38
	 * Sets a list of key/value pairs.
39
	 *
40
	 * @param array $values Associative list of key/value pairs
41
	 * @return \Aimeos\Base\Session\Iface Session instance for method chaining
42
	 */
43
	public function apply( array $values ) : Iface
44
	{
45
		foreach( $values as $key => $value ) {
46
			$this->user->setAndSaveSessionData( $key, $value );
47
		}
48
49
		return $this;
50
	}
51
52
53
	/**
54
	 * Remove the given key from the session.
55
	 *
56
	 * @param string $name Key of the requested value in the session
57
	 * @return \Aimeos\Base\Session\Iface Session instance for method chaining
58
	 */
59
	public function del( string $name ) : Iface
60
	{
61
		$this->user->setAndSaveSessionData( $name, null );
62
		return $this;
63
	}
64
65
66
	/**
67
	 * Returns the value of the requested session key.
68
	 *
69
	 * If the returned value wasn't a string, it's decoded from its serialized
70
	 * representation.
71
	 *
72
	 * @param string $name Key of the requested value in the session
73
	 * @param mixed $default Value returned if requested key isn't found
74
	 * @return mixed Value associated to the requested key
75
	 */
76
	public function get( string $name, $default = null )
77
	{
78
		if( ( $value = $this->user->getSessionData( $name ) ) !== null ) {
79
			return $value;
80
		}
81
82
		return $default;
83
	}
84
85
86
	/**
87
	 * Remove the list of keys from the session.
88
	 *
89
	 * @param array $names Keys to remove from the session
90
	 * @return \Aimeos\Base\Session\Iface Session instance for method chaining
91
	 */
92
	public function remove( array $names ) : Iface
93
	{
94
		foreach( $names as $name ) {
95
			$this->user->setAndSaveSessionData( $name, null );
96
		}
97
98
		return $this;
99
	}
100
101
102
	/**
103
	 * Sets the value for the specified key.
104
	 *
105
	 * If the value isn't a string, it's encoded into a serialized representation
106
	 * and decoded again when using the get() method.
107
	 *
108
	 * @param string $name Key to the value which should be stored in the session
109
	 * @param mixed $value Value that should be associated with the given key
110
	 * @return \Aimeos\Base\Session\Iface Session instance for method chaining
111
	 */
112
	public function set( string $name, $value ) : Iface
113
	{
114
		$this->user->setAndSaveSessionData( $name, $value );
115
		return $this;
116
	}
117
}
118