Completed
Push — master ( 8849ee...a1b70f )
by Andreas
24:35
created

midcom_services__sessioning::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
11
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
12
13
/**
14
 * Base singleton class of the MidCOM sessioning service.
15
 *
16
 * This is a singleton class, that is accessible through the MidCOM Service
17
 * infrastructure. It manages session data of MidCOM driven applications.
18
 *
19
 * This class provides a generic interface to store keyed session values in the
20
 * domain of the corresponding component.
21
 *
22
 * All requests involving this service will always be flagged as no_cache.
23
 *
24
 * If you store class instances within a session, which is perfectly safe in
25
 * general, there are known problems due to the fact, that a class declaration
26
 * has to be available before it can be deserialized. As PHP sessioning does this
27
 * deserialization automatically, this might fail with MidCOM, where the sequence
28
 * in which the code gets loaded and the sessioning gets started up is actually
29
 * undefined. To get around this problems, the sessioning system stores not the
30
 * actual data in the sessioning array, but a serialized string of the data, which
31
 * can always be deserialized on PHP sessioning startup (its a string after all).
32
 * This has an important implication though: The sessioning system always stores
33
 * copies of the data, not references. So if you put something in to the session
34
 * store and modify it afterwards, this change will not be reflected in the
35
 * sessioning store.
36
 *
37
 * <b>Important:</b>
38
 *
39
 * Do <b>never</b> create an instance of this class directly. This is handled
40
 * by the framework. Instead use midcom_service_session which ensures the
41
 * singleton pattern.
42
 *
43
 * Do <b>never</b> work directly with the $_SESSION["midcom_session_data"]
44
 * variable, this is a 100% must-not, as this will break functionality.
45
 *
46
 * @package midcom.services
47
 * @see midcom_services_session
48
 */
49
class midcom_services__sessioning extends Session
50
{
51 1
    public function __construct()
52
    {
53 1
        parent::__construct($this->prepare_storage(), new NamespacedAttributeBag('midcom_session_data'));
54 1
    }
55
56
    protected function prepare_storage()
57
    {
58
        $cookie_secure = (   !empty($_SERVER['HTTPS'])
59
                          && $_SERVER['HTTPS'] !== 'off'
60
                          && midcom::get()->config->get('auth_backend_simple_cookie_secure'));
61
62
        return new NativeSessionStorage([
63
            'cookie_path' => midcom_connection::get_url('prefix') ?: '/',
64
            'cookie_secure' => $cookie_secure,
65
            'cookie_httponly' => true
66
        ]);
67
    }
68
69
    /**
70
     * Returns a value from the session.
71
     *
72
     * Returns null if the key
73
     * is non-existent. Note, that this is not necessarily a valid non-existence
74
     * check, as the sessioning system does allow null values. Use the has function
75
     * if unsure.
76
     *
77
     * @param string $key        The key to query.
78
     * @param mixed $default
79
     * @return mixed            The session key's data value, or null on failure.
80
     */
81 1
    public function get($key, $default = null)
82
    {
83 1
        if ($this->has($key)) {
84 1
            midcom::get()->cache->content->no_cache();
85
        }
86 1
        return parent::get($key, $default);
87
    }
88
89
    /**
90
     * This will store the value to the specified key.
91
     *
92
     * @param mixed    $key        Session value identifier.
93
     * @param mixed    $value        Session value.
94
     */
95 61
    public function set($key, $value)
96
    {
97 61
        midcom::get()->cache->content->no_cache();
98 61
        parent::set($key, $value);
99 61
    }
100
}
101