Completed
Push — master ( 229a32...055b70 )
by Artem
02:23
created

ExpirePasswordTrait::canPasswordBeExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 "password expiration" feature.
18
 */
19 View Code Duplication
trait ExpirePasswordTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * @var int Unix Epoch timestamp when the password expires.
23
     *
24
     * @ORM\Column(name="password_expires_at", type="integer", nullable=true)
25
     */
26
    protected $passwordExpiresAt;
27
28
    /**
29
     * Makes password to expire at specified moment of time (NULL for no expiration).
30
     *
31
     * @param \DateTime $time
32
     *
33
     * @return self
34
     */
35 1
    public function expirePasswordAt(\DateTime $time = null)
36
    {
37 1
        if ($this->canPasswordBeExpired()) {
38 1
            $this->passwordExpiresAt = $time === null ? null : $time->getTimestamp();
39
        }
40
41 1
        return $this;
42
    }
43
44
    /**
45
     * Makes password to expire in specified period of time.
46
     *
47
     * @param \DateInterval $interval
48
     *
49
     * @return self
50
     */
51 1
    public function expirePasswordIn(\DateInterval $interval)
52
    {
53 1
        if ($this->canPasswordBeExpired()) {
54 1
            $this->passwordExpiresAt = date_create()->add($interval)->getTimestamp();
55
        }
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Specifies whether the "password expiration" feature is available for this user.
62
     *
63
     * @return bool
64
     */
65 2
    protected function canPasswordBeExpired(): bool
66
    {
67 2
        return true;
68
    }
69
}
70