Passed
Push — master ( 7853d0...c14c44 )
by Carlos Martínez
01:06 queued 17s
created

AbstractToken::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 1
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\Common\Token;
4
5
/**
6
 * Base token implementation for any OAuth version.
7
 */
8
abstract class AbstractToken implements TokenInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $accessToken;
14
15
    /**
16
     * @var string
17
     */
18
    protected $refreshToken;
19
20
    /**
21
     * @var int
22
     */
23
    protected $endOfLife;
24
25
    /**
26
     * @var array
27
     */
28
    protected $extraParams = [];
29
30
    /**
31
     * @param string $accessToken
32
     * @param string $refreshToken
33
     * @param int    $lifetime
34
     * @param array  $extraParams
35
     */
36
    public function __construct($accessToken = null, $refreshToken = null, $lifetime = null, $extraParams = [])
37
    {
38
        $this->accessToken = $accessToken;
39
        $this->refreshToken = $refreshToken;
40
        $this->setLifetime($lifetime);
41
        $this->extraParams = $extraParams;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getAccessToken()
48
    {
49
        return $this->accessToken;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getRefreshToken()
56
    {
57
        return $this->refreshToken;
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function getEndOfLife()
64
    {
65
        return $this->endOfLife;
66
    }
67
68
    public function setExtraParams(array $extraParams): void
69
    {
70
        $this->extraParams = $extraParams;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getExtraParams()
77
    {
78
        return $this->extraParams;
79
    }
80
81
    /**
82
     * @param string $accessToken
83
     */
84
    public function setAccessToken($accessToken): void
85
    {
86
        $this->accessToken = $accessToken;
87
    }
88
89
    /**
90
     * @param int $endOfLife
91
     */
92
    public function setEndOfLife($endOfLife): void
93
    {
94
        $this->endOfLife = $endOfLife;
95
    }
96
97
    /**
98
     * @param int $lifetime
99
     */
100
    public function setLifetime($lifetime): void
101
    {
102
        if (0 === $lifetime || static::EOL_NEVER_EXPIRES === $lifetime) {
103
            $this->endOfLife = static::EOL_NEVER_EXPIRES;
104
        } elseif (null !== $lifetime) {
0 ignored issues
show
introduced by
The condition null !== $lifetime is always true.
Loading history...
105
            $this->endOfLife = (int) $lifetime + time();
106
        } else {
107
            $this->endOfLife = static::EOL_UNKNOWN;
108
        }
109
    }
110
111
    /**
112
     * @param string $refreshToken
113
     */
114
    public function setRefreshToken($refreshToken): void
115
    {
116
        $this->refreshToken = $refreshToken;
117
    }
118
119
    public function isExpired()
120
    {
121
        return $this->getEndOfLife() !== TokenInterface::EOL_NEVER_EXPIRES
122
        && $this->getEndOfLife() !== TokenInterface::EOL_UNKNOWN
123
        && time() > $this->getEndOfLife();
124
    }
125
}
126