Passed
Push — master ( 436c58...98654e )
by Anton
06:21 queued 03:52
created

Token::getPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
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
 * @Cycle\Table(indexes={
20
 *     @Cycle\Table\Index(columns={"id", "hash"}, unique=true)
21
 * })
22
 */
23
final class Token implements TokenInterface, \JsonSerializable
24
{
25
    /** @Cycle\Column(type="bigPrimary") */
26
    private $id;
27
28
    /** @Cycle\Column(type="string(128)") */
29
    private $hash;
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                  $hash
42
     * @param array                   $payload
43
     * @param \DateTimeImmutable      $createdAt
44
     * @param \DateTimeInterface|null $expiresAt
45
     */
46
    public function __construct(
47
        string $hash,
48
        array $payload,
49
        \DateTimeImmutable $createdAt,
50
        \DateTimeInterface $expiresAt = null
51
    ) {
52
        $this->hash = $hash;
53
        $this->createdAt = $createdAt;
54
        $this->expiresAt = $expiresAt;
55
        $this->payload = json_encode($payload);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function __toString(): string
62
    {
63
        return $this->id;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function getID(): string
70
    {
71
        return sprintf('%s:%s', $this->id, $this->hash);
72
    }
73
74
    /**
75
     * @return \DateTimeInterface
76
     */
77
    public function getCreatedAt(): \DateTimeInterface
78
    {
79
        return $this->createdAt;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function getExpiresAt(): ?\DateTimeInterface
86
    {
87
        return $this->expiresAt;
88
    }
89
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function getPayload(): array
95
    {
96
        return json_decode($this->payload, true);
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function jsonSerialize(): string
103
    {
104
        return $this->getID();
105
    }
106
}
107