Passed
Pull Request — master (#190)
by Arman
02:53
created

SessionTrait::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Session\Traits;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Session\Exceptions\SessionException;
19
20
/**
21
 * Traits SessionTrait
22
 * @package Quantum\Libraries\Session
23
 */
24
trait SessionTrait
25
{
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function all(): array
31
    {
32
        $allSessions = [];
33
34
        foreach (self::$storage as $key => $value) {
35
            $allSessions[$key] = is_string($value) ? crypto_decode($value) : $value;
36
        }
37
38
        return $allSessions;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function has(string $key): bool
45
    {
46
        return isset(self::$storage[$key]) && !empty(self::$storage[$key]);
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function get(string $key)
53
    {
54
        return $this->has($key) ? crypto_decode(self::$storage[$key]) : null;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function set(string $key, $value)
61
    {
62
        self::$storage[$key] = crypto_encode($value);
0 ignored issues
show
Bug Best Practice introduced by
The property storage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function getFlash(string $key)
69
    {
70
        $flashData = null;
71
72
        if ($this->has($key)) {
73
            $flashData = $this->get($key);
74
            $this->delete($key);
75
        }
76
77
        return $flashData;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function setFlash(string $key, $value)
84
    {
85
        $this->set($key, $value);
86
        return $this;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function delete(string $key)
93
    {
94
        if ($this->has($key)) {
95
            unset(self::$storage[$key]);
96
        }
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function flush()
103
    {
104
        self::$storage = [];
0 ignored issues
show
Bug Best Practice introduced by
The property storage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
        session_destroy();
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function getId(): ?string
112
    {
113
        return session_id() ?? null;
114
    }
115
116
    /**
117
     * @inheritDoc
118
     * @throws DatabaseException
119
     * @throws SessionException
120
     */
121
    public function regenerateId(): bool
122
    {
123
        if (session_status() !== PHP_SESSION_ACTIVE) {
124
            return false;
125
        }
126
127
        $result = session_regenerate_id(true);
128
129
        if ($result) {
130
            session_write_close();
131
        }
132
133
        $this->initializeSession(self::$params);
0 ignored issues
show
Bug introduced by
It seems like initializeSession() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        $this->/** @scrutinizer ignore-call */ 
134
               initializeSession(self::$params);
Loading history...
134
135
        return $result;
136
    }
137
}