Test Failed
Push — master ( f574df...435265 )
by Florian
04:27 queued 35s
created

Config::getUseCookies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @param array $array Array
20
     * @return $this
21
     */
22
    public static function fromArray(array $array): ConfigInterface
23
    {
24
        $instance = new static();
25
26
        foreach ($array as $key => $value) {
27
            $method = 'set' . $key;
28
            if (method_exists($instance, $method)) {
29
                $instance->{$method}($value);
30
            }
31
        }
32
33
        return $instance;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function setUseTransSid(bool $useTransSid): ConfigInterface
40
    {
41
        $this->iniSet('session.use_trans_sid', $useTransSid);
42
43
        return $this;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function setSerializeHandler(string $handler): ConfigInterface
50
    {
51
        $this->iniSet('session.serialize_handler', $handler);
52
53
        return $this;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function setStrictMode(bool $useStrictMode): ConfigInterface
60
    {
61
        $this->iniSet('session.use_strict_mode', $useStrictMode);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function setCookiePath(bool $path): ConfigInterface
70
    {
71
        $this->iniSet('session.cookie_path', $path);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function setCookieHttpOnly(bool $onlyHttp): ConfigInterface
80
    {
81
        $this->iniSet('session.cookie_httponly', $onlyHttp);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function cookieHttpOnly(): bool
90
    {
91
        return (bool)ini_get('session.cookie_httponly');
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function setCookieSecure(bool $secure): ConfigInterface
98
    {
99
        $this->iniSet('session.cookie_secure', $secure);
100
101
        return $this;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function setSessionName(bool $name): ConfigInterface
108
    {
109
        $this->iniSet('session.name', $name);
110
111
        return $this;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function setUseCookies(bool $useCookies): ConfigInterface
118
    {
119
        $this->iniSet('session.use_cookies', $useCookies);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function setSavePath(string $path): ConfigInterface
128
    {
129
        $this->iniSet('session.save_path', $path);
130
131
        return $this;
132
    }
133
134
    /**
135
     * Checks an edge case for the handler
136
     *
137
     * @link https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d
138
     * @link http://php.net/manual/en/session.configuration.php
139
     * @param string $handler Handler
140
     * @return void
141
     */
142
    protected function checkHandler(string $handler): void
143
    {
144
        if (
145
            version_compare(PHP_VERSION, '7.2.0', '>=')
146
            && $handler === 'user'
147
        ) {
148
            throw new RuntimeException(
149
                'You can\'t set the `user` save handler any longer in php >= 7.2. '
150
                . 'See https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d'
151
            );
152
        }
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function setSaveHandler(string $handler): ConfigInterface
159
    {
160
        $this->checkHandler($handler);
161
        $this->iniSet('session.save_handler', $handler);
162
163
        return $this;
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function useTransSid(): bool
170
    {
171
        return (bool)ini_get('session.use_trans_sid');
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function serializeHandler(): string
178
    {
179
        return ini_get('session.serialize_handler');
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function useCookies(): string
186
    {
187
        return ini_get('session.use_cookies');
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function savePath(): string
194
    {
195
        return ini_get('session.save_path');
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function saveHandler(): string
202
    {
203
        return ini_get('session.save_handler');
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function setGcLifeTime(int $minutes): ConfigInterface
210
    {
211
        $this->iniSet('session.gc_maxlifetime', 60 * $minutes);
212
213
        return $this;
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219
    public function gcLifeTime(): int
220
    {
221
        return (int)ini_get('session.gc_maxlifetime') / 60;
222
    }
223
224
    /**
225
     * @inheritDoc
226
     */
227
    public function iniSet(string $setting, $value)
228
    {
229
        $result = ini_set($setting, (string)$value);
230
        if ($result === false) {
231
            throw new RuntimeException(sprintf('Could not set `%s`', $setting));
232
        }
233
234
        return $result;
235
    }
236
}
237