Completed
Push — master ( 3c5e5f...6e3c38 )
by Korotkov
02:32
created

ContainerSessionTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSession() 0 4 2
A setSession() 0 12 3
A hasSession() 0 4 2
A unsetSession() 0 8 2
A startSession() 0 4 1
A stopSession() 0 4 1
A clearSession() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Date: 06.04.17
7
 * Time: 15:04
8
 *
9
 * @author    : Korotkov Danila <[email protected]>
10
 * @copyright Copyright (c) 2016, Korotkov Danila
11
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
12
 */
13
14
namespace Rudra;
15
16
/**
17
 * Class ContainerSessionTrait
18
 *
19
 * @package Rudra
20
 */
21
trait ContainerSessionTrait
22
{
23
24
    /**
25
     * @param string      $key
26
     * @param string|null $subKey
27
     *
28
     * @return mixed
29
     */
30
    public function getSession(string $key, string $subKey = null)
31
    {
32
        return ($subKey === null) ? $_SESSION[$key] : $_SESSION[$key][$subKey];
33
    }
34
35
    /**
36
     * @param string      $key
37
     * @param             $value
38
     * @param string|null $subKey
39
     */
40
    public function setSession(string $key, $value, string $subKey = null): void
41
    {
42
        if (empty($subKey)) {
43
            $_SESSION[$key] = $value;
44
        } else {
45
            if ($subKey == 'increment') {
46
                $_SESSION[$key][] = $value;
47
            } else {
48
                $_SESSION[$key][$subKey] = $value;
49
            }
50
        }
51
    }
52
53
    /**
54
     * @param string $key
55
     * @param string $subKey
56
     *
57
     * @return bool
58
     */
59
    public function hasSession(string $key, string $subKey = null): bool
60
    {
61
        return empty($subKey) ? isset($_SESSION[$key]) : isset($_SESSION[$key][$subKey]);
62
    }
63
64
    /**
65
     * @param string      $key
66
     * @param string|null $subKey
67
     */
68
    public function unsetSession(string $key, string $subKey = null): void
69
    {
70
        if (empty($subKey)) {
71
            unset($_SESSION[$key]);
72
        } else {
73
            unset($_SESSION[$key][$subKey]);
74
        }
75
    }
76
77
    /**
78
     * @codeCoverageIgnore
79
     */
80
    public function startSession(): void
81
    {
82
        session_start();
83
    }
84
85
    /**
86
     * @codeCoverageIgnore
87
     */
88
    public function stopSession(): void
89
    {
90
        session_destroy();
91
    }
92
93
    public function clearSession(): void
94
    {
95
        $_SESSION = [];
96
    }
97
}