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

StorableSession::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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