Completed
Pull Request — master (#479)
by Andrey
02:39
created

AbstractToken::setEndOfLife()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 = array();
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 = array())
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
    /**
69
     * @param array $extraParams
70
     */
71
    public function setExtraParams(array $extraParams)
72
    {
73
        $this->extraParams = $extraParams;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getExtraParams()
80
    {
81
        return $this->extraParams;
82
    }
83
84
    /**
85
     * @param string $accessToken
86
     */
87
    public function setAccessToken($accessToken)
88
    {
89
        $this->accessToken = $accessToken;
90
    }
91
92
    /**
93
     * @param int $endOfLife
94
     */
95
    public function setEndOfLife($endOfLife)
96
    {
97
        $this->endOfLife = $endOfLife;
98
    }
99
100
    /**
101
     * @param int $lifetime
102
     */
103
    public function setLifetime($lifetime)
104
    {
105
        if (0 === $lifetime || static::EOL_NEVER_EXPIRES === $lifetime) {
106
            $this->endOfLife = static::EOL_NEVER_EXPIRES;
107
        } elseif (null !== $lifetime) {
108
            $this->endOfLife = intval($lifetime) + time();
109
        } else {
110
            $this->endOfLife = static::EOL_UNKNOWN;
111
        }
112
    }
113
114
    /**
115
     * @param string $refreshToken
116
     */
117
    public function setRefreshToken($refreshToken)
118
    {
119
        $this->refreshToken = $refreshToken;
120
    }
121
122
    public function isExpired()
123
    {
124
        return ($this->getEndOfLife() !== TokenInterface::EOL_NEVER_EXPIRES
125
        && $this->getEndOfLife() !== TokenInterface::EOL_UNKNOWN
126
        && time() > $this->getEndOfLife());
127
    }
128
}
129