Notification::init()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Flextype Components
5
 *
6
 * @author Sergey Romanenko <[email protected]>
7
 * @link http://components.flextype.org
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Flextype\Component\Notification;
14
15
class Notification
16
{
17
    /**
18
     * Notifications session key
19
     *
20
     * @var string
21
     */
22
    const SESSION_KEY = 'notifications';
23
24
    /**
25
     * Notifications array
26
     *
27
     * @var array
28
     */
29
    private static $notifications = [];
30
31
    /**
32
     * Returns a specific variable from the Notifications array.
33
     *
34
     * echo Notification::get('success');
35
     * echo Notification::get('errors');
36
     *
37
     * @param  string $key Variable name
38
     * @return mixed
39
     */
40
    public static function get(string $key)
41
    {
42
        return isset(Notification::$notifications[$key]) ? Notification::$notifications[$key] : null;
43
    }
44
45
    /**
46
     * Adds specific variable to the Notifications array.
47
     *
48
     * Notification::set('success', 'Data has been saved with success!');
49
     * Notification::set('errors', 'Data not saved!');
50
     *
51
     * @param string $key   Variable name
52
     * @param mixed  $value Variable value
53
     */
54
    public static function set(string $key, $value) : void
55
    {
56
        $_SESSION[Notification::SESSION_KEY][$key] = $value;
57
    }
58
59
    /**
60
     * Adds specific variable to the Notifications array for current page.
61
     *
62
     * Notification::setNow('success', 'Success!');
63
     *
64
     * @param string $var   Variable name
65
     * @param mixed  $value Variable value
66
     */
67
    public static function setNow(string $key, $value) : void
68
    {
69
        Notification::$notifications[$key] = $value;
70
    }
71
72
    /**
73
     * Clears the Notifications array.
74
     *
75
     * Notification::clean();
76
     *
77
     * Data that previous pages stored will not be deleted, just the data that
78
     * this page stored itself.
79
     */
80
    public static function clean() : void
81
    {
82
        $_SESSION[Notification::SESSION_KEY] = [];
83
    }
84
85
    /**
86
     * Initializes the Notification service.
87
     *
88
     * Notification::init();
89
     *
90
     * This will read notification/flash data from the $_SESSION variable and load it into
91
     * the $this->previous array.
92
     */
93
    public static function init() : void
94
    {
95
        if ( ! empty($_SESSION[Notification::SESSION_KEY]) && is_array($_SESSION[Notification::SESSION_KEY])) {
96
            Notification::$notifications = $_SESSION[Notification::SESSION_KEY];
97
        }
98
99
        $_SESSION[Notification::SESSION_KEY] = [];
100
    }
101
}
102