Completed
Push — master ( 7a685f...487635 )
by Alexander
01:57
created

Session::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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