Test Failed
Push — master ( 13c8bf...f549ef )
by Florian
11:18
created

Config   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A iniSet() 0 8 2
A getSerializeHandler() 0 3 1
A getSavePath() 0 3 1
A setStrictMode() 0 5 1
A getGcLifeTime() 0 3 1
A getUseCookies() 0 3 1
A setSessionName() 0 5 1
A getSaveHandler() 0 3 1
A setSerializeHandler() 0 5 1
A setCookieSecure() 0 5 1
A setSavePath() 0 5 1
A getCookieHttpOnly() 0 3 1
A setUseTransSid() 0 5 1
A getUseTransSid() 0 3 1
A setCookiePath() 0 5 1
A setUseCookies() 0 5 1
A setCookieHttpOnly() 0 5 1
A setGcLifeTime() 0 5 1
A checkHandler() 0 9 3
A setSaveHandler() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phauthentic\Infrastructure\Http\Session;
6
7
use RuntimeException;
8
9
/**
10
 * Session Configuration Abstraction
11
 *
12
 * This class provides a way to configure most of the session settings
13
 *
14
 * @link https://www.php.net/manual/en/session.security.ini.php
15
 */
16
class Config implements ConfigInterface
17
{
18
    /**
19
     * @inheritDoc
20
     */
21
    public function setUseTransSid(bool $useTransSid): ConfigInterface
22
    {
23
        $this->iniSet('session.use_trans_sid', $useTransSid);
24
25
        return $this;
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function setSerializeHandler(string $handler): ConfigInterface
32
    {
33
        $this->iniSet('session.serialize_handler', $handler);
34
35
        return $this;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function setStrictMode(bool $useStrictMode): ConfigInterface
42
    {
43
        $this->iniSet('session.use_strict_mode', $useStrictMode);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function setCookiePath(bool $path): ConfigInterface
52
    {
53
        $this->iniSet('session.cookie_path', $path);
54
55
        return $this;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function setCookieHttpOnly(bool $onlyHttp): ConfigInterface
62
    {
63
        $this->iniSet('session.cookie_httponly', $onlyHttp);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function getCookieHttpOnly(): bool
72
    {
73
        return (bool)ini_get('session.cookie_httponly');
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function setCookieSecure(bool $secure): ConfigInterface
80
    {
81
        $this->iniSet('session.cookie_secure', $secure);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function setSessionName(bool $name): ConfigInterface
90
    {
91
        $this->iniSet('session.name', $name);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function setUseCookies(bool $useCookies): ConfigInterface
100
    {
101
        $this->iniSet('session.use_cookies', $useCookies);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function setSavePath(string $path): ConfigInterface
110
    {
111
        $this->iniSet('session.save_path', $path);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Checks an edge case for the handler
118
     *
119
     * @link https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d
120
     * @link http://php.net/manual/en/session.configuration.php
121
     * @param string $handler Handler
122
     * @return void
123
     */
124
    protected function checkHandler(string $handler): void
125
    {
126
        if (
127
            version_compare(PHP_VERSION, '7.2.0', '>=')
128
            && $handler === 'user'
129
        ) {
130
            throw new RuntimeException(
131
                'You can\'t set the `user` save handler any longer in php >= 7.2. '
132
                . 'See https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d'
133
            );
134
        }
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function setSaveHandler(string $handler): ConfigInterface
141
    {
142
        $this->checkHandler($handler);
143
        $this->iniSet('session.save_handler', $handler);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function getUseTransSid(): bool
152
    {
153
        return (bool)ini_get('session.use_trans_sid');
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function getSerializeHandler(): string
160
    {
161
        return ini_get('session.serialize_handler');
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function getUseCookies(): string
168
    {
169
        return ini_get('session.use_cookies');
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function getSavePath(): string
176
    {
177
        return ini_get('session.save_path');
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function getSaveHandler(): string
184
    {
185
        return ini_get('session.save_handler');
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function setGcLifeTime(int $minutes): ConfigInterface
192
    {
193
        $this->iniSet('session.gc_maxlifetime', 60 * $minutes);
194
195
        return $this;
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function getGcLifeTime(): int
202
    {
203
        return (int)ini_get('session.gc_maxlifetime') / 60;
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function iniSet(string $setting, $value)
210
    {
211
        $result = ini_set($setting, (string)$value);
212
        if ($result === false) {
213
            throw new RuntimeException(sprintf('Could not set `%s`', $setting));
214
        }
215
216
        return $result;
217
    }
218
}
219