Passed
Push — master ( e4bd8f...4069ad )
by Anton
02:47
created

Token::getHashedValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Auth\Cycle;
13
14
use Cycle\Annotated\Annotation as Cycle;
15
use Spiral\Auth\TokenInterface;
16
17
/**
18
 * @Cycle\Entity(table="auth_tokens")
19
 */
20
final class Token implements TokenInterface, \JsonSerializable
21
{
22
    /** @Cycle\Column(type="string(64)", primary=true) */
23
    private $id;
24
25
    /** @var string */
26
    private $secretValue;
27
28
    /** @Cycle\Column(type="string(128)", name="hashed_value") */
29
    private $hashedValue;
30
31
    /** @Cycle\Column(type="datetime") */
32
    private $createdAt;
33
34
    /** @Cycle\Column(type="datetime", nullable=true) */
35
    private $expiresAt;
36
37
    /** @Cycle\Column(type="blob") */
38
    private $payload;
39
40
    /**
41
     * @param string                  $id
42
     * @param string                  $secretValue
43
     * @param array                   $payload
44
     * @param \DateTimeImmutable      $createdAt
45
     * @param \DateTimeInterface|null $expiresAt
46
     */
47
    public function __construct(
48
        string $id,
49
        string $secretValue,
50
        array $payload,
51
        \DateTimeImmutable $createdAt,
52
        \DateTimeInterface $expiresAt = null
53
    ) {
54
        $this->id = $id;
55
56
        $this->secretValue = $secretValue;
57
        $this->hashedValue = hash('sha512', $secretValue);
58
59
        $this->createdAt = $createdAt;
60
        $this->expiresAt = $expiresAt;
61
62
        $this->payload = json_encode($payload);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function __toString(): string
69
    {
70
        return $this->getID();
71
    }
72
73
    /**
74
     * @param string $value
75
     */
76
    public function setSecretValue(string $value): void
77
    {
78
        $this->secretValue = $value;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function getID(): string
85
    {
86
        return sprintf('%s:%s', $this->id, $this->secretValue);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getHashedValue(): string
93
    {
94
        return $this->hashedValue;
95
    }
96
97
    /**
98
     * @return \DateTimeInterface
99
     */
100
    public function getCreatedAt(): \DateTimeInterface
101
    {
102
        return $this->createdAt;
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function getExpiresAt(): ?\DateTimeInterface
109
    {
110
        return $this->expiresAt;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function getPayload(): array
117
    {
118
        return json_decode($this->payload, true);
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function jsonSerialize(): string
125
    {
126
        return $this->getID();
127
    }
128
}
129