Completed
Pull Request — master (#164)
by Alexander
02:35 queued 12s
created

Session   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 87.88%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 65
c 2
b 0
f 1
dl 0
loc 157
rs 9.92
wmc 31
ccs 58
cts 66
cp 0.8788

18 Methods

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