Token::getAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace FMCSSOClient;
4
5
use Carbon\Carbon;
6
7
class Token
8
{
9
    protected string $tokenType;
10
11
    protected ?\DateTimeInterface $expiresAt;
12
13
    protected string $accessToken;
14
15
    protected string $refreshToken;
16
17
    /**
18
     * @param string $tokenType
19
     * @param int $expiresIn
20
     * @param string $accessToken
21
     * @param string $refreshToken
22
     */
23 9
    public function __construct(string $tokenType, int $expiresIn, string $accessToken, string $refreshToken)
24
    {
25 9
        $this->tokenType    = $tokenType;
26 9
        $this->expiresAt    = Carbon::now()->addSeconds($expiresIn);
27 9
        $this->accessToken  = $accessToken;
28 9
        $this->refreshToken = $refreshToken;
29
    }
30
31
    /**
32
     * Check is access token valid.
33
     *
34
     * @return bool
35
     */
36 9
    public function valid(): bool
37
    {
38 9
        return $this->accessToken && Carbon::now()->addMinute()->lessThan($this->expiresAt);
39
    }
40
41
    /**
42
     * @return \DateTimeInterface
43
     */
44 3
    public function getExpiresAt(): \DateTimeInterface
45
    {
46 3
        if ($this->valid()) {
47 3
            return $this->expiresAt;
48
        }
49
50
        return Carbon::now();
51
    }
52
53
    /**
54
     * @return string
55
     */
56 6
    public function getAccessToken(): string
57
    {
58 6
        return $this->accessToken;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 4
    public function getRefreshToken(): string
65
    {
66 4
        return $this->refreshToken;
67
    }
68
69
    /**
70
     * Get Authorization header value
71
     *
72
     * @return string|null
73
     */
74 6
    public function getAuthorizationHeader(): ?string
75
    {
76 6
        if ($this->valid()) {
77 6
            return "{$this->tokenType} {$this->getAccessToken()}";
78
        }
79
80
        return null;
81
    }
82
83 1
    public function __serialize(): array
84
    {
85
        return [
86 1
            'tokenType'    => $this->tokenType,
87 1
            'refreshToken' => $this->refreshToken,
88 1
            'accessToken'  => $this->accessToken,
89 1
            'expiresAt'    => $this->expiresAt->getTimestamp(),
0 ignored issues
show
Bug introduced by
The method getTimestamp() does not exist on null. ( Ignorable by Annotation )

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

89
            'expiresAt'    => $this->expiresAt->/** @scrutinizer ignore-call */ getTimestamp(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        ];
91
    }
92
93 1
    public function __unserialize(array $data): void
94
    {
95 1
        $this->tokenType    = $data['tokenType']    ?? '';
96 1
        $this->accessToken  = $data['accessToken']  ?? '';
97 1
        $this->refreshToken = $data['refreshToken'] ?? '';
98 1
        $this->expiresAt    = Carbon::createFromTimestamp($data['expiresAt'] ?? 0);
99
    }
100
}
101