Completed
Pull Request — master (#135)
by Zhukov
02:10
created

Session::getCookieParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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