Passed
Push — master ( 3bf5ce...7b847e )
by Korotkov
02:17 queued 10s
created

Session::setFlash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Jagepard <[email protected]">
7
 * @license   https://mit-license.org/ MIT
8
 */
9
10
namespace Rudra\Container;
11
12
use Rudra\Container\Interfaces\ContainerInterface;
13
14
class Session implements ContainerInterface
15
{
16
    public function get(string $key = null)
17
    {
18
        if (empty($key)) {
19
            return $_SESSION;
20
        }
21
22
        if (!array_key_exists($key, $_SESSION)) {
23
            throw new \InvalidArgumentException("No data corresponding to the $key");
24
        }
25
26
        return $_SESSION[$key];
27
    }
28
29
    public function set(array $data): void
30
    {
31
        if (count($data) !== 2) {
32
            throw new \InvalidArgumentException("The array contains the wrong number of elements");
33
        }
34
35
        if (array_key_exists($data[0], $_SESSION) && is_array($_SESSION[$data[0]])) {
36
            array_merge($_SESSION[$data[0]], $data[1]);
37
        } else {
38
            $_SESSION[$data[0]] = $data[1];
39
        }
40
    }
41
42
    public function has(string $key): bool
43
    {
44
        return isset($_SESSION[$key]);
45
    }
46
47
    public function unset(string $key): void
48
    {
49
        unset($_SESSION[$key]);
50
    }
51
52
    public function setFlash(string $type, array $data): void
53
    {
54
        foreach ($data as $key => $value) {
55
            $this->set([$type, [$key => $value]]);
56
        }
57
    }
58
59
    /**
60
     * @codeCoverageIgnore
61
     */
62
    public function start(): void
63
    {
64
        session_start();
65
    }
66
67
    /**
68
     * @codeCoverageIgnore
69
     */
70
    public function stop(): void
71
    {
72
        session_destroy();
73
    }
74
75
    public function clear(): void
76
    {
77
        $_SESSION = [];
78
    }
79
}
80