Passed
Push — master ( 19e3b7...6548a2 )
by Andreas
23:32
created

midcom_services__sessioning   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 36
ccs 10
cts 16
cp 0.625
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 4 1
A get() 0 6 2
A prepare_storage() 0 6 4
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\AttributeBag;
11
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Base singleton class of the MidCOM sessioning service.
17
 *
18
 * This is a singleton class, that is accessible through the MidCOM Service
19
 * infrastructure. It manages session data of MidCOM driven applications.
20
 *
21
 * This class provides a generic interface to store keyed session values in the
22
 * domain of the corresponding component.
23
 *
24
 * All requests involving this service will always be flagged as no_cache.
25
 *
26
 * If you store class instances within a session, which is perfectly safe in
27
 * general, there are known problems due to the fact, that a class declaration
28
 * has to be available before it can be deserialized. As PHP sessioning does this
29
 * deserialization automatically, this might fail with MidCOM, where the sequence
30
 * in which the code gets loaded and the sessioning gets started up is actually
31
 * undefined. To get around this problems, the sessioning system stores not the
32
 * actual data in the sessioning array, but a serialized string of the data, which
33
 * can always be deserialized on PHP sessioning startup (its a string after all).
34
 * This has an important implication though: The sessioning system always stores
35
 * copies of the data, not references. So if you put something in to the session
36
 * store and modify it afterwards, this change will not be reflected in the
37
 * sessioning store.
38
 *
39
 * <b>Important:</b>
40
 *
41
 * Do <b>never</b> create an instance of this class directly. This is handled
42
 * by the framework. Instead use midcom_service_session which ensures the
43
 * singleton pattern.
44
 *
45
 * Do <b>never</b> work directly with the $_SESSION["midcom_session_data"]
46
 * variable, this is a 100% must-not, as this will break functionality.
47
 *
48
 * @package midcom.services
49
 * @see midcom_services_session
50
 */
51
class midcom_services__sessioning extends Session
52
{
53 1
    public function __construct(RequestStack $stack, bool $cookie_secure)
54
    {
55 1
        $storage = $this->prepare_storage($stack->getCurrentRequest(), $cookie_secure);
56
57 1
        parent::__construct($storage, new AttributeBag('midcom_session_data'));
58
    }
59
60
    protected function prepare_storage(?Request $request, bool $cookie_secure)
61
    {
62
        return new NativeSessionStorage([
63
            'cookie_path' => midcom_connection::get_url('prefix') ?: '/',
64
            'cookie_secure' => $cookie_secure && $request && $request->isSecure(),
65
            'cookie_httponly' => true
66
        ]);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 94
    public function get(string $key, $default = null)
73
    {
74 94
        if ($this->has($key)) {
75 67
            midcom::get()->cache->content->no_cache();
76
        }
77 94
        return parent::get($key, $default);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 68
    public function set(string $key, $value)
84
    {
85 68
        midcom::get()->cache->content->no_cache();
86 68
        parent::set($key, $value);
87
    }
88
}
89