Passed
Pull Request — master (#24)
by Alexander
03:09
created

SessionCurrentIdentityStorage::get()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 14
nc 7
nop 0
dl 0
loc 26
ccs 0
cts 11
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\User\CurrentIdentity\Storage;
6
7
use Yiisoft\Session\SessionInterface;
8
9
/**
10
 * Stores current identity ID in a session.
11
 */
12
final class SessionCurrentIdentityStorage implements CurrentIdentityStorageInterface
13
{
14
    private const SESSION_AUTH_ID = '__auth_id';
15
    private const SESSION_AUTH_EXPIRE = '__auth_expire';
16
    private const SESSION_AUTH_ABSOLUTE_EXPIRE = '__auth_absolute_expire';
17
18
    /**
19
     * @var int|null the number of seconds in which the user will be logged out automatically in case of
20
     * remaining inactive. If this property is not set, the user will be logged out after
21
     * the current session expires.
22
     */
23
    private ?int $authTimeout = null;
24
25
    /**
26
     * @var int|null the number of seconds in which the user will be logged out automatically
27
     * regardless of activity.
28
     */
29
    private ?int $absoluteAuthTimeout = null;
30
31
    private SessionInterface $session;
32
33
    public function __construct(SessionInterface $session)
34
    {
35
        $this->session = $session;
36
    }
37
38
    public function setAuthTimeout(int $timeout = null): self
39
    {
40
        $this->authTimeout = $timeout;
41
        return $this;
42
    }
43
44
    public function setAbsoluteAuthTimeout(int $timeout = null): self
45
    {
46
        $this->absoluteAuthTimeout = $timeout;
47
        return $this;
48
    }
49
50
    public function get(): ?string
51
    {
52
        /** @var mixed $id */
53
        $id = $this->session->get(self::SESSION_AUTH_ID);
54
55
        if (
56
            $id !== null &&
57
            ($this->authTimeout !== null || $this->absoluteAuthTimeout !== null)
58
        ) {
59
            $expire = $this->getExpire();
60
            $expireAbsolute = $this->getExpireAbsoulte();
61
62
            if (
63
                ($expire !== null && $expire < time()) ||
64
                ($expireAbsolute !== null && $expireAbsolute < time())
65
            ) {
66
                $this->clear();
67
                return null;
68
            }
69
70
            if ($this->authTimeout !== null) {
71
                $this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout);
72
            }
73
        }
74
75
        return $id === null ? null : (string)$id;
76
    }
77
78
    private function getExpire(): ?int
79
    {
80
        /** @var mixed $expire */
81
        $expire = $this->authTimeout !== null
82
            ? $this->session->get(self::SESSION_AUTH_EXPIRE)
83
            : null;
84
        return $expire !== null ? (int)$expire : null;
85
    }
86
87
    private function getExpireAbsoulte(): ?int
88
    {
89
        /** @var mixed $expire */
90
        $expire = $this->absoluteAuthTimeout !== null
91
            ? $this->session->get(self::SESSION_AUTH_ABSOLUTE_EXPIRE)
92
            : null;
93
        return $expire !== null ? (int)$expire : null;
94
    }
95
96
    public function set(string $id): void
97
    {
98
        $this->switchId($id);
99
    }
100
101
    public function clear(): void
102
    {
103
        $this->switchId(null);
104
    }
105
106
    private function switchId(?string $id): void
107
    {
108
        $this->session->regenerateID();
109
110
        $this->session->remove(self::SESSION_AUTH_ID);
111
        $this->session->remove(self::SESSION_AUTH_EXPIRE);
112
113
        if ($id === null) {
114
            return;
115
        }
116
117
        $this->session->set(self::SESSION_AUTH_ID, $id);
118
        if ($this->authTimeout !== null) {
119
            $this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout);
120
        }
121
        if ($this->absoluteAuthTimeout !== null) {
122
            $this->session->set(self::SESSION_AUTH_ABSOLUTE_EXPIRE, time() + $this->absoluteAuthTimeout);
123
        }
124
    }
125
}
126