Completed
Pull Request — master (#52)
by Abdul Malik
04:34 queued 02:16
created

SessionManager::setUnset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 4
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace SanSessionToolbar\Manager;
20
21
use Zend\Session\Container;
22
use Zend\Session\SessionManager as ZFSessionManager;
23
use Zend\Stdlib\ArrayObject;
24
25
/**
26
 * A class to manage session data.
27
 *
28
 * @author Abdul Malik Ikhsan <[email protected]>
29
 */
30
final class SessionManager implements SessionManagerInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getSessionData()
36
    {
37
        $manager = new ZFSessionManager();
38
        if (!$manager->sessionExists()) {
39
            return;
40
        }
41
42
        $container = new Container();
43
        $arraysession = $container->getManager()->getStorage()->toArray();
44
45
        return $this->collectSessionData($arraysession);
46
    }
47
48
    /**
49
     * @param array $arraysession
50
     *
51
     * @return array
52
     */
53
    private function collectSessionData(array $arraysession)
54
    {
55
        $data = array();
56
57
        foreach ($arraysession as $key => $row) {
58
            if ($row instanceof ArrayObject) {
59
                $iterator = $row->getIterator();
60
                while ($iterator->valid()) {
61
                    $data[$key][$iterator->key()] = $iterator->current();
62
                    $iterator->next();
63
                }
64
            }
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function sessionSetting($containerName, $keysession, $value = null, $options = array())
74
    {
75
        $container = new Container($containerName);
76
        $set = (!empty($options['set'])) ? $options['set'] : false;
77
        $new = (!empty($options['new'])) ? $options['new'] : false;
78
79
        if ($new) {
80
            return $this->addSession($container, $keysession, $value);
81
        }
82
83
        return $this->setUnset($container, $keysession, $value, $set);
84
    }
85
86
    /**
87
     * Add new session data.
88
     *
89
     * @param Container $container
90
     * @param string    $keysession
91
     * @param string    $value
92
     *
93
     * @return bool
94
     */
95
    private function addSession(Container $container, $keysession, $value)
96
    {
97
        if ($container->offsetExists($keysession)) {
98
            return false;
99
        }
100
101
        $container->offsetSet($keysession, $value);
102
103
        return true;
104
    }
105
106
    /**
107
     * Set/Unset session data.
108
     *
109
     * @param Container   $container
110
     * @param string      $keysession
111
     * @param null|string $value
112
     * @param false|bool  $set
113
     *
114
     * @return bool
115
     */
116
    private function setUnset(Container $container, $keysession, $value = null, $set = false)
117
    {
118
        if ($container->offsetExists($keysession)) {
119
            if ($set) {
120
                $container->offsetSet($keysession, $value);
121
            } else {
122
                $container->offsetUnset($keysession);
123
            }
124
125
            return true;
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function clearSession($byContainer = null)
135
    {
136
        $container = new Container();
137
        $container->getManager()->getStorage()->clear($byContainer);
138
    }
139
}
140