Completed
Push — master ( ba068f...084be9 )
by Korotkov
03:09 queued 01:20
created

ContainerSessionTrait::unsetSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2016, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
namespace Rudra\Traits;
12
13
/**
14
 * Trait ContainerSessionTrait
15
 * @package Rudra\Traits
16
 */
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
83
    {
84
        session_destroy();
85
    }
86
87
    public function clearSession(): void
88
    {
89
        $_SESSION = [];
90
    }
91
}
92