GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.x ( 854d93...81f322 )
by Jindřich
01:52
created

User   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 89.55%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 0
loc 205
ccs 60
cts 67
cp 0.8955
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getLoginId() 0 4 1
A getRoleId() 0 4 1
A getUnitId() 0 4 1
A getLogoutDate() 0 4 1
A setLoginData() 0 10 1
A updateLoginData() 0 26 5
A resetLoginData() 0 4 1
A isLoggedIn() 0 16 6
A isAuthConfirmed() 0 4 1
A setAuthConfirmed() 0 5 1
A confirmAuth() 0 9 2
A updateLogoutTime() 0 22 3
A saveToSession() 0 6 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Skautis;
5
6
use DateTime;
7
use Skautis\SessionAdapter\AdapterInterface;
8
use Skautis\Wsdl\WsdlManager;
9
10
/**
11
 * @author Petr Morávek <[email protected]>
12
 */
13
class User
14
{
15
16
    public const ID_LOGIN = 'ID_Login';
17
    public const ID_ROLE = 'ID_Role';
18
    public const ID_UNIT = 'ID_Unit';
19
    public const LOGOUT_DATE = 'LOGOUT_Date';
20
    private const AUTH_CONFIRMED = 'AUTH_Confirmed';
21
    private const SESSION_ID = 'skautis_user_data';
22
23
    /**
24
     * @var WsdlManager
25
     */
26
    private $wsdlManager;
27
28
    /**
29
     * @var AdapterInterface|null
30
     */
31
    private $session;
32
33
    /**
34
     * Informace o přihlášení uživatele
35
     *
36
     * @var array
37
     */
38
    protected $loginData = [];
39
40
41
    /**
42
     * @param WsdlManager $wsdlManager
43
     * @param AdapterInterface|null $session
44
     */
45 4
    public function __construct(WsdlManager $wsdlManager, AdapterInterface $session = null)
46
    {
47 4
        $this->wsdlManager = $wsdlManager;
48 4
        $this->session = $session;
49
50 4
        if ($session !== null && $session->has(self::SESSION_ID)) {
51
            $this->loginData = (array)$session->get(self::SESSION_ID);
52
        }
53 4
    }
54
55 3
    public function getLoginId(): ?string
56
    {
57 3
        return $this->loginData[self::ID_LOGIN] ?? null;
58
    }
59
60 2
    public function getRoleId(): ?int
61
    {
62 2
        return $this->loginData[self::ID_ROLE] ?? null;
63
    }
64
65 2
    public function getUnitId(): ?int
66
    {
67 2
        return $this->loginData[self::ID_UNIT] ?? null;
68
    }
69
70
    /**
71
     * Vrací datum a čas automatického odhlášení ze skautISu
72
     */
73 3
    public function getLogoutDate(): ?DateTime
74
    {
75 3
        return $this->loginData[self::LOGOUT_DATE] ?? null;
76
    }
77
78
    /**+
79
     * Hromadné nastavení po přihlášení
80
     */
81 3
    public function setLoginData(
82
      ?string $loginId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
83
      ?int $roleId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
84
      ?int $unitId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
85
      ?DateTime $logoutDate = null
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
86
    ): self {
87 3
        $this->loginData = [];
88
89 3
        return $this->updateLoginData($loginId, $roleId, $unitId, $logoutDate);
90
    }
91
    
92
    /**
93
     * Hromadná změna údajů, bez vymazání stávajících
94
     */
95 3
    public function updateLoginData(
96
      ?string $loginId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
97
      ?int $roleId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
98
      ?int $unitId = null,
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
99
      ?DateTime $logoutDate = null
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 6
Loading history...
100
    ): self {
101 3
        if ($loginId !== null) {
102 3
            $this->loginData[self::ID_LOGIN] = $loginId;
103
        }
104
105 3
        if ($roleId !== null) {
106 3
            $this->loginData[self::ID_ROLE] = $roleId;
107
        }
108
109 3
        if ($unitId !== null) {
110 3
            $this->loginData[self::ID_UNIT] = $unitId;
111
        }
112
113 3
        if ($logoutDate !== null) {
114 3
            $this->loginData[self::LOGOUT_DATE] = $logoutDate;
115
        }
116
117 3
        $this->saveToSession();
118
119 3
        return $this;
120
    }
121
122
    /**
123
     * Hromadný reset dat po odhlášení
124
     */
125 1
    public function resetLoginData(): self
126
    {
127 1
        return $this->setLoginData();
128
    }
129
130
    /**
131
     * Kontoluje, jestli je přihlášení platné.
132
     * Pro správné fungování je nezbytně nutné, aby byl na serveru nastaven správný čas.
133
     *
134
     * @param bool $hardCheck vynutí kontrolu přihlášení na serveru
135
     */
136 2
    public function isLoggedIn(bool $hardCheck = false): bool
137
    {
138 2
        if (empty($this->loginData[self::ID_LOGIN])) {
139 1
            return false;
140
        }
141
142 1
        if ($hardCheck || !$this->isAuthConfirmed()) {
143 1
            $this->confirmAuth();
144
        }
145
146 1
        if ($this->getLogoutDate() === null) {
147
          return false;
148
        }
149
150 1
        return $this->isAuthConfirmed() && $this->getLogoutDate()->getTimestamp() > time();
151
    }
152
153
    /**
154
     * Bylo potvrzeno přihlášení dotazem na skautIS?
155
     */
156 1
    protected function isAuthConfirmed(): bool
157
    {
158 1
        return !empty($this->loginData[self::AUTH_CONFIRMED]);
159
    }
160
161 1
    protected function setAuthConfirmed(bool $isConfirmed): void
162
    {
163 1
        $this->loginData[self::AUTH_CONFIRMED] = $isConfirmed;
164 1
        $this->saveToSession();
165 1
    }
166
167
    /**
168
     * Potvrdí (a prodlouží) přihlášení dotazem na skautIS.
169
     */
170 1
    protected function confirmAuth(): void
171
    {
172
        try {
173 1
            $this->updateLogoutTime();
174 1
            $this->setAuthConfirmed(true);
175
        } catch (\Exception $e) {
176
            $this->setAuthConfirmed(false);
177
        }
178 1
    }
179
180
    /**
181
     * Prodloužení přihlášení o 30 min
182
     *
183
     * @throws UnexpectedValueException pokud se nepodaří naparsovat datum
184
     */
185 1
    public function updateLogoutTime(): self
186
    {
187 1
        $loginId = $this->getLoginId();
188 1
        if ($loginId === null) {
189
            // Nemáme token, uživatel není přihlášen a není, co prodlužovat
190
            return $this;
191
        }
192
193 1
        $result = $this->wsdlManager->getWebService('UserManagement', $loginId)->LoginUpdateRefresh(['ID' => $loginId]);
194
195 1
        $logoutDate = preg_replace('/\.(\d*)$/', '', $result->DateLogout); //skautIS vrací sekundy včetně desetinné části
196 1
        $tz = new \DateTimeZone('Europe/Prague');
197 1
        $logoutDate = DateTime::createFromFormat('Y-m-d\TH:i:s', $logoutDate, $tz);
198 1
        if ($logoutDate === false) {
199
            throw new UnexpectedValueException("Could not parse logout date '{$result->DateLogout}'.");
200
        }
201 1
        $this->loginData[self::LOGOUT_DATE] = $logoutDate;
202
203 1
        $this->saveToSession();
204
205 1
        return $this;
206
    }
207
208
    /**
209
     * Uloží nastavení do session
210
     */
211 3
    protected function saveToSession(): void
212
    {
213 3
        if ($this->session !== null) {
214
            $this->session->set(self::SESSION_ID, $this->loginData);
215
        }
216 3
    }
217
}
218