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

SessionFlash::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
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
            return isset($current_data);
102
        }
103
104
        /**
105
         * Get all the data or data of a type.
106
         *
107
         * @return mixed
108
         */
109
        public function getCurrentData()
110
        {
111
            $current_data = $this->session->getValue($this->key_name);
112
113
            return isset($current_data) ? $current_data : $current_data = array();
114
        }
115
116
        /**
117
         * Reflash all of the session flash data.
118
         *
119
         * @return void
120
         */
121
        public function reflash()
122
        {
123
            $this->mergeNewFlashes($this->session->getValue($this->key_name.'.old'));
124
            $this->session->setValue($this->key_name.'.old', []);
125
        }
126
127
128
        /**
129
         * Reflash a subset of the current flash data.
130
         *
131
         * @param  array|mixed  $keys
132
         * @return void
133
         */
134
        public function keep($keys = null)
135
        {
136
            $this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
137
            $this->removeFromOldFlashData($keys);
138
        }
139
140
141
        /**
142
         * Merge new flash keys into the new flash array.
143
         *
144
         * @param  array  $keys
145
         * @return void
146
         */
147
        protected function mergeNewFlashes(array $keys)
148
        {
149
            $values = array_unique(array_merge($this->session->getValue($this->key_name.'.new'), $keys));
150
            $this->session->setValue($this->key_name.'.new', $values);
151
        }
152
153
        /**
154
         * Remove the given keys from the old flash data.
155
         *
156
         * @param  array  $keys
157
         * @return void
158
         */
159
        protected function removeFromOldFlashData(array $keys)
160
        {
161
            $old_data = $this->session->getValue($this->key_name.'.old');
162
163
            if (! is_array($old_data)) {
164
                $old_data = array();
165
            }
166
            $this->session->setValue($this->key_name.'.old', array_diff($old_data, $keys));
167
        }
168
169
        /**
170
         * Age the flash data for the session.
171
         *
172
         * @return void
173
         */
174
        public function ageFlashData()
175
        {
176
            $old_data = $this->session->getValue($this->key_name.'.old');
177
            if (! is_array($old_data)) {
178
                $old_data = array();
179
            }
180
            $this->session->forget($old_data);
181
            $this->session->put($this->key_name.'.old', $this->session->getValue($this->key_name.'.new'));
182
            $this->session->put($this->key_name.'.new', []);
183
        }
184
185
186
        /**
187
         * Clear all data flash.
188
         *
189
         */
190
        public function clear()
191
        {
192
            $this->session->unsetValue($this->key_name);
193
        }
194
195
196
        /**
197
         * Calling this class in a singleton way.
198
         *
199
         * @param Session|null $session
200
         * @return SessionFlash
201
         */
202
        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...
203
        {
204
            if (self::$singleton == null) {
205
                self::$singleton = new SessionFlash($session);
0 ignored issues
show
Bug introduced by
It seems like $session defined by parameter $session on line 202 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...
206
            }
207
208
            return self::$singleton;
209
        }
210
    }