Failed Conditions
Push — master ( 5591cb...4c31fe )
by Florian
02:12
created

Config   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Test Coverage

Coverage 55.71%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 230
ccs 39
cts 70
cp 0.5571
rs 10
c 0
b 0
f 0
wmc 27

22 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 fromArray() 0 15 3
A getCookiePath() 0 3 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
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Http\Session;
18
19
use RuntimeException;
20
21
/**
22
 * Session Configuration Abstraction
23
 *
24
 * This class provides a way to configure most of the session settings
25
 *
26
 * @link https://www.php.net/manual/en/session.security.ini.php
27
 */
28
class Config implements ConfigInterface
29
{
30
    /**
31
     * @param array $config
32
     * @return \Phauthentic\Infrastructure\Http\Session\Config
33
     */
34 1
    public static function fromArray(array $config): Config
35
    {
36 1
        $that = new self();
37
38 1
        foreach ($config as $key => $value) {
39 1
            $method = 'set' . $key;
40 1
            if (method_exists($that, $method)) {
41 1
                $that->$method($value);
42 1
                continue;
43
            }
44
45
            ini_set('session.' . $key, $value);
46
        }
47
48 1
        return $that;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 3
    public function setUseTransSid(bool $useTransSid): ConfigInterface
55
    {
56 3
        $this->iniSet('session.use_trans_sid', $useTransSid);
57
58 3
        return $this;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function setSerializeHandler(string $handler): ConfigInterface
65
    {
66
        $this->iniSet('session.serialize_handler', $handler);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function setStrictMode(bool $useStrictMode): ConfigInterface
75
    {
76
        $this->iniSet('session.use_strict_mode', $useStrictMode);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 2
    public function setCookiePath(string $path): ConfigInterface
85
    {
86 2
        $this->iniSet('session.cookie_path', $path);
87
88 2
        return $this;
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94 2
    public function getCookiePath(): string
95
    {
96 2
        return (string)ini_get('session.cookie_path');
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 2
    public function setCookieHttpOnly(bool $onlyHttp): ConfigInterface
103
    {
104 2
        $this->iniSet('session.cookie_httponly', $onlyHttp);
105
106 2
        return $this;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 2
    public function getCookieHttpOnly(): bool
113
    {
114 2
        return (bool)ini_get('session.cookie_httponly');
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function setCookieSecure(bool $secure): ConfigInterface
121
    {
122
        $this->iniSet('session.cookie_secure', $secure);
123
124
        return $this;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function setSessionName(string $name): ConfigInterface
131
    {
132
        $this->iniSet('session.name', $name);
133
134
        return $this;
135
    }
136
137
    /**
138
     * @inheritDoc
139
     */
140
    public function setUseCookies(bool $useCookies): ConfigInterface
141
    {
142
        $this->iniSet('session.use_cookies', $useCookies);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150 2
    public function setSavePath(string $path): ConfigInterface
151
    {
152 2
        $this->iniSet('session.save_path', $path);
153
154 2
        return $this;
155
    }
156
157
    /**
158
     * Checks an edge case for the handler
159
     *
160
     * @link https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d
161
     * @link http://php.net/manual/en/session.configuration.php
162
     * @param string $handler Handler
163
     * @return void
164
     */
165
    protected function checkHandler(string $handler): void
166
    {
167
        if (
168
            PHP_VERSION_ID >= 70200
169
            && $handler === 'user'
170
        ) {
171
            throw new RuntimeException(
172
                'You can\'t set the `user` save handler any longer in php >= 7.2. '
173
                . 'See https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d'
174
            );
175
        }
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function setSaveHandler(string $handler): ConfigInterface
182
    {
183
        $this->checkHandler($handler);
184
        $this->iniSet('session.save_handler', $handler);
185
186
        return $this;
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192 2
    public function getUseTransSid(): bool
193
    {
194 2
        return (bool)ini_get('session.use_trans_sid');
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200 2
    public function getSerializeHandler(): string
201
    {
202 2
        return ini_get('session.serialize_handler');
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    public function getUseCookies(): string
209
    {
210
        return ini_get('session.use_cookies');
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216 2
    public function getSavePath(): string
217
    {
218 2
        return ini_get('session.save_path');
219
    }
220
221
    /**
222
     * @inheritDoc
223
     */
224
    public function getSaveHandler(): string
225
    {
226
        return ini_get('session.save_handler');
227
    }
228
229
    /**
230
     * @inheritDoc
231
     */
232 2
    public function setGcLifeTime(int $minutes): ConfigInterface
233
    {
234 2
        $this->iniSet('session.gc_maxlifetime', 60 * $minutes);
235
236 2
        return $this;
237
    }
238
239
    /**
240
     * @inheritDoc
241
     */
242 2
    public function getGcLifeTime(): int
243
    {
244 2
        return (int)ini_get('session.gc_maxlifetime') / 60;
245
    }
246
247
    /**
248
     * @inheritDoc
249
     */
250 3
    public function iniSet(string $setting, $value)
251
    {
252 3
        $result = ini_set($setting, (string)$value);
253 3
        if ($result === false) {
254
            throw new RuntimeException(sprintf('Could not set `%s`', $setting));
255
        }
256
257 3
        return $result;
258
    }
259
}
260