Completed
Pull Request — master (#52)
by Abdul Malik
02:22
created

SessionManager::collectSessionData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 4
nop 1
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
     * @var array $arraysession
50
     * @return array
51
     */
52
    private function collectSessionData(array $arraysession)
53
    {
54
        $data = array();
55
56
        foreach ($arraysession as $key => $row) {
57
            if ($row instanceof ArrayObject) {
58
                $iterator = $row->getIterator();
59
                while ($iterator->valid()) {
60
                    $data[$key][$iterator->key()] = $iterator->current();
61
                    $iterator->next();
62
                }
63
            }
64
        }
65
66
        return $data;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function sessionSetting($containerName, $keysession, $value = null, $options = array())
73
    {
74
        $container = new Container($containerName);
75
        $set = (!empty($options['set'])) ? $options['set'] : false;
76
        $new = (!empty($options['new'])) ? $options['new'] : false;
77
78
        if ($new) {
79
            return $this->addSession($container, $keysession, $value);
80
        }
81
82
        return $this->setUnset($container, $keysession, $value, $set);
83
    }
84
85
    /**
86
     * Add new session data.
87
     *
88
     * @param Container $container
89
     * @param string    $keysession
90
     * @param string    $value
91
     *
92
     * @return bool
93
     */
94
    private function addSession(Container $container, $keysession, $value)
95
    {
96
        if ($container->offsetExists($keysession)) {
97
            return false;
98
        }
99
100
        $container->offsetSet($keysession, $value);
101
102
        return true;
103
    }
104
105
    /**
106
     * Set/Unset session data.
107
     *
108
     * @param Container   $container
109
     * @param string      $keysession
110
     * @param null|string $value
111
     * @param false|bool  $set
112
     *
113
     * @return bool
114
     */
115
    private function setUnset(Container $container, $keysession, $value = null, $set = false)
116
    {
117
        if ($container->offsetExists($keysession)) {
118
            if ($set) {
119
                $container->offsetSet($keysession, $value);
120
            } else {
121
                $container->offsetUnset($keysession);
122
            }
123
124
            return true;
125
        }
126
127
        return false;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function clearSession($byContainer = null)
134
    {
135
        $container = new Container();
136
        $container->getManager()->getStorage()->clear($byContainer);
137
    }
138
}
139