Passed
Pull Request — master (#42)
by Korotkov
02:47
created

Session   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 75
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A unset() 0 8 2
A clear() 0 3 1
A set() 0 13 3
A start() 0 3 1
A has() 0 3 2
A get() 0 3 2
A stop() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Jagepard <[email protected]">
7
 * @copyright Copyright (c) 2019, Jagepard
8
 * @license   https://mit-license.org/ MIT
9
 */
10
11
namespace Rudra\Container;
12
13
use Rudra\Container\Interfaces\SessionInterface;
14
15
class Session  implements SessionInterface
16
{
17
    /**
18
     * @param string      $key
19
     * @param string|null $subKey
20
     * @return mixed
21
     */
22
    public function get(string $key, string $subKey = null)
23
    {
24
        return ($subKey === null) ? $_SESSION[$key] : $_SESSION[$key][$subKey];
25
    }
26
27
    /**
28
     * @param string      $key
29
     * @param             $value
30
     * @param string|null $subKey
31
     */
32
    public function set(string $key, $value, string $subKey = null): void
33
    {
34
        if (empty($subKey)) {
35
            $_SESSION[$key] = $value;
36
            return;
37
        }
38
39
        if ($subKey == 'increment') {
40
            $_SESSION[$key][] = $value;
41
            return;
42
        }
43
44
        $_SESSION[$key][$subKey] = $value;
45
    }
46
47
    /**
48
     * @param string      $key
49
     * @param string|null $subKey
50
     * @return bool
51
     */
52
    public function has(string $key, string $subKey = null): bool
53
    {
54
        return empty($subKey) ? isset($_SESSION[$key]) : isset($_SESSION[$key][$subKey]);
55
    }
56
57
    /**
58
     * @param string      $key
59
     * @param string|null $subKey
60
     */
61
    public function unset(string $key, string $subKey = null): void
62
    {
63
        if (empty($subKey)) {
64
            unset($_SESSION[$key]);
65
            return;
66
        }
67
68
        unset($_SESSION[$key][$subKey]);
69
    }
70
71
    /**
72
     * @codeCoverageIgnore
73
     */
74
    public function start(): void
75
    {
76
        session_start();
77
    }
78
79
    /**
80
     * @codeCoverageIgnore
81
     */
82
    public function stop(): void
83
    {
84
        session_destroy();
85
    }
86
87
    public function clear(): void
88
    {
89
        $_SESSION = [];
90
    }
91
}
92