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

SessionManager::getSessionData()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 14
nc 5
nop 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SanSessionToolbar\Manager\SessionManager.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 SessionManager();
38
        if (! $manager->sessionExists()) {
39
            return;
40
        }
41
42
        $container    = new Container();
43
        $arraysession = $container->getManager()->getStorage()->toArray();
44
45
        $data = array();
46
47
        foreach ($arraysession as $key => $row) {
48
            if ($row instanceof ArrayObject) {
49
                $iterator = $row->getIterator();
50
                while ($iterator->valid()) {
51
                    $data[$key][$iterator->key()] = $iterator->current();
52
                    $iterator->next();
53
                }
54
            }
55
        }
56
57
        return $data;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function sessionSetting($containerName, $keysession, $value = null, $options = array())
64
    {
65
        $container = new Container($containerName);
66
        $set = (!empty($options['set'])) ? $options['set'] : false;
67
        $new = (!empty($options['new'])) ? $options['new'] : false;
68
69
        if ($new) {
70
            return $this->addSession($container, $keysession, $value);
71
        }
72
73
        return $this->setUnset($container, $keysession, $value, $set);
74
    }
75
76
    /**
77
     * Add new session data.
78
     *
79
     * @param Container $container
80
     * @param string    $keysession
81
     * @param string    $value
82
     *
83
     * @return bool
84
     */
85
    private function addSession(Container $container, $keysession, $value)
86
    {
87
        if ($container->offsetExists($keysession)) {
88
            return false;
89
        }
90
91
        $container->offsetSet($keysession, $value);
92
93
        return true;
94
    }
95
96
    /**
97
     * Set/Unset session data.
98
     *
99
     * @param Container   $container
100
     * @param string      $keysession
101
     * @param null|string $value
102
     * @param false|bool  $set
103
     *
104
     * @return bool
105
     */
106
    private function setUnset(Container $container, $keysession, $value = null, $set = false)
107
    {
108
        if ($container->offsetExists($keysession)) {
109
            if ($set) {
110
                $container->offsetSet($keysession, $value);
111
            } else {
112
                $container->offsetUnset($keysession);
113
            }
114
115
            return true;
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function clearSession($byContainer = null)
125
    {
126
        $container = new Container();
127
        $container->getManager()->getStorage()->clear($byContainer);
128
    }
129
}
130