Completed
Pull Request — master (#9)
by
unknown
01:15
created

SessionFlash::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
    namespace Sesshin;
4
5
6
    class SessionFlash
7
    {
8
        /**
9
         * Singleton of this class
10
         *
11
         * @var $this
12
         */
13
        public static $singleton;
14
15
        /**
16
         * Object manager of sessions.
17
         *
18
         * @var Session
19
         */
20
        private $session;
21
22
        /**
23
         * Unique ID in each transaction request.
24
         *
25
         * @var int
26
         */
27
        private $msg_id;
0 ignored issues
show
Unused Code introduced by
The property $msg_id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
30
        /**
31
         * key name to save the flash dat
32
         *
33
         * @var string
34
         */
35
        protected $key_name = '_flash';
36
37
        /**
38
         * SessionFlash constructor.
39
         *
40
         * @param Session $session
41
         */
42
        public function __construct(Session $session)
43
        {
44
            $this->session = $session;
45
        }
46
47
        /**
48
         * Add a value in the flash session.
49
         *
50
         * @param $value
51
         * @param string $type
52
         */
53
        public function add($value, $type = 'n')
54
        {
55
            $this->session->put($type, $value);
56
            $this->session->push($this->key_name.'.new', $type);
57
            $this->removeFromOldFlashData([$type]);
58
        }
59
60
61
        /**
62
         * Add a value in the flash session.
63
         *
64
         * @param $key
65
         * @param $value
66
         */
67
        public function set($key, $value)
68
        {
69
            $this->session->put($key, $value);
70
            $this->session->push($this->key_name.'.new', $key);
71
            $this->removeFromOldFlashData([$key]);
72
        }
73
74
        /**
75
         * Get a value in the flash session.
76
         *
77
         * @param $key
78
         * @return mixed
79
         */
80
        public function get($key)
81
        {
82
            return $this->session->getValue($key);
83
        }
84
85
        /**
86
         * Determine if exist key in flash data.
87
         *
88
         * @param $key
89
         * @return bool
90
         */
91
        public function has($key)
92
        {
93
            $current_data = $this->session->getValue($key);
94
95
            return isset($current_data);
96
        }
97
98
        /**
99
         * Get all the data or data of a type.
100
         *
101
         * @return mixed
102
         */
103
        public function getCurrentData()
104
        {
105
            $current_data = $this->session->getValue($this->key_name);
106
107
            return isset($current_data) ? $current_data : $current_data = array();
108
        }
109
110
        /**
111
         * Reflash all of the session flash data.
112
         *
113
         * @return void
114
         */
115
        public function reflash()
116
        {
117
            $this->mergeNewFlashes($this->session->getValue($this->key_name.'.old'));
118
            $this->session->setValue($this->key_name.'.old', []);
119
        }
120
121
122
        /**
123
         * Reflash a subset of the current flash data.
124
         *
125
         * @param  array|mixed  $keys
126
         * @return void
127
         */
128
        public function keep($keys = null)
129
        {
130
            $this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
131
            $this->removeFromOldFlashData($keys);
132
        }
133
134
135
        /**
136
         * Merge new flash keys into the new flash array.
137
         *
138
         * @param  array  $keys
139
         * @return void
140
         */
141
        protected function mergeNewFlashes(array $keys)
142
        {
143
            $values = array_unique(array_merge($this->session->getValue($this->key_name.'.new'), $keys));
144
            $this->session->setValue($this->key_name.'.new', $values);
145
        }
146
147
        /**
148
         * Remove the given keys from the old flash data.
149
         *
150
         * @param  array  $keys
151
         * @return void
152
         */
153
        protected function removeFromOldFlashData(array $keys)
154
        {
155
            $old_data = $this->session->getValue($this->key_name.'.old');
156
157
            if (! is_array($old_data)) {
158
                $old_data = array();
159
            }
160
            $this->session->setValue($this->key_name.'.old', array_diff($old_data, $keys));
161
        }
162
163
        /**
164
         * Age the flash data for the session.
165
         *
166
         * @return void
167
         */
168
        public function ageFlashData()
169
        {
170
            $old_data = $this->session->getValue($this->key_name.'.old');
171
            if (! is_array($old_data)) {
172
                $old_data = array();
173
            }
174
            $this->session->forget($old_data);
175
            $this->session->put($this->key_name.'.old', $this->session->getValue($this->key_name.'.new'));
176
            $this->session->put($this->key_name.'.new', []);
177
        }
178
179
180
        /**
181
         * Clear all data flash.
182
         *
183
         */
184
        public function clear()
185
        {
186
            $this->session->unsetValue($this->key_name);
187
        }
188
189
190
        /**
191
         * Calling this class in a singleton way.
192
         *
193
         * @param Session|null $session
194
         * @return SessionFlash
195
         */
196
        static function singleton(Session $session = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
197
        {
198
            if (self::$singleton == null) {
199
                self::$singleton = new SessionFlash($session);
0 ignored issues
show
Bug introduced by
It seems like $session defined by parameter $session on line 196 can be null; however, Sesshin\SessionFlash::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
200
            }
201
202
            return self::$singleton;
203
        }
204
    }