Completed
Push — master ( 0fb455...ba0b4e )
by Hilmi Erdem
03:52 queued 02:19
created

GenericAccessToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Erdemkeren\TemporaryAccess;
4
5
use Carbon\Carbon;
6
use LogicException;
7
use Erdemkeren\TemporaryAccess\Token\TokenInterface;
8
use Erdemkeren\TemporaryAccess\Contracts\AccessTokenInterface;
9
10
final class GenericAccessToken implements AccessTokenInterface
11
{
12
    /**
13
     * The token.
14
     *
15
     * @var TokenInterface
16
     */
17
    private $token;
18
19
    /**
20
     * The attributes of the access token.
21
     *
22
     * @var array
23
     */
24
    private $attributes;
25
26
    /**
27
     * GenericAccessToken constructor.
28
     *
29
     * @param  TokenInterface $token The access token.
30
     * @param  array $attributes     The attributes of the access token.
31
     */
32 22
    public function __construct(TokenInterface $token, array $attributes)
33
    {
34 22
        $this->token = $token;
35 22
        $this->attributes = $attributes;
36 22
    }
37
38
    /**
39
     * Get the unique identifier of the authenticatable who owns the access token.
40
     *
41
     * @return string
42
     */
43 7
    public function authenticatableId()
44
    {
45 7
        return $this->getAttributeValue('authenticatable_id');
46
    }
47
48
    /**
49
     * Get the token.
50
     *
51
     * @return TokenInterface
52
     */
53 8
    public function token()
54
    {
55 8
        return $this->token;
56
    }
57
58
    /**
59
     * Get the access token as plain text.
60
     *
61
     * @return string
62
     * @throws LogicException
63
     */
64 2
    public function plain()
65
    {
66 2
        if (! $plainText = $this->token->plain()) {
67 1
            $message = 'The plain text is not available at this state.';
68 1
            throw new LogicException($message);
69
        }
70
71 1
        return $plainText;
72
    }
73
74
    /**
75
     * Get the access token encrypted.
76
     *
77
     * @return string
78
     */
79 6
    public function encrypted()
80
    {
81 6
        return (string) $this->token();
82
    }
83
84
    /**
85
     * Get the created at timestamp of the access token.
86
     *
87
     * @return \Carbon\Carbon
88
     */
89 7
    public function createdAt()
90
    {
91 7
        return new Carbon($this->getAttributeValue('created_at'));
92
    }
93
94
    /**
95
     * Get the expires at timestamp of the access token.
96
     *
97
     * @return \Carbon\Carbon
98
     */
99 6
    public function expiresAt()
100
    {
101 6
        return new Carbon($this->getAttributeValue('expires_at'));
102
    }
103
104
    /**
105
     * Get a new instance of the access token with a longer expire date.
106
     *
107
     * @param  int $prolong The prolong time in seconds.
108
     *
109
     * @return GenericAccessToken
110
     */
111 6
    public function prolong($prolong)
112
    {
113 6
        return new static($this->token(), [
114 6
            'authenticatable_id' => $this->authenticatableId(),
115 6
            'created_at'         => $this->createdAt(),
116 6
            'expires_at'         => (string) $this->expiresAt()->addSeconds($prolong),
117 6
        ]);
118
    }
119
120
    /**
121
     * Get the value of the given key.
122
     *
123
     * @param  string $key
124
     *
125
     * @return mixed
126
     */
127 8
    private function getAttributeValue($key)
128
    {
129 8
        return $this->attributes[$key];
130
    }
131
132
    /**
133
     * Convert the access token to string.
134
     *
135
     * @return string
136
     */
137 5
    public function __toString()
138
    {
139 5
        return $this->encrypted();
140
    }
141
}
142