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

Token::getExpiresAt()   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\Session;
13
14
use Spiral\Auth\TokenInterface;
15
16
final class Token implements TokenInterface, \JsonSerializable
17
{
18
    /** @var string */
19
    private $id;
20
21
    /** @var \DateTimeInterface|null */
22
    private $expiresAt;
23
24
    /** @var array */
25
    private $payload;
26
27
    /**
28
     * @param string                  $id
29
     * @param array                   $payload
30
     * @param \DateTimeInterface|null $expiresAt
31
     */
32
    public function __construct(string $id, array $payload, \DateTimeInterface $expiresAt = null)
33
    {
34
        $this->id = $id;
35
        $this->expiresAt = $expiresAt;
36
        $this->payload = $payload;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function __toString(): string
43
    {
44
        return $this->id;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function getID(): string
51
    {
52
        return $this->id;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getExpiresAt(): ?\DateTimeInterface
59
    {
60
        return $this->expiresAt;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getPayload(): array
67
    {
68
        return $this->payload;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function jsonSerialize(): string
75
    {
76
        return $this->getID();
77
    }
78
79
    /**
80
     * Pack token data into array form.
81
     *
82
     * @return array
83
     */
84
    public function pack(): array
85
    {
86
        return [
87
            'id'        => $this->id,
88
            'expiresAt' => $this->expiresAt,
89
            'payload'   => $this->payload
90
        ];
91
    }
92
93
    /**
94
     * Unpack token from serialized data.
95
     *
96
     * @param array $data
97
     * @return Token
98
     * @throws \Exception
99
     */
100
    public static function unpack(array $data): Token
101
    {
102
        $expiresAt = null;
103
        if ($data['expiresAt'] != null) {
104
            $expiresAt = (new \DateTimeImmutable())->setTimestamp($data['expiresAt']);
105
        }
106
107
        return new Token($data['id'], $data['payload'], $expiresAt);
0 ignored issues
show
Bug introduced by
It seems like $expiresAt can also be of type false; however, parameter $expiresAt of Spiral\Auth\Session\Token::__construct() does only seem to accept DateTimeInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return new Token($data['id'], $data['payload'], /** @scrutinizer ignore-type */ $expiresAt);
Loading history...
108
    }
109
}
110