Completed
Push — master ( 7e4c42...0479c1 )
by Andreas
14:27
created

midcom_services__sessioning   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 14.29%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 56
ccs 3
cts 21
cp 0.1429
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A get() 0 6 2
A prepare_storage() 0 6 1
A set() 0 4 1
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
    public function __construct()
52
    {
53
        $cookie_path = midcom::get()->config->get('auth_backend_simple_cookie_path');
54
        if ($cookie_path == 'auto') {
55
            $cookie_path = midcom_connection::get_url('self');
56
        }
57
58
        $cookie_secure = (   !empty($_SERVER['HTTPS'])
59
                          && $_SERVER['HTTPS'] !== 'off'
60
                          && midcom::get()->config->get('auth_backend_simple_cookie_secure'));
61
62
        $storage = $this->prepare_storage($cookie_path, $cookie_secure);
63
        parent::__construct($storage, new NamespacedAttributeBag('midcom_session_data'));
64
    }
65
66
    protected function prepare_storage($cookie_path, $cookie_secure)
67
    {
68
        return new NativeSessionStorage([
69
            'cookie_path' => $cookie_path,
70
            'cookie_secure' => $cookie_secure,
71
            'cookie_httponly' => true
72
        ]);
73
    }
74
75
    /**
76
     * Returns a value from the session.
77
     *
78
     * Returns null if the key
79
     * is non-existent. Note, that this is not necessarily a valid non-existence
80
     * check, as the sessioning system does allow null values. Use the has function
81
     * if unsure.
82
     *
83
     * @param string $key        The key to query.
84
     * @param mixed $default
85
     * @return mixed            The session key's data value, or null on failure.
86
     */
87
    public function get($key, $default = null)
88
    {
89
        if ($this->has($key)) {
90
            midcom::get()->cache->content->no_cache();
91
        }
92
        return parent::get($key, $default);
93
    }
94
95
    /**
96
     * This will store the value to the specified key.
97
     *
98
     * @param mixed    $key        Session value identifier.
99
     * @param mixed    $value        Session value.
100
     */
101 61
    public function set($key, $value)
102
    {
103 61
        midcom::get()->cache->content->no_cache();
104 61
        parent::set($key, $value);
105 61
    }
106
}
107