Completed
Push — master ( 8cfd20...08abed )
by Andreas
17:15
created

midcom_services_session   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 88
ccs 9
cts 15
cp 0.6
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A get() 0 3 1
A exists() 0 3 1
A set() 0 3 1
A get_session_data() 0 3 1
A __construct() 0 5 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
/**
10
 * Provides sessioning for components.
11
 *
12
 * Use get, set and exists to access the sessioning data within your component.
13
 *
14
 * <b>Example:</b>
15
 *
16
 * <code>
17
 * <?php
18
 * $session = new midcom_services_session();
19
 * if ($session->exists("mykey")) {
20
 *     echo "found session value: " . $session->get ("mykey") . ", removing it.";
21
 *     $value = $session->remove("mykey");
22
 * } else {
23
 *     echo "setting session value...";
24
 *     $session->set("mykey", "hello world");
25
 * }
26
 * </code>
27
 *
28
 * You should keep in mind, that the isolation level by default is per-component,
29
 * not per-request. If you, for example, have the same component active twice
30
 * (through dynamic_load) you have to manually ensure, that each request is
31
 * treated separately. Unfortunately MidCOM cannot help you here.
32
 *
33
 * <b>Implementation Notes:</b>
34
 *
35
 * This is a simple wrapper that provides access to the sessioning singleton.
36
 * It has the same public member functions as midcom_services__sessioning, refer
37
 * to this class for a detailed documentation.
38
 *
39
 * Basically this wrapper ensures the singleton pattern is maintained and provides
40
 * you with an easy way of lock the domain you're working in.
41
 *
42
 * @package midcom.services
43
 * @see midcom_services__sessioning
44
 */
45
class midcom_services_session
46
{
47
    /**
48
     * Sessioning singleton.
49
     *
50
     * @var midcom_services__sessioning
51
     */
52
    private $_sessioning;
53
54
    /**
55
     * The domain we're working in.
56
     *
57
     * @var string
58
     */
59
    private $_domain;
60
61
    /**
62
     * Constructs a session object.
63
     *
64
     * The default constructor will create a sessioning object within the domain
65
     * of the current context's component. This will be sufficient for almost all
66
     * actual uses of the sessions.
67
     *
68
     * If passed a string argument, this value is used as a domain. This
69
     * is useful for components that need sessioning while under dynamic_load
70
     * conditions or while used as a library.
71
     */
72 86
    public function __construct(string $domain = null)
73
    {
74 86
        $this->_domain = $domain ?? midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
0 ignored issues
show
Documentation Bug introduced by
It seems like $domain ?? midcom_core_c...DCOM_CONTEXT_COMPONENT) can also be of type false. However, the property $_domain is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
76 86
        $this->_sessioning = midcom::get()->session;
77 86
    }
78
79
    /**
80
     * Returns a value from the session.
81
     *
82
     * Returns null if the key
83
     * is non-existent. Note, that this is not necessarily a valid non-existence
84
     * check, as the sessioning system does allow null values. Use the exists function
85
     * if unsure.
86
     */
87
    public function get(string $key)
88
    {
89
        return $this->_sessioning->get($this->_domain . '/' . $key);
90
    }
91
92
    /**
93
     * This will store the value to the specified key.
94
     *
95
     * Note, that a _copy_ is stored,
96
     * the actual object is not referenced in the session data. You will have to update
97
     * it manually in case of changes.
98
     */
99 62
    public function set(string $key, $value)
100
    {
101 62
        $this->_sessioning->set($this->_domain . '/' . $key, $value);
102 62
    }
103
104
    /**
105
     * Checks, if the specified key has been added to the session store.
106
     *
107
     * This is often used in conjunction with get to verify a keys existence.
108
     */
109 31
    public function exists(string $key) : bool
110
    {
111 31
        return $this->_sessioning->has($this->_domain . '/' . $key);
112
    }
113
114
    /**
115
     * Removes the value associated with the specified key. Returns null if the key
116
     * is non-existent or the value of the key just removed otherwise. Note, that
117
     * this is not necessarily a valid non-existence check, as the sessioning
118
     * system does allow null values. Use the exists function if unsure.
119
     */
120
    public function remove(string $key)
121
    {
122
        return $this->_sessioning->remove($this->_domain . '/' . $key);
123
    }
124
125
    /**
126
     * Get all the session data
127
     *
128
     * @return Array containing session data
129
     */
130
    public function get_session_data()
131
    {
132
        return $this->_sessioning->get($this->_domain, false);
133
    }
134
}
135