SessionBag   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 4 2
A keys() 0 4 2
A set() 0 8 2
A fetch() 0 4 1
A count() 0 4 2
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
     * @return bool true
25
     */
26
    public function has(string $key): bool
27
    {
28
        return isset($_SESSION) ? array_key_exists($key, $_SESSION) : false;
29
    }
30
31
    /**
32
     * Get the SESSION keys.
33
     *
34
     * @return array
35
     */
36
    public function keys(): array
37
    {
38
        return isset($_SESSION) ? array_keys($_SESSION) : [];
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param mixed $value
44
     * @throws \LogicException
45
     */
46
    public function set($key, $value)
47
    {
48
        if (isset($_SESSION)) {
49
            $_SESSION[$key] = $value;
50
        } else {
51
            throw new \LogicException('Cannot set the value of an inactive session');
52
        }
53
    }
54
55
    /**
56
     * Returns a SESSION parameter by name.
57
     *
58
     * @param string $key
59
     * @param mixed|null $default The default value if parameter does not exist
60
     * @return mixed|null
61
     */
62
    public function fetch(string $key, $default = null)
63
    {
64
        return $_SESSION[$key] ?? $default;
65
    }
66
67
    /**
68
     * Returns the number of values.
69
     * @return int
70
     */
71
    public function count(): int
72
    {
73
        return isset($_SESSION) ? count($_SESSION) : 0;
74
    }
75
}