Passed
Push — master ( ed3164...71916c )
by Petr
02:23
created

TAuthLock::getLock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_auth_sources\Traits;
4
5
6
use kalanis\kw_auth_sources\AuthSourcesException;
7
use kalanis\kw_locks\Interfaces\ILock;
8
use kalanis\kw_locks\LockException;
9
10
11
/**
12
 * Trait TAuthLock
13
 * @package kalanis\kw_auth_sources\Traits
14
 */
15
trait TAuthLock
16
{
17
    use TLang;
18
19
    /** @var ILock|null */
20
    protected $lock = null;
21
22 55
    protected function initAuthLock(?ILock $lock): void
23
    {
24 55
        $this->lock = $lock;
25 55
    }
26
27
    /**
28
     * @throws AuthSourcesException
29
     * @return ILock
30
     */
31 30
    protected function getLock(): ILock
32
    {
33 30
        if (!$this->lock) {
34 1
            throw new AuthSourcesException($this->getAusLang()->kauLockSystemNotSet());
35
        }
36 29
        return $this->lock;
37
    }
38
39
    /**
40
     * @param string $note
41
     * @throws AuthSourcesException
42
     * @throws LockException
43
     */
44 30
    protected function checkLock(string $note = ''): void
45
    {
46 30
        if ($this->getLock()->has()) {
47 1
            throw new AuthSourcesException(empty($note) ? $this->getAusLang()->kauAuthAlreadyOpen() : $note);
48
        }
49 28
    }
50
}
51