Completed
Push — master ( 61acac...62012c )
by Gorka
03:59
created

UserToken::isExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Model;
14
15
use Ramsey\Uuid\Uuid;
16
17
/**
18
 * User token domain class.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 * @author Gorka Laucirica <[email protected]>
22
 */
23
final class UserToken
24
{
25
    /**
26
     * The id in a primitive type.
27
     *
28
     * @var string|int
29
     */
30
    private $token;
31
32
    /**
33
     * The created on.
34
     *
35
     * @var \DateTimeImmutable
36
     */
37
    private $createdOn;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param string|null $token User token. New will be generated if empty
43
     */
44
    public function __construct($token = null)
45
    {
46
        $this->token = $token ?: Uuid::uuid4()->toString();
47
        $this->createdOn = new \DateTimeImmutable();
48
    }
49
50
    /**
51
     * Gets the id.
52
     *
53
     * @return string|int
54
     */
55
    public function token()
56
    {
57
        return $this->token;
58
    }
59
60
    /**
61
     * Gets the created on.
62
     *
63
     * @return \DateTimeImmutable
64
     */
65
    public function createdOn()
66
    {
67
        return $this->createdOn;
68
    }
69
70
    /**
71
     * Method that checks if the id given is equal to the current.
72
     *
73
     * @param UserToken $aToken The token
74
     *
75
     * @return bool
76
     */
77
    public function equals(UserToken $aToken)
78
    {
79
        return $this->token === $aToken->token();
80
    }
81
82
    /**
83
     * Checks if the token is expired comparing
84
     * with the given lifetime.
85
     *
86
     * @param int $lifetime The lifetime of the token
87
     *
88
     * @return bool
89
     */
90
    public function isExpired($lifetime)
91
    {
92
        $interval = $this->createdOn->diff(new \DateTimeImmutable());
93
94
        return $interval->s >= (int) $lifetime;
95
    }
96
97
    /**
98
     * Magic method that represents the token in string format.
99
     *
100
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
101
     */
102
    public function __toString()
103
    {
104
        return $this->token();
105
    }
106
}
107