Completed
Branch 2.x (b1c655)
by Julián
07:53
created

Configuration::getDefaultSessionSettings()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 0
1
<?php
2
3
/*
4
 * sessionware (https://github.com/juliangut/sessionware).
5
 * PSR7 session management middleware.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/sessionware
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Jgut\Middleware\Sessionware;
13
14
/**
15
 * Session configuration.
16
 */
17
class Configuration
18
{
19
    use SessionIniSettingsTrait;
20
21
    const LIFETIME_FLASH    = 300; // 5 minutes
22
    const LIFETIME_SHORT    = 600; // 10 minutes
23
    const LIFETIME_NORMAL   = 900; // 15 minutes
24
    const LIFETIME_DEFAULT  = 1440; // 24 minutes
25
    const LIFETIME_EXTENDED = 3600; // 1 hour
26
    const LIFETIME_INFINITE = PHP_INT_MAX; // Around 1145 years (x86_64)
27
28
    const TIMEOUT_KEY_DEFAULT = '__SESSIONWARE_TIMEOUT__';
29
30
    const SESSION_NAME_DEFAULT = 'PHPSESSID';
31
32
    const SESSION_ID_LENGTH = 80;
33
34
    /**
35
     * @var string
36
     */
37
    protected $name;
38
39
    /**
40
     * @var string
41
     */
42
    protected $savePath;
43
44
    /**
45
     * @var int
46
     */
47
    protected $lifetime;
48
49
    /**
50
     * @var string
51
     */
52
    protected $timeoutKey;
53
54
    /**
55
     * @var string
56
     */
57
    protected $cookiePath;
58
59
    /**
60
     * @var string
61
     */
62
    protected $cookieDomain;
63
64
    /**
65
     * @var bool
66
     */
67
    protected $cookieSecure;
68
69
    /**
70
     * @var bool
71
     */
72
    protected $cookieHttpOnly;
73
74
    /**
75
     * Configuration constructor.
76
     *
77
     * @param array $configurations
78
     */
79
    public function __construct(array $configurations = [])
80
    {
81
        $configurations = array_merge(
82
            $this->getDefaultSessionSettings(),
83
            $configurations
84
        );
85
86
        $this->seedConfigurations($configurations);
87
    }
88
89
    /**
90
     * Retrieve default session settings.
91
     *
92
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|integer|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
93
     */
94
    protected function getDefaultSessionSettings()
95
    {
96
        $lifeTime = $this->getIntegerIniSetting('cookie_lifetime') === 0
97
            ? $this->getIntegerIniSetting('gc_maxlifetime')
98
            : min($this->getIntegerIniSetting('cookie_lifetime'), $this->getIntegerIniSetting('gc_maxlifetime'));
99
        $sessionName = session_name() !== static::SESSION_NAME_DEFAULT ? session_name() : static::SESSION_NAME_DEFAULT;
100
101
        return [
102
            'name'           => $this->getStringIniSetting('name', $sessionName),
103
            'savePath'       => $this->getStringIniSetting('save_path', sys_get_temp_dir()),
104
            'lifetime'       => $lifeTime > 0 ? $lifeTime : static::LIFETIME_DEFAULT,
105
            'timeoutKey'     => static::TIMEOUT_KEY_DEFAULT,
106
            'cookiePath'     => $this->getStringIniSetting('cookie_path', '/'),
107
            'cookieDomain'   => $this->getStringIniSetting('cookie_domain'),
108
            'cookieSecure'   => $this->hasBoolIniSetting('cookie_secure'),
109
            'cookieHttpOnly' => $this->hasBoolIniSetting('cookie_httponly'),
110
        ];
111
    }
112
113
    /**
114
     * Seed configurations.
115
     *
116
     * @param array $configurations
117
     */
118
    protected function seedConfigurations(array $configurations)
119
    {
120
        $configs = [
121
            'name',
122
            'cookiePath',
123
            'cookieDomain',
124
            'cookieSecure',
125
            'cookieHttpOnly',
126
            'savePath',
127
            'lifetime',
128
            'timeoutKey',
129
        ];
130
131
        foreach ($configs as $config) {
132
            if (isset($configurations[$config])) {
133
                $callback = [$this, 'set' . ucfirst($config)];
134
135
                call_user_func($callback, $configurations[$config]);
136
            }
137
        }
138
    }
139
140
    /**
141
     * Get session name.
142
     *
143
     * @return string
144
     */
145
    public function getName()
146
    {
147
        return $this->name;
148
    }
149
150
    /**
151
     * Set session name.
152
     *
153
     * @param string $name
154
     *
155
     * @throws \InvalidArgumentException
156
     *
157
     * @return static
158
     */
159
    public function setName($name)
160
    {
161
        if (trim($name) === '') {
162
            throw new \InvalidArgumentException('Session name must be a non empty string');
163
        }
164
165
        $this->name = $name;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Get session cookie path.
172
     *
173
     * @return string
174
     */
175
    public function getCookiePath()
176
    {
177
        return $this->cookiePath;
178
    }
179
180
    /**
181
     * Set session cookie path.
182
     *
183
     * @param string $cookiePath
184
     *
185
     * @return static
186
     */
187
    public function setCookiePath($cookiePath)
188
    {
189
        $this->cookiePath = $cookiePath;
190
191
        return $this;
192
    }
193
194
    /**
195
     * Get session cookie domain.
196
     *
197
     * @return string
198
     */
199
    public function getCookieDomain()
200
    {
201
        return $this->cookieDomain;
202
    }
203
204
    /**
205
     * Set session cookie domain.
206
     *
207
     * @param string $cookieDomain
208
     *
209
     * @return static
210
     */
211
    public function setCookieDomain($cookieDomain)
212
    {
213
        $this->cookieDomain = $cookieDomain;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Is session cookie HTTPS only.
220
     *
221
     * @return bool
222
     */
223
    public function isCookieSecure()
224
    {
225
        return $this->cookieSecure;
226
    }
227
228
    /**
229
     * Set session cookie HTTPS only.
230
     *
231
     * @param bool $cookieSecure
232
     *
233
     * @return static
234
     */
235
    public function setCookieSecure($cookieSecure)
236
    {
237
        $this->cookieSecure = $cookieSecure === true;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Is session cookie HTTP only.
244
     *
245
     * @return bool
246
     */
247
    public function isCookieHttpOnly()
248
    {
249
        return $this->cookieHttpOnly;
250
    }
251
252
    /**
253
     * Set session cookie HTTP only.
254
     *
255
     * @param bool $cookieHttpOnly
256
     *
257
     * @return static
258
     */
259
    public function setCookieHttpOnly($cookieHttpOnly)
260
    {
261
        $this->cookieHttpOnly = $cookieHttpOnly;
262
263
        return $this;
264
    }
265
266
    /**
267
     * Get session save path.
268
     *
269
     * @return string
270
     */
271
    public function getSavePath()
272
    {
273
        return $this->savePath;
274
    }
275
276
    /**
277
     * Set session save path.
278
     *
279
     * @param string $savePath
280
     *
281
     * @throws \InvalidArgumentException
282
     *
283
     * @return static
284
     */
285
    public function setSavePath($savePath)
286
    {
287
        if (trim($savePath) === '') {
288
            throw new \InvalidArgumentException('Session save path must be a non empty string');
289
        }
290
291
        $this->savePath = trim($savePath);
292
293
        return $this;
294
    }
295
296
    /**
297
     * Get session lifetime.
298
     *
299
     * @return int
300
     */
301
    public function getLifetime()
302
    {
303
        return $this->lifetime;
304
    }
305
306
    /**
307
     * Set session lifetime.
308
     *
309
     * @param int $lifetime
310
     *
311
     * @throws \InvalidArgumentException
312
     *
313
     * @return static
314
     */
315
    public function setLifetime($lifetime)
316
    {
317
        if ((int) $lifetime < 1) {
318
            throw new \InvalidArgumentException('Session lifetime must be a positive integer');
319
        }
320
321
        $this->lifetime = (int) $lifetime;
322
323
        return $this;
324
    }
325
326
    /**
327
     * Get session timeout control key.
328
     *
329
     * @return string
330
     */
331
    public function getTimeoutKey()
332
    {
333
        return $this->timeoutKey;
334
    }
335
336
    /**
337
     * Set session timeout control key.
338
     *
339
     * @param string $timeoutKey
340
     *
341
     * @throws \InvalidArgumentException
342
     *
343
     * @return static
344
     */
345
    public function setTimeoutKey($timeoutKey)
346
    {
347
        if (trim($timeoutKey) === '') {
348
            throw new \InvalidArgumentException('Session timeout key must be a non empty string');
349
        }
350
351
        $this->timeoutKey = $timeoutKey;
352
353
        return $this;
354
    }
355
}
356