Completed
Push — master ( e4c888...b002ee )
by Alexander
10:41 queued 05:38
created

Session::regenerateId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Web\Session;
4
5
/**
6
 * Session provides session data management and the related configurations.
7
 */
8
class Session implements SessionInterface
9
{
10
    private const DEFAULT_OPTIONS = [
11
        'use_cookies' => 1,
12
        'cookie_secure' => 1,
13
        'use_only_cookies' => 1,
14
        'cookie_httponly' => 1,
15
        'use_strict_mode' => 1,
16
        'sid_bits_per_character' => 6,
17
        'sid_length' => 48,
18
        'cache_limiter' => 'nocache',
19
    ];
20
21
    private const DEFAULT_OPTIONS_73 = [
22
        'cookie_samesite' => 'Lax',
23
    ];
24
25
    private $options;
26
27
    public function __construct(array $options = [], \SessionHandlerInterface $handler = null)
28
    {
29
        if ($handler !== null) {
30
            session_set_save_handler($handler);
31
        }
32
33
        $defaultOptions = self::DEFAULT_OPTIONS;
34
        if (PHP_VERSION_ID >= 73000) {
35
            $defaultOptions = array_merge($defaultOptions, self::DEFAULT_OPTIONS_73);
36
        }
37
        $this->options = array_merge($defaultOptions, $options);
38
39
        if ($this->isActive()) {
40
            throw new \RuntimeException('Session is already started');
41
        }
42
    }
43
44
    public function get(string $key, $default = null)
45
    {
46
        $this->open();
47
        return $_SESSION[$key] ?? $default;
48
    }
49
50
    public function set(string $key, $value): void
51
    {
52
        $this->open();
53
        $_SESSION[$key] = $value;
54
    }
55
56
    public function close(): void
57
    {
58
        if ($this->isActive()) {
59
            try {
60
                session_write_close();
61
            } catch (\Throwable $e) {
62
                throw new SessionException('Unable to close session', $e->getCode(), $e);
63
            }
64
        }
65
    }
66
67
    public function open(): void
68
    {
69
        if ($this->isActive()) {
70
            return;
71
        }
72
73
        try {
74
            session_start($this->options);
75
        } catch (\Throwable $e) {
76
            throw new SessionException('Failed to start session', $e->getCode(), $e);
77
        }
78
    }
79
80
    public function isActive(): bool
81
    {
82
        return session_status() === PHP_SESSION_ACTIVE;
83
    }
84
85
    public function getId(): ?string
86
    {
87
        $id = session_id();
88
        return $id === '' ? null : $id;
89
    }
90
91
    public function regenerateId(): void
92
    {
93
        if ($this->isActive()) {
94
            try {
95
                session_regenerate_id(true);
96
            } catch (\Throwable $e) {
97
                throw new SessionException('Failed to regenerate ID', $e->getCode(), $e);
98
            }
99
        }
100
    }
101
102
    public function discard(): void
103
    {
104
        if ($this->isActive()) {
105
            session_abort();
106
        }
107
    }
108
109
    public function getName(): string
110
    {
111
        return session_name();
112
    }
113
114
    public function all(): array
115
    {
116
        $this->open();
117
        return $_SESSION;
118
    }
119
120
    public function remove(string $key): void
121
    {
122
        $this->open();
123
        unset($_SESSION[$key]);
124
    }
125
126
    public function has(string $key): bool
127
    {
128
        $this->open();
129
        return isset($_SESSION[$key]);
130
    }
131
132
    public function pull(string $key)
133
    {
134
        $value = $this->get($key);
135
        $this->remove($key);
136
        return $value;
137
    }
138
139
    public function clear(): void
140
    {
141
        $this->open();
142
        $_SESSION = [];
143
    }
144
145
    public function destroy(): void
146
    {
147
        if ($this->isActive()) {
148
            session_destroy();
149
        }
150
    }
151
152
    public function getCookieParameters(): array
153
    {
154
        return session_get_cookie_params();
155
    }
156
}
157