Completed
Push — master ( 055b70...427a37 )
by Artem
03:41
created

LockAccountTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 67
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A incAuthFailures() 0 4 2
A lockAccount() 0 9 3
A unlockAccount() 0 7 1
A canAccountBeLocked() 0 4 1
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017 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 Pignus\Model;
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
     * Increases number of authentication failures.
38
     *
39
     * @return int|null New authentication failures number.
40
     */
41 3
    public function incAuthFailures()
42
    {
43 3
        return $this->canAccountBeLocked() ? ++$this->authFailures : null;
44
    }
45
46
    /**
47
     * Locks the account until specified moment of time (NULL for permanent lock).
48
     *
49
     * @param \DateTime $time
50
     *
51
     * @return self
52
     */
53 4
    public function lockAccount(\DateTime $time = null)
54
    {
55 4
        if ($this->canAccountBeLocked()) {
56 4
            $this->authFailures = null;
57 4
            $this->lockedUntil  = $time === null ? 0 : $time->getTimestamp();
58
        }
59
60 4
        return $this;
61
    }
62
63
    /**
64
     * Unlocks the account.
65
     *
66
     * @return self
67
     */
68 2
    public function unlockAccount()
69
    {
70 2
        $this->authFailures = null;
71 2
        $this->lockedUntil  = null;
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * Specifies whether the "lock account" feature is available for this user.
78
     *
79
     * @return bool
80
     */
81 7
    protected function canAccountBeLocked(): bool
82
    {
83 7
        return true;
84
    }
85
}
86