Completed
Pull Request — master (#30)
by Rudolph
04:12
created

StorableSession   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 2
dl 0
loc 115
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A fromId() 0 10 1
A __construct() 0 6 1
A getId() 0 4 1
A set() 0 5 1
A get() 0 4 1
A remove() 0 5 1
A clear() 0 5 1
A has() 0 4 1
A hasChanged() 0 4 1
A isEmpty() 0 4 1
A jsonSerialize() 0 4 1
A save() 0 4 1
1
<?php
2
3
namespace PSR7Session\Session;
4
5
use PSR7Session\Id\SessionIdInterface;
6
use PSR7Session\Id\UuidSessionId;
7
use PSR7Session\Storage\StorageInterface;
8
9
class StorableSession implements StorableSessionInterface
10
{
11
    /** @var SessionIdInterface */
12
    private $id;
13
    /** @var SessionInterface */
14
    private $wrappedSession;
15
    /** @var StorageInterface */
16
    private $storage;
17
18
    public static function create(StorageInterface $storage):StorableSession
19
    {
20
        return new self(DefaultSessionData::newEmptySession(), $storage);
21
    }
22
23
    /**
24
     * @internal Should only be called by a storage
25
     */
26
    public static function fromId(
27
        SessionInterface $wrappedSession,
28
        StorageInterface $storage,
29
        SessionIdInterface $id
30
    ):StorableSession
31
    {
32
        $session = new self($wrappedSession, $storage);
33
        $session->id = $id;
34
        return $session;
35
    }
36
37
    public function __construct(SessionInterface $wrappedSession, StorageInterface $storage)
38
    {
39
        $this->id = new UuidSessionId;
40
        $this->wrappedSession = $wrappedSession;
41
        $this->storage = $storage;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getId() : SessionIdInterface
48
    {
49
        return $this->id;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function set(string $key, $value)
56
    {
57
        $this->wrappedSession->set($key, $value);
58
        $this->save();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function get(string $key, $default = null)
65
    {
66
        return $this->wrappedSession->get($key, $default);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function remove(string $key)
73
    {
74
        $this->wrappedSession->remove($key);
75
        $this->save();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function clear()
82
    {
83
        $this->wrappedSession->clear();
84
        $this->save();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function has(string $key) : bool
91
    {
92
        return $this->wrappedSession->has($key);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function hasChanged() : bool
99
    {
100
        return $this->wrappedSession->hasChanged();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function isEmpty() : bool
107
    {
108
        return $this->wrappedSession->isEmpty();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function jsonSerialize()
115
    {
116
        return $this->wrappedSession->jsonSerialize();
117
    }
118
119
    private function save()
120
    {
121
        $this->storage->save($this);
122
    }
123
}
124