Passed
Push — developer ( db306d...128070 )
by Mariusz
105:17 queued 70:07
created

Session::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Sesion class.
4
 *
5
 * @copyright YetiForce Sp. z o.o.
6
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
7
 * @author    Tomasz Kur <[email protected]>
8
 */
9
10
namespace App;
11
12
class Session
0 ignored issues
show
Coding Style introduced by
Session does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
{
14
	/**
15
	 * Session and cookie init.
16
	 *
17
	 * @return void
18
	 */
19
	public static function init(): void
20
	{
21
		if (PHP_SESSION_ACTIVE === session_status()) {
22
			return;
23
		}
24
		$cookie = session_get_cookie_params();
25
		$cookie['secure'] = \App\RequestUtil::isHttps();
26
		if (isset($_SERVER['HTTP_HOST'])) {
27
			$cookie['domain'] = strtok($_SERVER['HTTP_HOST'], ':');
28
		}
29
		if (isset(\App\Config::$cookieForceHttpOnly)) {
30
			$cookie['httponly'] = \App\Config::$cookieForceHttpOnly;
31
		}
32
		if ($cookie['secure']) {
33
			$cookie['samesite'] = \App\Config::$cookieSameSite;
34
		}
35
		session_name(\App\Config::$sessionName ?: 'YFPSID');
36
		session_set_cookie_params($cookie);
37
		session_save_path(ROOT_DIRECTORY . '/cache/session');
38
		session_start();
39
	}
40
41
	/**
42
	 * Get value from session.
43
	 *
44
	 * @param string $key
45
	 * @param mixed  $defaultValue
46
	 *
47
	 * @return mixed for the key
48
	 */
49
	public static function get(string $key, $defaultValue = false)
50
	{
51
		return \array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $defaultValue;
52
	}
53
54
	/**
55
	 * @param string $key Key in table
56
	 *
57
	 * @return bool if key is definied - return true
58
	 */
59
	public static function has(string $key): bool
60
	{
61
		return \array_key_exists($key, $_SESSION);
62
	}
63
64
	/**
65
	 * @param $key   Key in table
66
	 * @param $value Value for the key
67
	 */
68
	public static function set(string $key, $value)
69
	{
70
		$_SESSION[$key] = $value;
71
	}
72
73
	/**
74
	 * Unset value.
75
	 *
76
	 * @param string $key
77
	 *
78
	 * @return void
79
	 */
80
	public static function unset(string $key)
81
	{
82
		if (\array_key_exists($key, $_SESSION)) {
83
			unset($_SESSION[$key]);
84
		}
85
	}
86
}
87