Completed
Pull Request — master (#106)
by Marco
02:15 queued 24s
created

LazySession   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 110
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
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 LazySession implements SessionInterface
24
{
25
    /** @internal do not access directly: use {@see LazySession::getRealSession} instead */
26
    private ?SessionInterface $realSession = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
27
28
    /** @var callable */
29
    private $sessionLoader;
30
31
    /**
32
     * Instantiation via __construct is not allowed, use {@see LazySession::fromContainerBuildingCallback} instead
33
     */
34 57
    private function __construct()
35
    {
36 57
    }
37
38 57
    public static function fromContainerBuildingCallback(callable $sessionLoader) : self
39
    {
40 57
        $instance = new self();
41
42 57
        $instance->sessionLoader = $sessionLoader;
43
44 57
        return $instance;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 21
    public function set(string $key, $value) : void
51
    {
52 21
        $this->getRealSession()->set($key, $value);
53 21
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 7
    public function get(string $key, $default = null)
59
    {
60 7
        return $this->getRealSession()->get($key, $default);
61
    }
62
63 2
    public function remove(string $key) : void
64
    {
65 2
        $this->getRealSession()->remove($key);
66 2
    }
67
68 4
    public function clear() : void
69
    {
70 4
        $this->getRealSession()->clear();
71 4
    }
72
73 1
    public function has(string $key) : bool
74
    {
75 1
        return $this->getRealSession()->has($key);
76
    }
77
78 50
    public function hasChanged() : bool
79
    {
80 50
        return $this->realSession && $this->realSession->hasChanged();
81
    }
82
83 44
    public function isEmpty() : bool
84
    {
85 44
        return $this->getRealSession()->isEmpty();
86
    }
87
88 26
    public function jsonSerialize() : object
89
    {
90 26
        return $this->getRealSession()->jsonSerialize();
91
    }
92
93
    /**
94
     * Get or initialize the session
95
     */
96 51
    private function getRealSession() : SessionInterface
97
    {
98 51
        return $this->realSession ?? $this->realSession = $this->loadSession();
99
    }
100
101
    /**
102
     * Type-safe wrapper that ensures that the given callback returns the expected type of object, when called
103
     */
104 51
    private function loadSession() : SessionInterface
105
    {
106 51
        $sessionLoader = $this->sessionLoader;
107
108 51
        return $sessionLoader();
109
    }
110
}
111