Passed
Pull Request — master (#100)
by Arman
05:16 queued 02:39
created

Session::setFlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
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.8.0
13
 */
14
15
namespace Quantum\Libraries\Session;
16
17
use Quantum\Exceptions\CryptorException;
18
19
/**
20
 * Class Session
21
 * @package Quantum\Libraries\Session
22
 */
23
class Session implements SessionStorageInterface
24
{
25
26
    /**
27
     * Session storage
28
     * @var array $storage
29
     */
30
    private static $storage = [];
31
32
    /**
33
     * Session instance
34
     * @var Session|null
35
     */
36
    private static $instance = null;
37
38
    /**
39
     * Session constructor.
40
     */
41
    private function __construct()
42
    {
43
        // Preventing to create new object through constructor
44
    }
45
46
    /**
47
     * Gets the session instance
48
     * @param array $storage
49
     * @return Session|null
50
     */
51
    public static function getInstance(array &$storage): ?Session
52
    {
53
        self::$storage = &$storage;
54
55
        if (self::$instance === null) {
56
            self::$instance = new self();
57
        }
58
59
        return self::$instance;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     * @throws CryptorException
65
     */
66
    public function all(): array
67
    {
68
        $allSessions = [];
69
70
        foreach (self::$storage as $key => $value) {
71
            $allSessions[$key] = crypto_decode($value);
72
        }
73
74
        return $allSessions;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function has(string $key): bool
81
    {
82
        return isset(self::$storage[$key]) && !empty(self::$storage[$key]);
83
    }
84
85
    /**
86
     * @inheritDoc
87
     * @throws CryptorException
88
     */
89
    public function get(string $key)
90
    {
91
        return $this->has($key) ? crypto_decode(self::$storage[$key]) : null;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     * @throws CryptorException
97
     */
98
    public function set(string $key, $value)
99
    {
100
        self::$storage[$key] = crypto_encode($value);
101
    }
102
103
    /**
104
     * @inheritDoc
105
     * @throws CryptorException
106
     */
107
    public function getFlash(string $key)
108
    {
109
        $flashData = null;
110
111
        if ($this->has($key)) {
112
            $flashData = $this->get($key);
113
            $this->delete($key);
114
        }
115
116
        return $flashData;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     * @throws CryptorException
122
     */
123
    public function setFlash(string $key, $value)
124
    {
125
        $this->set($key, $value);
126
        return $this;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function delete(string $key)
133
    {
134
        if ($this->has($key)) {
135
            unset(self::$storage[$key]);
136
        }
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function flush()
143
    {
144
        self::$storage = [];
145
        session_destroy();
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function getId(): ?string
152
    {
153
        return session_id() ?? null;
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function regenerateId(): bool
160
    {
161
        return session_regenerate_id(true);
162
    }
163
}
164