LockAccountTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 79
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A incAuthFailures() 0 3 2
A canAccountBeLocked() 0 3 1
A isAccountNonLocked() 0 5 4
A unlockAccount() 0 6 1
A lockAccount() 0 8 3
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017-2018 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace LazySec\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
16
/**
17
 * Trait for "lock account" feature.
18
 */
19
trait LockAccountTrait
20
{
21
    /**
22
     * @var int Number of consecutive unsuccessful attempts to authenticate.
23
     *
24
     * @ORM\Column(name="auth_failures", type="integer", nullable=true)
25
     */
26
    protected $authFailures;
27
28
    /**
29
     * @var int Unix Epoch timestamp which the account is locked till.
30
     *          When zero, the account is considered as locked for permanent.
31
     *
32
     * @ORM\Column(name="locked_until", type="integer", nullable=true)
33
     */
34
    protected $lockedUntil;
35
36
    /**
37
     * Checks whether the user is locked.
38
     *
39
     * Inherited from '\Symfony\Component\Security\Core\User\AdvancedUserInterface'.
40
     *
41
     * @return bool TRUE if the user is not locked, FALSE otherwise.
42
     */
43 5
    public function isAccountNonLocked(): bool
44
    {
45 5
        return $this->canAccountBeLocked()
46 5
            ? $this->lockedUntil === null || $this->lockedUntil !== 0 && $this->lockedUntil <= time()
47 5
            : true;
48
    }
49
50
    /**
51
     * Increases number of authentication failures.
52
     *
53
     * @return null|int New authentication failures number.
54
     */
55 1
    public function incAuthFailures(): ?int
56
    {
57 1
        return $this->canAccountBeLocked() ? ++$this->authFailures : null;
58
    }
59
60
    /**
61
     * Locks the account until specified moment of time (NULL for permanent lock).
62
     *
63
     * @param \DateTime $time
64
     *
65
     * @return self
66
     */
67 2
    public function lockAccount(\DateTime $time = null): self
68
    {
69 2
        if ($this->canAccountBeLocked()) {
70 2
            $this->authFailures = null;
71 2
            $this->lockedUntil  = $time === null ? 0 : $time->getTimestamp();
72
        }
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * Unlocks the account.
79
     *
80
     * @return self
81
     */
82 1
    public function unlockAccount(): self
83
    {
84 1
        $this->authFailures = null;
85 1
        $this->lockedUntil  = null;
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Specifies whether the "lock account" feature is available for this user.
92
     *
93
     * @return bool
94
     */
95 6
    protected function canAccountBeLocked(): bool
96
    {
97 6
        return true;
98
    }
99
}
100