Test Failed
Branch master (254a18)
by Florian
02:37
created

Config::getCookieHttpOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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