Completed
Pull Request — 2.x (#80)
by
unknown
02:00
created

Timer::getExpireTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Auth\Session;
10
11
use Aura\Auth\Exception;
12
use Aura\Auth\Status;
13
14
/**
15
 *
16
 * Timer
17
 *
18
 * @package Aura.Auth
19
 *
20
 */
21
class Timer
22
{
23
    /**
24
     * ini_gc_maxlifetime
25
     *
26
     * @var int
27
     * @access protected
28
     */
29
    protected $ini_gc_maxlifetime;
30
31
    /**
32
     * ini_cookie_lifetime
33
     *
34
     * @var int
35
     * @access protected
36
     */
37
    protected $ini_cookie_lifetime;
38
39
    /**
40
     *
41
     * Maximum idle time in seconds; zero is forever.
42
     *
43
     * @var int
44
     *
45
     */
46
    protected $idle_ttl = 1440;
47
48
    /**
49
     *
50
     * Maximum authentication lifetime in seconds; zero is forever.
51
     *
52
     * @var int
53
     *
54
     */
55
    protected $expire_ttl = 14400;
56
57
    /**
58
     *
59
     * Constructor.
60
     *
61
     * @param int $ini_gc_maxlifetime
62
     *
63
     * @param int $ini_cookie_lifetime
64
     *
65
     * @param int $idle_ttl The maximum idle time in seconds.
66
     *
67
     * @param int $expire_ttl The maximum authentication time in seconds.
68
     *
69
     */
70 13
    public function __construct(
71
        $ini_gc_maxlifetime = 1440,
72
        $ini_cookie_lifetime = 0,
73
        $idle_ttl = 1440,
74
        $expire_ttl = 14400
75
    ) {
76 13
        $this->ini_gc_maxlifetime = $ini_gc_maxlifetime;
77 13
        $this->ini_cookie_lifetime = $ini_cookie_lifetime;
78 13
        $this->setIdleTtl($idle_ttl);
79 13
        $this->setExpireTtl($expire_ttl);
80 13
    }
81
82
    /**
83
     *
84
     * Sets the maximum idle time.
85
     *
86
     * @param int $idle_ttl The maximum idle time in seconds.
87
     *
88
     * @throws Exception when the session garbage collection max lifetime is
89
     * less than the idle time.
90
     *
91
     * @return null
92
     *
93
     */
94 13
    public function setIdleTtl($idle_ttl)
95
    {
96 13
        if ($this->ini_gc_maxlifetime < $idle_ttl) {
97 1
            throw new Exception('session.gc_maxlifetime less than idle time');
98
        }
99 13
        $this->idle_ttl = $idle_ttl;
100 13
    }
101
102
    /**
103
     *
104
     * Returns the maximum idle time.
105
     *
106
     * @return int
107
     *
108
     */
109 4
    public function getIdleTtl()
110
    {
111 4
        return $this->idle_ttl;
112
    }
113
114
    /**
115
     *
116
     * Sets the maximum authentication lifetime.
117
     *
118
     * @param int $expire_ttl The maximum authentication lifetime in seconds.
119
     *
120
     * @throws Exception when the session cookie lifetime is less than the
121
     * authentication lifetime.
122
     *
123
     * @return null
124
     *
125
     */
126 13
    public function setExpireTtl($expire_ttl)
127
    {
128 13
        $bad = $this->ini_cookie_lifetime > 0
129 13
            && $this->ini_cookie_lifetime < $expire_ttl;
130 13
        if ($bad) {
131 1
            throw new Exception('session.cookie_lifetime less than expire time');
132
        }
133 13
        $this->expire_ttl = $expire_ttl;
134 13
    }
135
136
    /**
137
     *
138
     * Returns the maximum authentication lifetime.
139
     *
140
     * @return int
141
     *
142
     */
143 5
    public function getExpireTtl()
144
    {
145 5
        return $this->expire_ttl;
146
    }
147
148
    /**
149
     *
150
     * Has the authentication time expired?
151
     *
152
     * @param int $first_active
153
     *
154
     * @return bool
155
     *
156
     */
157 5
    public function hasExpired($first_active)
158
    {
159 5
        return $this->expire_ttl <= 0
160 5
            || ($first_active + $this->getExpireTtl()) < time();
161
    }
162
163
    /**
164
     *
165
     * Has the idle time been exceeded?
166
     *
167
     * @param int $last_active
168
     *
169
     * @return bool
170
     *
171
     */
172 4
    public function hasIdled($last_active)
173
    {
174 4
        return $this->idle_ttl <= 0
175 4
            || ($last_active + $this->getIdleTtl()) < time();
176
    }
177
178
    /**
179
     *
180
     * Get Timeout Status
181
     *
182
     * @param int $first_active
183
     *
184
     * @param int $last_active
185
     *
186
     * @return string
187
     *
188
     */
189 4
    public function getTimeoutStatus($first_active, $last_active)
190
    {
191 4
        if ($this->hasExpired($first_active)) {
192 2
            return Status::EXPIRED;
193
        }
194
195 3
        if ($this->hasIdled($last_active)) {
196 2
            return Status::IDLE;
197
        }
198 2
    }
199
}
200