Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

SessionBag::keys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\Session;
6
7
use Pivasic\Bundle\Common\Bag\ValueBag;
8
9
/**
10
 * Class SessionBag
11
 * @package Pivasic\Bundle\Common\Session
12
 */
13
class SessionBag extends ValueBag
14
{
15
    public function __construct()
16
    {
17
        parent::__construct([]);
18
    }
19
20
    /**
21
     * Get true if the SESSION parameter is defined.
22
     *
23
     * @param string $key
24
     *
25
     * @return bool true
26
     */
27
    public function has($key)
28
    {
29
        return isset($_SESSION) ? array_key_exists($key, $_SESSION) : false;
30
    }
31
32
    /**
33
     * Get the SESSION keys.
34
     *
35
     * @return array
36
     */
37
    public function keys()
38
    {
39
        return isset($_SESSION) ? array_keys($_SESSION) : [];
40
    }
41
42
    /**
43
     * @param string $key
44
     * @param mixed $value
45
     *
46
     * @throws \LogicException
47
     */
48
    public function set($key, $value)
49
    {
50
        if (isset($_SESSION)) {
51
            $_SESSION[$key] = $value;
52
        } else {
53
            throw new \LogicException('Cannot set the value of an inactive session');
54
        }
55
    }
56
57
    /**
58
     * Returns a SESSION parameter by name.
59
     *
60
     * @param string $key
61
     * @param mixed|null $default The default value if parameter does not exist
62
     *
63
     * @return mixed|null
64
     */
65
    public function fetch($key, $default = null)
66
    {
67
        return $_SESSION[$key] ?? $default;
68
    }
69
70
    /**
71
     * Returns the number of values.
72
     *
73
     * @return int
74
     */
75
    public function count()
76
    {
77
        return isset($_SESSION) ? count($_SESSION) : 0;
78
    }
79
}