Completed
Push — master ( 5581e0...d01905 )
by Christoph
13:08
created

CookieHelper::setCookie()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 96
nop 8
dl 0
loc 42
rs 8.0035
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Http;
26
27
class CookieHelper {
28
29
	const SAMESITE_NONE = 0;
30
	const SAMESITE_LAX = 1;
31
	const SAMESITE_STRICT = 2;
32
33
	public static function setCookie(string $name,
34
									 string $value = '',
35
									 int $maxAge = 0,
36
									 string $path = '',
37
									 string $domain = '',
38
									 bool $secure = false,
39
									 bool $httponly = false,
40
									 int $samesite = self::SAMESITE_NONE) {
41
		$header = sprintf(
42
			'Set-Cookie: %s=%s',
43
			$name,
44
			urlencode($value)
45
		);
46
47
		if ($path !== '') {
48
			$header .= sprintf('; Path=%s', $path);
49
		}
50
51
		if ($domain !== '') {
52
			$header .= sprintf('; Domain=%s', $domain);
53
		}
54
55
		if ($maxAge > 0) {
56
			$header .= sprintf('; Max-Age=%d', $maxAge);
57
		}
58
59
		if ($secure) {
60
			$header .= '; Secure';
61
		}
62
63
		if ($httponly) {
64
			$header .= '; HttpOnly';
65
		}
66
67
		if ($samesite === self::SAMESITE_LAX) {
68
			$header .= '; SameSite=Lax';
69
		} else if ($samesite === self::SAMESITE_STRICT) {
70
			$header .= '; SameSite=Strict';
71
		}
72
73
		header($header, false);
74
	}
75
}
76