Passed
Pull Request — master (#20)
by Hilmi Erdem
10:18
created

OtpToken::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/*
3
 * Copyright (c) 2021. Hilmi Erdem Keren
4
 * license MIT
5
 */
6
7
namespace Erdemkeren\Otp;
8
9
use Carbon\Carbon;
10
11
/**
12
 * Class OtpToken.
13
 */
14
class OtpToken
15
{
16
    /**
17
     * OtpToken constructor.
18
     *
19
     * @param array $attributes
20
     */
21
    public function __construct(private array $attributes)
22
    {
23
        if (!array_key_exists('created_at', $this->attributes)) {
24
            $this->attributes['created_at'] = $this->getNow();
25
            $this->attributes['updated_at'] = $this->getNow();
26
        }
27
    }
28
29
    /**
30
     * Get the plain text.
31
     *
32
     * @return string|null
33
     */
34
    public function plainText(): ?string
35
    {
36
        return $this->getAttributeValue('plain_text');
37
    }
38
39
    /**
40
     * Get the cipher text of the token.
41
     *
42
     * @return string
43
     */
44
    public function cipherText(): string
45
    {
46
        return $this->getAttributeValue('cipher_text');
47
    }
48
49
    /**
50
     * Get the expiry time of the token.
51
     *
52
     * @return int
53
     */
54
    public function expiryTime(): int
55
    {
56
        return $this->getAttributeValue('expiry_time');
57
    }
58
59
    /**
60
     * Get the time left for the expiration
61
     *
62
     * @return int
63
     */
64
    public function timeLeft(): int
65
    {
66
        return $this->getNow()->diffInSeconds($this->expiresAt(), false);
67
    }
68
69
    /**
70
     * Get the expiry date.
71
     *
72
     * @return Carbon
73
     */
74
    public function expiresAt(): Carbon
75
    {
76
        return (clone $this->createdAt())->addSeconds($this->expiryTime());
77
    }
78
79
    /**
80
     * Get the authenticable id.
81
     *
82
     * @return int|string
83
     */
84
    public function authenticableId(): int|string
85
    {
86
        return $this->getAttributeValue('authenticable_id');
87
    }
88
89
    /**
90
     * Get the date token was created.
91
     *
92
     * @return Carbon
93
     */
94
    public function createdAt(): Carbon
95
    {
96
        return new Carbon($this->getAttributeValue('created_at'));
97
    }
98
99
    /**
100
     * Get the last date the token was updated.
101
     *
102
     * @return Carbon
103
     */
104
    public function updatedAt(): Carbon
105
    {
106
        return new Carbon($this->getAttributeValue('updated_at'));
107
    }
108
109
    /**
110
     * Get a new instance of the token
111
     * with an extended expiry time.
112
     *
113
     * @param int $secs
114
     *
115
     * @return $this
116
     */
117
    public function extend(int $secs): self
118
    {
119
        return new static(array_merge($this->attributes, [
120
            'expiry_time' => $this->expiryTime() + $secs,
121
            'updated_at' => $this->getNow(),
122
        ]));
123
    }
124
125
    /**
126
     * Get a new instance of the token
127
     * with a refreshed expiry time.
128
     */
129
    public function refresh(): self
130
    {
131
        return $this->extend($this->getNow()->diffInSeconds($this->updatedAt()));
132
    }
133
134
    /**
135
     * Get a new instance of the token
136
     * that is expired.
137
     */
138
    public function invalidate(): self
139
    {
140
        return new static(array_merge($this->attributes, [
141
            'expiry_time' => 0,
142
            'updated_at' => $this->getNow(),
143
        ]));
144
    }
145
146
    /**
147
     * Determine if the token is expired or not.
148
     *
149
     * @return bool
150
     */
151
    public function expired(): bool
152
    {
153
        return $this->timeLeft() <= 0;
154
    }
155
156
    /**
157
     * Get the token without the plain text.
158
     *
159
     * return self
160
     */
161
    public function withoutPlainText(): self
162
    {
163
        return new static(array_filter(array_merge($this->attributes, [
164
            'plain_text' => null,
165
        ])));
166
    }
167
168
    /**
169
     * Get the array representation of the token.
170
     *
171
     * @return array
172
     */
173
    public function toArray(): array
174
    {
175
        return $this->attributes;
176
    }
177
178
    /**
179
     * Get the date time at the moment.
180
     *
181
     * @return Carbon
182
     */
183
    private function getNow(): Carbon
184
    {
185
        return Carbon::now();
186
    }
187
188
    /**
189
     * Get the value of an attribute.
190
     *
191
     * @param string $key
192
     *
193
     * @return mixed
194
     */
195
    private function getAttributeValue(string $key): mixed
196
    {
197
        return array_key_exists($key, $this->attributes)
198
            ? $this->attributes[$key]
199
            : null;
200
    }
201
}
202