| Total Complexity | 12 |
| Total Lines | 73 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | trait ContainerSessionTrait |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param string $key |
||
| 22 | * @param string|null $subKey |
||
| 23 | * @return mixed |
||
| 24 | */ |
||
| 25 | public function getSession(string $key, string $subKey = null) |
||
| 26 | { |
||
| 27 | return ($subKey === null) ? $_SESSION[$key] : $_SESSION[$key][$subKey]; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $key |
||
| 32 | * @param $value |
||
| 33 | * @param string|null $subKey |
||
| 34 | */ |
||
| 35 | public function setSession(string $key, $value, string $subKey = null): void |
||
| 36 | { |
||
| 37 | if (empty($subKey)) { |
||
| 38 | $_SESSION[$key] = $value; |
||
| 39 | } else { |
||
| 40 | if ($subKey == 'increment') { |
||
| 41 | $_SESSION[$key][] = $value; |
||
| 42 | } else { |
||
| 43 | $_SESSION[$key][$subKey] = $value; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $key |
||
| 50 | * @param string|null $subKey |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function hasSession(string $key, string $subKey = null): bool |
||
| 54 | { |
||
| 55 | return empty($subKey) ? isset($_SESSION[$key]) : isset($_SESSION[$key][$subKey]); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $key |
||
| 60 | * @param string|null $subKey |
||
| 61 | */ |
||
| 62 | public function unsetSession(string $key, string $subKey = null): void |
||
| 63 | { |
||
| 64 | if (empty($subKey)) { |
||
| 65 | unset($_SESSION[$key]); |
||
| 66 | } else { |
||
| 67 | unset($_SESSION[$key][$subKey]); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @codeCoverageIgnore |
||
| 73 | */ |
||
| 74 | public function startSession(): void |
||
| 75 | { |
||
| 76 | session_start(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @codeCoverageIgnore |
||
| 81 | */ |
||
| 82 | public function stopSession(): void |
||
| 85 | } |
||
| 86 | |||
| 87 | public function clearSession(): void |
||
| 88 | { |
||
| 89 | $_SESSION = []; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |