FlashData   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 70.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 58
ccs 19
cts 27
cp 0.7037
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A write() 0 3 1
A read() 0 8 3
A remove() 0 4 1
A add() 0 4 1
A __construct() 0 3 1
A has() 0 3 1
A get() 0 3 1
1
<?php
2
3
namespace Nip\FlashData;
4
5
/**
6
 * Nip Framework
7
 *
8
 * LICENSE
9
 *
10
 * This source file is subject to the license that is bundled
11
 * with this package in the file LICENSE.txt.
12
 *
13
 * @category   Nip
14
 * @copyright  2009 Nip Framework
15
 * @version    SVN: $Id: Flash.php 14 2009-04-13 11:24:22Z victor.stanciu $
16
 */
17
18
class FlashData
19
{
20
    protected $previous = [];
21
    protected $next = [];
22
23
    public const  DEFAULT_SESSION_KEY = 'flash-data';
24
25
    protected $sessionKey = self::DEFAULT_SESSION_KEY;
26
27
    /**
28
     * FlashData constructor.
29
     */
30 3
    public function __construct()
31
    {
32 3
        $this->read();
33 3
    }
34
35 3
    public function read()
36
    {
37 3
        if (isset($_SESSION[$this->sessionKey])) {
38 2
            $data = $_SESSION[$this->sessionKey];
39 2
            if (is_array($data)) {
40 2
                $this->previous = $data;
41
            }
42 2
            unset($_SESSION[$this->sessionKey]);
43
        }
44 3
    }
45
46
    public function has($var)
47
    {
48
        return isset($this->previous[trim($var)]);
49
    }
50
51 1
    public function get($var)
52
    {
53 1
        return $this->previous[trim($var)] ?? null;
54
    }
55
56 1
    public function add($var, $value)
57
    {
58 1
        $this->next[trim($var)] = $value;
59 1
        $this->write();
60 1
    }
61
62 1
    protected function write()
63
    {
64 1
        $_SESSION[$this->sessionKey] = $this->next;
65 1
    }
66
67
    public function remove($var)
68
    {
69
        unset($this->next[trim($var)]);
70
        $this->write();
71
    }
72
73
    protected function clear()
74
    {
75
        $this->next = [];
76
    }
77
}
78