Completed
Pull Request — master (#55)
by Jefersson
02:55
created

DefaultSessionData   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 134
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromDecodedTokenData() 0 8 1
A fromTokenData() 0 14 2
A newEmptySession() 0 8 1
A set() 0 4 1
A get() 0 8 2
A remove() 0 4 1
A clear() 0 4 1
A has() 0 4 1
A hasChanged() 0 4 1
A isEmpty() 0 4 1
A jsonSerialize() 0 4 1
A convertValueToScalar() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace PSR7Sessions\Storageless\Session;
22
23
final class DefaultSessionData implements SessionInterface
24
{
25
    /**
26
     * @var array
27
     */
28
    private $data;
29
30
    /**
31
     * @var array
32
     */
33
    private $originalData;
34
35
    /**
36
     * Instantiation via __construct is not allowed, use
37
     * - {@see DefaultSessionData::fromDecodedTokenData}
38
     * - {@see DefaultSessionData::fromTokenData}
39
     * - {@see DefaultSessionData::newEmptySession}
40
     * instead
41
     */
42 180
    private function __construct()
43
    {
44 180
    }
45
46 30
    public static function fromDecodedTokenData(\stdClass $data) : self
47
    {
48 30
        $instance = new self();
49
50 30
        $instance->originalData = $instance->data = self::convertValueToScalar($data);
51
52 30
        return $instance;
53
    }
54
55 67
    public static function fromTokenData(array $data): self
56
    {
57 67
        $instance = new self();
58
59 67
        $instance->data = [];
60
61 67
        foreach ($data as $key => $value) {
62 65
            $instance->set((string) $key, $value);
63
        }
64
65 67
        $instance->originalData = $instance->data;
66
67 67
        return $instance;
68
    }
69
70 105
    public static function newEmptySession(): self
71
    {
72 105
        $instance = new self();
73
74 105
        $instance->originalData = $instance->data = [];
75
76 105
        return $instance;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 122
    public function set(string $key, $value)
83
    {
84 122
        $this->data[$key] = self::convertValueToScalar($value);
85 122
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 90
    public function get(string $key, $default = null)
91
    {
92 90
        if (! $this->has($key)) {
93 25
            return self::convertValueToScalar($default);
94
        }
95
96 65
        return $this->data[$key];
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 1
    public function remove(string $key)
103
    {
104 1
        unset($this->data[$key]);
105 1
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 4
    public function clear()
111
    {
112 4
        $this->data = [];
113 4
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 91
    public function has(string $key): bool
119
    {
120 91
        return array_key_exists($key, $this->data);
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 83
    public function hasChanged() : bool
127
    {
128 83
        return $this->data !== $this->originalData;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 44
    public function isEmpty() : bool
135
    {
136 44
        return empty($this->data);
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 31
    public function jsonSerialize()
143
    {
144 31
        return $this->data;
145
    }
146
147
    /**
148
     * @param int|bool|string|float|array|object|\JsonSerializable $value
149
     *
150
     * @return int|bool|string|float|array
151
     */
152 167
    private static function convertValueToScalar($value)
153
    {
154 167
        return json_decode(json_encode($value, \JSON_PRESERVE_ZERO_FRACTION), true);
155
    }
156
}
157