Passed
Push — master ( 033b76...80b8b4 )
by Anton
02:44
created

Token::pack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 6
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
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
     * @inheritDoc
41
     */
42
    public function getID(): string
43
    {
44
        return $this->id;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function getExpiresAt(): ?\DateTimeInterface
51
    {
52
        return $this->expiresAt;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getPayload(): array
59
    {
60
        return $this->payload;
61
    }
62
63
    /**
64
     * Pack token data into array form.
65
     *
66
     * @return array
67
     */
68
    public function pack(): array
69
    {
70
        return [
71
            'id'        => $this->id,
72
            'expiresAt' => $this->expiresAt,
73
            'payload'   => $this->payload
74
        ];
75
    }
76
77
    /**
78
     * Unpack token from serialized data.
79
     *
80
     * @param array $data
81
     * @return Token
82
     * @throws \Exception
83
     */
84
    public static function unpack(array $data): Token
85
    {
86
        $expiresAt = null;
87
        if ($data['expiresAt'] != null) {
88
            $expiresAt = (new \DateTimeImmutable())->setTimestamp($data['expiresAt']);
89
        }
90
91
        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

91
        return new Token($data['id'], $data['payload'], /** @scrutinizer ignore-type */ $expiresAt);
Loading history...
92
    }
93
}
94