Completed
Pull Request — master (#30)
by Rudolph
04:05 queued 53s
created

StorableSession   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 4
dl 0
loc 88
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 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
    public function getId() : SessionIdInterface
47
    {
48
        return $this->id;
49
    }
50
51
    public function set(string $key, $value)
52
    {
53
        $this->wrappedSession->set($key, $value);
54
        $this->save();
55
    }
56
57
    public function get(string $key, $default = null)
58
    {
59
        return $this->wrappedSession->get($key, $default);
60
    }
61
62
    public function remove(string $key)
63
    {
64
        $this->wrappedSession->remove($key);
65
        $this->save();
66
    }
67
68
    public function clear()
69
    {
70
        $this->wrappedSession->clear();
71
        $this->save();
72
    }
73
74
    public function has(string $key) : bool
75
    {
76
        return $this->wrappedSession->has($key);
77
    }
78
79
    public function hasChanged() : bool
80
    {
81
        return $this->wrappedSession->hasChanged();
82
    }
83
84
    public function isEmpty() : bool
85
    {
86
        return $this->wrappedSession->isEmpty();
87
    }
88
89
    public function jsonSerialize()
90
    {
91
        return $this->wrappedSession->jsonSerialize();
92
    }
93
94
    private function save()
95
    {
96
        $this->storage->save($this);
97
    }
98
}
99