Passed
Push — 1.0.0-dev ( 896dae...8851fa )
by nguereza
02:32
created

Session::clearAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
    
31
    class Session extends BaseClass {
32
		
33
        /**
34
         * The session flash key to use
35
         * @const
36
         */
37
        const SESSION_FLASH_KEY = 'session_flash';
38
39
40
        /**
41
         * Construct new instance
42
         */
43
        public function __construct() {
44
            parent::__construct();
45
        }
46
47
        /**
48
         * Get the session item value
49
         * @param  string $item    the session item name to get
50
         * @param  mixed $default the default value to use if can not find the session item in the list
51
         * @return mixed          the session value if exist or the default value
52
         */
53
        public function get($item, $default = null) {
54
            $sessions = get_instance()->globalvar->session();
55
            $this->logger->debug('Getting session data for item [' . $item . '] ...');
56
            if (array_key_exists($item, $sessions)) {
57
                $this->logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($sessions[$item]) . ']');
58
                return $sessions[$item];
59
            }
60
            $this->logger->warning('Cannot find session item [' . $item . '] using the default value [' . $default . ']');
61
            return $default;
62
        }
63
64
        /**
65
         * Set the session item value
66
         * @param string $item  the session item name to set
67
         * @param mixed $value the session item value
68
         */
69
        public function set($item, $value) {
70
            $this->logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']');
71
            get_instance()->globalvar->setSession($item, $value);
72
        }
73
74
        /**
75
         * Get the session flash item value
76
         * @param  string $item    the session flash item name to get
77
         * @param  mixed $default the default value to use if can not find the session flash item in the list
78
         * @return mixed          the session flash value if exist or the default value
79
         */
80
        public function getFlash($item, $default = null) {
81
            $key = self::SESSION_FLASH_KEY . '_' . $item;
82
            $return = $default;
83
            $sessions = get_instance()->globalvar->session();
84
            if (array_key_exists($key, $sessions)) {
85
                $return = $sessions[$key];
86
                get_instance()->globalvar->removeSession($key);
87
            } else {
88
                $this->logger->warning('Cannot find session flash item [' . $key . '] using the default value [' . $default . ']');
89
            }
90
            return $return;
91
        }
92
93
        /**
94
         * Check whether the given session flash item exists
95
         * @param  string  $item the session flash item name
96
         * @return boolean 
97
         */
98
        public function hasFlash($item) {
99
            $key = self::SESSION_FLASH_KEY . '_' . $item;
100
            return array_key_exists($key, get_instance()->globalvar->session());
101
        }
102
103
        /**
104
         * Set the session flash item value
105
         * @param string $item  the session flash item name to set
106
         * @param mixed $value the session flash item value
107
         */
108
        public function setFlash($item, $value) {
109
            $key = self::SESSION_FLASH_KEY . '_' . $item;
110
            get_instance()->globalvar->setSession($key, $value);
111
        }
112
113
        /**
114
         * Clear the session item in the list
115
         * @param  string $item the session item name to be deleted
116
         *
117
         * @return boolean
118
         */
119
        public function clear($item) {
120
            if (array_key_exists($item, get_instance()->globalvar->session())) {
121
                $this->logger->info('Deleting of session for item [' . $item . ' ]');
122
                get_instance()->globalvar->removeSession($item);
123
                return true;
124
            } 
125
            $this->logger->warning('Session item [' . $item . '] to be deleted does not exists');
126
            return false;
127
        }
128
		
129
        /**
130
         * Clear the session flash item in the list
131
         * @param  string $item the session flash item name to be deleted
132
         *
133
         * @return boolean
134
         */
135
        public function clearFlash($item) {
136
            $key = self::SESSION_FLASH_KEY . '_' . $item;
137
            if (array_key_exists($key, get_instance()->globalvar->session())) {
138
                $this->logger->info('Delete session flash for item [' . $key . ']');
139
                get_instance()->globalvar->removeSession($key);
140
                return true;
141
            } 
142
            $this->logger->warning('Dession flash item [' . $key . '] to be deleted does not exists');
143
            return false;
144
        }
145
146
        /**
147
         * Check whether the given session item exists
148
         * @param  string  $item the session item name
149
         * @return boolean 
150
         */
151
        public function exists($item) {
152
            return array_key_exists($item, get_instance()->globalvar->session());
153
        }
154
155
        /**
156
         * Destroy all session data values
157
         */
158
        public function clearAll() {
159
            session_unset();
160
            session_destroy();
161
        }
162
163
    }
164