Passed
Push — master ( 07213d...3b1a54 )
by Andreas
11:05
created

midcom_services_session::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
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
/**
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
     * @var midcom_services__sessioning
49
     */
50
    private $_sessioning;
51
52
    /**
53
     * @var string
54
     */
55
    private $_domain;
56
57
    /**
58
     * Constructs a session object.
59
     *
60
     * The default constructor will create a sessioning object within the domain
61
     * of the current context's component. This will be sufficient for almost all
62
     * actual uses of the sessions.
63
     *
64
     * If passed a string argument, this value is used as a domain. This
65
     * is useful for components that need sessioning while under dynamic_load
66
     * conditions or while used as a library.
67
     */
68 94
    public function __construct(string $domain = null)
69
    {
70 94
        $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...
71
72 94
        $this->_sessioning = midcom::get()->session;
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 exists function
81
     * if unsure.
82
     */
83
    public function get(string $key)
84
    {
85
        $data = $this->_sessioning->get($this->_domain, []);
86
        return $data[$key] ?? null;
87
    }
88
89
    /**
90
     * This will store the value to the specified key.
91
     *
92
     * Note, that a _copy_ is stored,
93
     * the actual object is not referenced in the session data. You will have to update
94
     * it manually in case of changes.
95
     */
96 68
    public function set(string $key, $value)
97
    {
98 68
        $data = $this->_sessioning->get($this->_domain, []);
99 68
        $data[$key] = $value;
100 68
        $this->_sessioning->set($this->_domain, $data);
101
    }
102
103
    /**
104
     * Checks, if the specified key has been added to the session store.
105
     *
106
     * This is often used in conjunction with get to verify a keys existence.
107
     */
108 35
    public function exists(string $key) : bool
109
    {
110 35
        $data = $this->_sessioning->get($this->_domain, []);
111 35
        return array_key_exists($key, $data);
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
        $data = $this->_sessioning->get($this->_domain, []);
123
        unset($data[$key]);
124
    }
125
}
126