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

SessionFlash::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
    /**
3
     * Created by PhpStorm.
4
     * User: yabafinet
5
     * Date: 10/01/18
6
     * Time: 09:24 AM
7
     */
8
9
    namespace Sesshin;
10
11
12
    class SessionFlash
13
    {
14
        /**
15
         * Singleton of this class
16
         *
17
         * @var $this
18
         */
19
        public static $singleton;
20
21
        /**
22
         * Object manager of sessions.
23
         *
24
         * @var Session
25
         */
26
        private $session;
27
28
        /**
29
         * Unique ID in each transaction request.
30
         *
31
         * @var int
32
         */
33
        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...
34
35
36
        /**
37
         * key name to save the flash dat
38
         *
39
         * @var string
40
         */
41
        protected $key_name = '_flash';
42
43
        /**
44
         * SessionFlash constructor.
45
         *
46
         * @param Session $session
47
         */
48
        public function __construct(Session $session)
49
        {
50
            $this->session = $session;
51
        }
52
53
        /**
54
         * Add a value in the flash session.
55
         *
56
         * @param $value
57
         * @param string $type
58
         */
59
        public function add($value, $type = 'n')
60
        {
61
            $this->session->put($type, $value);
62
            $this->session->push($this->key_name.'.new', $type);
63
            $this->removeFromOldFlashData([$type]);
64
        }
65
66
67
        /**
68
         * Add a value in the flash session.
69
         *
70
         * @param $key
71
         * @param $value
72
         */
73
        public function set($key, $value)
74
        {
75
            $this->session->put($key, $value);
76
            $this->session->push($this->key_name.'.new', $key);
77
            $this->removeFromOldFlashData([$key]);
78
        }
79
80
        /**
81
         * Get a value in the flash session.
82
         *
83
         * @param $key
84
         * @return mixed
85
         */
86
        public function get($key)
87
        {
88
            return $this->session->getValue($key);
89
        }
90
91
        /**
92
         * Determine if exist key in flash data.
93
         *
94
         * @param $key
95
         * @return bool
96
         */
97
        public function has($key)
98
        {
99
            $current_data = $this->session->getValue($key);
100
101
            if (isset($current_data)) {
102
                return true;
103
            } else {
104
                return false;
105
            }
106
        }
107
108
        /**
109
         * Get all the data or data of a type.
110
         *
111
         * @return mixed
112
         */
113
        public function getCurrentData()
114
        {
115
            $current_data = $this->session->getValue($this->key_name);
116
117
            return isset($current_data) ? $current_data : $current_data = array();
118
        }
119
120
        /**
121
         * Reflash all of the session flash data.
122
         *
123
         * @return void
124
         */
125
        public function reflash()
126
        {
127
            $this->mergeNewFlashes($this->session->getValue($this->key_name.'.old'));
128
            $this->session->setValue($this->key_name.'.old', []);
129
        }
130
131
132
        /**
133
         * Reflash a subset of the current flash data.
134
         *
135
         * @param  array|mixed  $keys
136
         * @return void
137
         */
138
        public function keep($keys = null)
139
        {
140
            $this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
141
            $this->removeFromOldFlashData($keys);
142
        }
143
144
145
        /**
146
         * Merge new flash keys into the new flash array.
147
         *
148
         * @param  array  $keys
149
         * @return void
150
         */
151
        protected function mergeNewFlashes(array $keys)
152
        {
153
            $values = array_unique(array_merge($this->session->getValue($this->key_name.'.new'), $keys));
154
            $this->session->setValue($this->key_name.'.new', $values);
155
        }
156
157
        /**
158
         * Remove the given keys from the old flash data.
159
         *
160
         * @param  array  $keys
161
         * @return void
162
         */
163
        protected function removeFromOldFlashData(array $keys)
164
        {
165
            $old_data = $this->session->getValue($this->key_name.'.old');
166
167
            if (! is_array($old_data)) {
168
                $old_data = array();
169
            }
170
            $this->session->setValue($this->key_name.'.old', array_diff($old_data, $keys));
171
        }
172
173
        /**
174
         * Age the flash data for the session.
175
         *
176
         * @return void
177
         */
178
        public function ageFlashData()
179
        {
180
            $old_data = $this->session->getValue($this->key_name.'.old');
181
            if (! is_array($old_data)) {
182
                $old_data = array();
183
            }
184
            $this->session->forget($old_data);
185
            $this->session->put($this->key_name.'.old', $this->session->getValue($this->key_name.'.new'));
186
            $this->session->put($this->key_name.'.new', []);
187
        }
188
189
190
        /**
191
         * Clear all data flash.
192
         *
193
         */
194
        public function clear()
195
        {
196
            $this->session->unsetValue($this->key_name);
197
        }
198
199
200
        /**
201
         * Calling this class in a singleton way.
202
         *
203
         * @param Session|null $session
204
         * @return SessionFlash
205
         */
206
        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...
207
        {
208
            if (self::$singleton == null) {
209
                self::$singleton = new SessionFlash($session);
0 ignored issues
show
Bug introduced by
It seems like $session defined by parameter $session on line 206 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...
210
            }
211
212
            return self::$singleton;
213
        }
214
    }