Test Failed
Pull Request — master (#80)
by Maximo
05:46
created

TokenTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 55
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenTimeNotBefore() 0 3 1
A getToken() 0 3 1
A getTokenTimeExpiration() 0 3 1
A getTokenTimeIssuedAt() 0 3 1
A getTokenAudience() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Lcobucci\JWT\Parser;
8
use Lcobucci\JWT\Token;
9
use function Gewaer\Core\envValue;
10
use function time;
11
12
/**
13
 * Trait TokenTrait.
14
 *
15
 * @package Niden\Traits
16
 */
17
trait TokenTrait
18
{
19
    /**
20
     * Returns the JWT token object.
21
     *
22
     * @param string $token
23
     *
24
     * @return Token
25
     */
26
    protected function getToken(string $token): Token
27
    {
28
        return (new Parser())->parse($token);
29
    }
30
31
    /**
32
     * Returns the default audience for the tokens.
33
     *
34
     * @return string
35
     */
36
    protected function getTokenAudience(): string
37
    {
38
        /** @var string $audience */
39
        $audience = envValue('TOKEN_AUDIENCE', '');
40
41
        return $audience;
42
    }
43
44
    /**
45
     * Returns the time the token is issued at.
46
     *
47
     * @return int
48
     */
49
    protected function getTokenTimeIssuedAt(): int
50
    {
51
        return time();
52
    }
53
54
    /**
55
     * Returns the time drift i.e. token will be valid not before.
56
     *
57
     * @return int
58
     */
59
    protected function getTokenTimeNotBefore(): int
60
    {
61
        return (time() + envValue('TOKEN_NOT_BEFORE', 10));
62
    }
63
64
    /**
65
     * Returns the expiry time for the token.
66
     *
67
     * @return int
68
     */
69
    protected function getTokenTimeExpiration(): int
70
    {
71
        return (time() + envValue('TOKEN_EXPIRATION', 86400));
72
    }
73
}
74